Elasticsearch术语聚合重复项

时间:2015-08-22 16:02:41

标签: elasticsearch

我有一个使用ngram分析器的字段,并尝试在字段上使用术语聚合来返回字段中的唯一文档。聚合中返回的密钥与返回的文档字段不匹配,并且我获得了重复的字段。

"analysis" : {
   "filter" : {
     "autocomplete_filter" : {
        "type" : "edge_ngram",
         "min_gram" : "1",
         "max_gram" : "20"
      }
    },
    "analyzer" : {
      "autocomplete" : {
        "type" : "custom",
        "filter" : [ "lowercase", "autocomplete_filter" ],
        "tokenizer" : "standard"
      }
    }
  }
}

"name" : {
  "type" : "string",
  "analyzer" : "autocomplete",
  "fields" : {
    "raw" : {
      "type" : "string",
      "index" : "not_analyzed"
    }
  }
}

{
  "query": {
    "query_string": {
      "query":"bra",
      "fields":["name"],
      "use_dis_max":true
     }
  },
  "aggs": {
    "group_by_name": {
      "terms": { "field":"name.raw" }
    }
  }
}

我找回了以下名字和钥匙。

Braingeyser, Brainstorm, Braingeyser, Brainstorm, Brainstorm, Brainstorm, Bramblecrush, Brainwash, Brainwash, Braingeyser

{"key":"Bog Wraith","doc_count":18}
{"key":"Birds of Paradise","doc_count":15}
{"key":"Circle of Protection: Black","doc_count":15}
{"key":"Lightning Bolt","doc_count":15}
{"key":"Grizzly Bears","doc_count":14}
{"key":"Black Knight","doc_count":13}
{"key":"Bad Moon","doc_count":12}
{"key":"Boomerang","doc_count":12}
{"key":"Wall of Bone","doc_count":12}
{"key":"Balance","doc_count":11}

如何让elasticsearch只返回聚合中的唯一字段?

1 个答案:

答案 0 :(得分:1)

要删除聚合中返回的重复项,您可以尝试:

"aggs": {
  "group_by_name": {
    "terms": { "field":"name.raw" },
    "aggs": {
      "remove_dups": {
        "top_hits": {
          "size": 1,
          "_source": false
        }
      }
    }
  }
}