Elasticsearch在top_hits聚合中排序

时间:2016-12-13 08:07:35

标签: elasticsearch

我有一个消息索引,我也为每个messageHash存储message。我还有更多的领域。索引中有多个重复的message字段,例如“你好”。我想要检索唯一的消息。

以下是我撰写的用于搜索唯一邮件的查询,按日期对其进行排序。我的意思是所有重复日期中包含最新日期的消息是我想要的 被退回。

{
  "query": {
    "bool": {
      "must": {
        "match_phrase": {
          "message": "Hello"
        }
      }
    }
  },
  "sort": [
              {
                "date": {
                  "order": "desc"
                }
              }
  ],
  "aggs": {
    "top_messages": {
      "terms": {
        "field": "messageHash"
      },
      "aggs": {
        "top_messages_hits": {
          "top_hits": {
            "sort": [
              {
                "date": {
                  "order": "desc"
                }
              },
              "_score"
            ],
            "size": 1
          }
        }
      }
    }
  }
}

问题在于它没有按日期排序。它按doc_count排序。我只是在响应中得到排序值,而不是真正的排序结果。怎么了?我现在想知道是否有可能这样做。

编辑:

我尝试将"terms" : { "field" : "messageHash", "order" : { "mydate" : "desc" } } , "aggs" : { "mydate" : { "max" : { "field" : "date" } } }替换为"terms": { "field": "messageHash" },但我得到了:

{
  "error" : {
    "root_cause" : [
      {
        "type" : "parsing_exception",
        "reason" : "Found two sub aggregation definitions under [top_messages]",
        "line" : 1,
        "col" : 412
      }
    ],
    "type" : "parsing_exception",
    "reason" : "Found two sub aggregation definitions under [top_messages]",
    "line" : 1,
    "col" : 412
  },
  "status" : 400
}

0 个答案:

没有答案