Elasticsearch在聚合中指定索引过滤器

时间:2019-02-07 07:30:03

标签: elasticsearch

我有一个弹性查询聚合,其中我需要根据索引名称过滤聚合。查询部分实际上在多个索引上工作,但是我想过滤特定索引的聚合。请帮助我如何在聚合中通过索引过滤器-

{       
    "query": {
        "bool": {
            "filter": [
                {
                    "bool": {
                        "should": [
                            {
                                "query_string": {
                                    "fields": [
                                        "productDesc",
                                        "productDescription"
                                    ],
                                    "default_operator": "AND",
                                    "query": "machine"
                                }
                            }
                        ]
                    }
                }
            ],
            "must": [ ],
            "must_not": [ ]
        }
    },
    "size": 0,
    "aggs": {
        "RelatedKeywords": { //here I want to add filter of index
            "sampler": {
                "shard_size": 20
            },
            "aggregations": {
                "keywords": {
                    "significant_text": {
                        "field": "productDesc",
                        "size": 100,
                        "filter_duplicate_text": true
                    }
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您可以这样做:

{
  "aggs": {
    "index": {
      "filter": {
        "term": {
          "_index": "index-name"
        }
      },
      "aggs": {
        "RelatedKeywords": {
          "sampler": {
            "shard_size": 20
          },
          "aggregations": {
            "keywords": {
              "significant_text": {
                "field": "productDesc",
                "size": 100,
                "filter_duplicate_text": true
              }
            }
          }
        }
      }
    }
  }
}