2

我正在使用 Elasticsearch(2.3 版)在具有多种文档类型的索引中进行搜索。我想将结果数限制为每种文档类型的前 X 个搜索命中。怎样才能做到这一点?

查询非常简单明了:

{
  "sort": [
    {
      "_type": {
        "order": "asc"
      }
    }
  ],
  "query": {
    "query_string": {
      "query": "[some search query here]"
    }
  }
}

我在https://stackoverflow.com/a/27720049/467650找到了答案,但似乎自 1.4.0 版以来语法可能已经改变,因为我只收到以下错误消息:

"reason": {
    "type": "search_parse_exception",
    "reason": "Found two aggregation type definitions in [types]: [terms] and [hits]",
    "line": 1,
    "col": 44
}
4

1 回答 1

2

我会termstype元字段上进行聚合,然后使用top_hits子聚合,如下所示:

{
  "size": 0,
  "aggs": {
    "types": {
      "terms": {
        "field": "_type"
      },
      "aggs": {
        "topten": {
          "top_hits": {
            "size": 10
          }
        }
      }
    }
  }
}
于 2016-10-14T10:15:37.003 回答