在子字段中排除搜索(不得)

Search with exclusion (MUSTNOT) in children fields

我想找到街道名称中不包含术语 "big" 的所有城市。

这是我在索引中的项目:

{
  "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
},

我需要找到所有 城市 ,其中名称不得包含(排除)术语 "big"。

当我尝试将术语查询与 must_not

一起使用时
{
  "collapse": {
    "field": "city"
  },
  "query": {
    "bool" : {
"must_not" : {
        "term" : {
          "name" : { "big" }
        }
      },
    }
 }
}

我发现城市包括 "NY",到期项目 "st42" - 与 "the big street" 项目的到期匹配错误。

如何解决?

下面的决定很好,但是查询可能会更复杂,我需要突出显示:

{
  "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
}

您似乎想"find all the cities which does not have any street which has "在街道名称中加大“?

您可以使用 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
}
  1. 分组所有城市
  2. 统计街道名称包含"big"
  3. 的所有文档
  4. 如果上面的计数 == 0 那么 select 这个 aggs_by_city 聚合使用 bucket_selector