elasticsearch - 在子字段中进行排除(不得)搜索
问题描述
我想找到街道名称中不包含“大”一词的所有城市。
这是我在索引中的项目:
{
"name":"the big street",
"city": "NY",
"count": 2
},
{
"name":"not big street",
"city": "AM",
"count": 43
},
{
"name":"st42",
"city": "NY",
"count": 1687
},
{
"name":"Anacostia Drive",
"city": "WASH",
"count": 1687
},
我需要找到名称不得包含(排除)“大”一词的所有城市。
当我尝试将术语查询与 must_not 一起使用时
{
"collapse": {
"field": "city"
},
"query": {
"bool" : {
"must_not" : {
"term" : {
"name" : { "big" }
}
},
}
}
}
我发现城市包括“NY”,到期项目与“st42” - 与“大街道”项目的正确匹配是错误的。
如何解决?
下面的决定很好,但查询可能更复杂,我需要突出显示:
{
"aggs": {
"aggs_by_city": {
"terms": {
"field": "city.keyword",
"size": 20
},
"aggs": {
"filter_exclude_count": {
"filter": { "match" : { "name" : "big" }}
},
"filter_include_count": {
"filter": { "match" : { "name" : "Anacostia" }}
},
"selector": {
"bucket_selector": {
"buckets_path": {
"exclude_cnt": "filter_exclude_count._count",
"include_name": "filter_include_count._count"
},
"script": "params.exclude_cnt == 0 && params.include_name > 0"
}
}
}
}
},
"size": 0
}
解决方案
看起来你想“找到所有没有街道名称为“大”的街道的城市?
您可以使用aggregation
,此解决方案不使用collapse
:
{
"aggs": {
"aggs_by_city": {
"terms": {
"field": "city",
"size": 10
},
"aggs": {
"filter_exclude_count": {
"filter": { "match" : { "street" : "big" }}
},
"sellector": {
"bucket_selector": {
"buckets_path": {
"exclude_cnt": "filter_exclude_count._count"
},
"script": "params.exclude_cnt == 0"
}
}
}
}
},
"size": 0
}
- 分组所有城市
- 统计街道名称中包含“大”的所有文件
- 如果上述计数 == 0,则使用bucket_selector
aggs_by_city
选择此聚合
推荐阅读
- android - 我想关闭适配器中的覆盖方法。解决方法是好习惯吗?
- android - 视图寻呼机适配器中的回调更改为空对象
- javascript - Amped Studio 2 - 声音引擎
- visual-studio-code - Visual Studio Code snake_case 单词选择,如 IntelliJ Camel Humps
- javascript - Facebook 登录按钮:已连接
- react-admin - 你可以在配置中禁用 React-Admin Undo 功能吗?
- python - 如何为数据框中的每个单独类别绘制一周中的每一天
- c - 运行时不可分割的循环大小对 openMP SIMD 的影响
- php - php函数(将html添加到主题)不起作用
- php - Laravel 工作检查网站(如果在线)