【问题标题】:Elasticsearch filter document ids with same field values countElasticsearch 过滤具有相同字段值的文档 ID 计数
【发布时间】:2020-03-14 19:55:58
【问题描述】:

我需要返回具有相同 myField 值重复 [range] 次的文档的 ID。 即,我有 clientName 字段,并希望找到 clientName = 'John' 的所有文档的 ID,但前提是在 2-3 个文档中找到此名称。

我有以下查询

_search?filter_path=hits.total,hits.hits._id


"query": {
    some filters that return matching document ids
 },
"post_filter": {
    "bool": {
        "should": [
            {
                "range": {
                    "myField": {
                        "lte": 3,
                        "gte": 2
                    }
                }
            }
        ],
        "minimum_should_match": 1
    }
}

但它返回 myField 值的范围而不是 myField 值计数。 我尝试使用聚合,但聚合过滤器不适用于主查询结果。 如何修改我的 post_filter 以获得我需要的结果?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    后过滤器:

    post_filter 应用于搜索请求最后的搜索命中,在聚合已经计算出来之后

    您不能根据查询或 post_filter 中某个字段的出现次数来过滤文档,也不能使用聚合结果来过滤查询中的文档。

    可以通过两种方式解决问题

    1.获取给定次数出现的术语并在文档中搜索这些术语(2 次调用弹性搜索)

    2。使用 top_hits

    在聚合中获取文档

    映射:

    {
      "index48" : {
        "mappings" : {
          "properties" : {
            "name" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            }
          }
        }
      }
    }
    

    查询:

    {
      "size": 0,
      "aggs": {
        "NAME": {
          "terms": {
            "field": "name.keyword",
            "size": 10
          },
          "aggs": {
            "documents": {
              "top_hits": {
                "size": 10
              }
            },
            "bucket_count": {
            "bucket_selector": {
              "buckets_path": {
                "path": "_count"
              },
              "script": "if(params.path>=1 && params.path<=3) return true"
            }
          }
          }
        }
      }
    }
    

    结果:

    "aggregations" : {
        "NAME" : {
          "doc_count_error_upper_bound" : 0,
          "sum_other_doc_count" : 0,
          "buckets" : [
            {
              "key" : "John",
              "doc_count" : 3,
              "documents" : {
                "hits" : {
                  "total" : {
                    "value" : 3,
                    "relation" : "eq"
                  },
                  "max_score" : 1.0,
                  "hits" : [
                    {
                      "_index" : "index48",
                      "_type" : "_doc",
                      "_id" : "ZRPC3HABF9RqmGpImxj_",
                      "_score" : 1.0,
                      "_source" : {
                        "name" : "John"
                      }
                    },
                    {
                      "_index" : "index48",
                      "_type" : "_doc",
                      "_id" : "ZhPC3HABF9RqmGpIpBh4",
                      "_score" : 1.0,
                      "_source" : {
                        "name" : "John"
                      }
                    },
                    {
                      "_index" : "index48",
                      "_type" : "_doc",
                      "_id" : "ZxPC3HABF9RqmGpIqRhj",
                      "_score" : 1.0,
                      "_source" : {
                        "name" : "John"
                      }
                    }
                  ]
                }
              }
            },
            {
              "key" : "Doe",
              "doc_count" : 2,
              "documents" : {
                "hits" : {
                  "total" : {
                    "value" : 2,
                    "relation" : "eq"
                  },
                  "max_score" : 1.0,
                  "hits" : [
                    {
                      "_index" : "index48",
                      "_type" : "_doc",
                      "_id" : "aBPC3HABF9RqmGpIyhhU",
                      "_score" : 1.0,
                      "_source" : {
                        "name" : "Doe"
                      }
                    },
                    {
                      "_index" : "index48",
                      "_type" : "_doc",
                      "_id" : "aRPC3HABF9RqmGpIzhh7",
                      "_score" : 1.0,
                      "_source" : {
                        "name" : "Doe"
                      }
                    }
                  ]
                }
              }
            }
          ]
        }
      }
    

    【讨论】:

      猜你喜欢
      • 2021-04-09
      • 1970-01-01
      • 2013-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-17
      • 2021-05-24
      • 2012-08-12
      相关资源
      最近更新 更多