elasticsearch - 如何在 DSL 查询中对搜索参数应用过滤器
问题描述
我在弹性搜索中保存了大量客户数据
我的查询如下
{"query": {"query_string": {"query": "Mobile"}}}
- 问题 1
我需要过滤掉{country: Germany}
"filter": [ { "term": { "Country.keyword": "Germany" }}]
- 问题2
我需要过滤掉
{country: Germany}
和{continent:Europe}
解决方案
添加带有索引数据、搜索查询和搜索结果的工作示例
{
"item":"mobile are essential",
"Country":"India",
"continent":"Europe"
}
{
"item":"mobile",
"Country":"Germany",
"continent":"Europe"
}
{
"item":"mobile",
"Country":"Germany",
"continent":"asia"
}
对于您的第一个问题,need to filter out {country: Germany}
您需要将查询包装在 bool 查询中:
{
"query": {
"bool": {
"must": {
"query_string": {
"query": "Mobile"
}
},
"filter": {
"term": {
"Country.keyword": "Germany"
}
}
}
}
}
搜索结果:
"hits": [
{
"_index": "stof_64278091",
"_type": "_doc",
"_id": "1",
"_score": 0.15965708,
"_source": {
"item": "mobile",
"Country": "Germany",
"continent": "Europe"
}
},
{
"_index": "stof_64278091",
"_type": "_doc",
"_id": "3",
"_score": 0.15965708,
"_source": {
"item": "mobile",
"Country": "Germany",
"continent": "asia"
}
}
]
对于您的第二个问题,请尝试以下查询:
{
"query": {
"bool": {
"must": {
"query_string": {
"query": "Mobile"
}
},
"filter": [
{
"term": {
"Country.keyword": "Germany"
}
},
{
"term": {
"continent": "Europe"
}
}
]
}
}
}
搜索结果:
"hits": [
{
"_index": "stof_64278091",
"_type": "_doc",
"_id": "1",
"_score": 0.22920428,
"_source": {
"item": "mobile",
"Country": "Germany",
"continent": "Europe"
}
}
]
推荐阅读
- java - 我在 Android Studio 中的操作栏的 setHomeAsUpIndicator() 似乎不起作用
- c# - 半透明地形问题(可能是深度缓冲区问题)
- javascript - Ajax 请求没有收到响应
- python - 如何在 django sql 格式中转义引号
- html - 当我添加导航栏时,我的内容下方会添加空格。我该如何删除这个?
- javascript - Electron 收据热敏打印机
- javascript - 无法使用 API 密钥作为环境变量与 asyncData() 中的 Butter CMS 库
- ruby-on-rails - 如何使用 pry 在 Rails 控制台中回滚失败的事务
- tensorflow - ValueError:您正在尝试将包含 5 层的权重文件加载到具有 0 层的模型中
- python - 无法理解 scipy.sparse.csr_matrix 示例