首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有相同字段值计数的Elasticsearch筛选文档ids

具有相同字段值计数的Elasticsearch筛选文档ids
EN

Stack Overflow用户
提问于 2020-03-14 19:55:58
回答 1查看 640关注 0票数 0

我需要返回具有相同myField值、重复范围时间的文档的ids。也就是说,我有clientName字段,并且希望找到所有文档的in,其中clientName = 'John‘,但前提是这个名称在2-3个文档中找到。

我有以下查询

代码语言:javascript
复制
_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以获得所需的结果?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-15 06:50:34

Post_filter:

post_filter应用于搜索请求末尾的搜索命中,在已经计算了聚合之后。

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

解决这个问题的方法有两种

1.获取给定次数的项,并搜索这些项的文档(2次对弹性搜索的调用)

2.使用 顶部获取聚合文档本身

制图:

代码语言:javascript
复制
{
  "index48" : {
    "mappings" : {
      "properties" : {
        "name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}

查询:

代码语言:javascript
复制
{
  "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"
        }
      }
      }
    }
  }
}

结果:

代码语言:javascript
复制
"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"
                  }
                }
              ]
            }
          }
        }
      ]
    }
  }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60686769

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档