【问题标题】:Elasticsearch sort date after aggregation聚合后的 Elasticsearch 排序日期
【发布时间】:2018-08-16 16:16:33
【问题描述】:

我需要找到相关或相似的帖子,然后按日期对它们进行排序。我怎样才能做到这一点? 这是一个查询:

     {
  "fields":[
    "id", "score"
  ],
  "size":0,
  "query":{
    "function_score":{
      "query":{
        "bool":{
          "should":[
            { "match":{ "main_headline.en":{"query":"some text"}}},
            {"match":{"body.en":"some text" }}
          ],
          "must_not":{ "term":{  "id":76484 }}
        }
      },
      "functions":[
        {
          "gauss":{
            "published_at":{ "scale":"140w","decay":0.3
            }
          }
        }]
    }
  },
  "aggregations":{
    "postslug":{
      "terms":{
        "field":"slug",
        "size":9,
        "order":{"max_score":"desc"}
      },
      "aggs":{
        "max_score":{
          "max":{ "script":"_score" }
        },
        "fields":{
          "top_hits":{
            "size":1,
            "_source":{
              "includes":["id", "published_at", "slug"]
            }
          }
        },
        "aggs":{
          "byDate":{
            "max":{"field":"published_at"}
          }
        }
      }
    }
  }
}

我已经尝试通过子聚合来实现 - byDate 但我得到一个错误`在 [aggs] 中找不到聚合器类型 [byDate]。如何修复此错误或如何在聚合后对数据进行排序?

【问题讨论】:

    标签: elasticsearch elasticsearch-2.0 elasticsearch-aggregation


    【解决方案1】:

    您收到的错误表明 Elasticsearch 找不到 byDate 聚合类型,我想您打算将其作为聚合的名称(有关详细信息,请参阅 this section of the documentation)。

    在您提供的示例中,aggsfields 处于同一级别,ES 将其解释为另一个子聚合的名称。可能您想将 aggs 移动到 fields 下。

    既然您使用的是top_hits,那么您可以使用内置的sort 选项。

    希望有帮助!

    【讨论】:

      猜你喜欢
      • 2022-01-26
      • 2014-08-04
      • 2017-12-28
      • 1970-01-01
      • 2015-08-25
      • 1970-01-01
      • 2015-01-23
      • 2016-04-12
      • 2021-02-11
      相关资源
      最近更新 更多