【问题标题】:Alternative for sorting results based on script_metric aggregation in elasticsearch在 elasticsearch 中基于 script_metric 聚合对结果进行排序的替代方法
【发布时间】:2016-10-07 21:47:56
【问题描述】:

我想根据script_metric 聚合的结果对我的搜索结果进行排序。但我总是得到错误的回应。 我搜索了一下,发现这是弹性搜索实现的一个限制。不可能基于 script_metric 聚合字段进行排序。 另外,我必须对结果进行分页,因为可能会有很多结果。 所以我无法检索所有结果,然后在代码中对结果进行排序。

我想知道这种情况是否有任何好的选择。 这是我在下面的查询

{
  "query": {
    "range": {
      "creationTimestamp": {
        "gte": 1475613000,
        "lte": 1475699400
      }
    }
  },
  "size": 0,
  "aggs": {
    "messages_count": {
      "terms": {
        "field": "sourceId",
        "order": {
          "totalViews": "desc"
        },
        "size": 10
      },
      "aggs": {
        "totalViews": {
          "scripted_metric": {
            "init_script": "_agg['maximum'] = []",
            "map_script": "max = _source.histories[_source.histories.values.size()-1].views; _agg.maximum.add(max);",
            "combine_script": "sum = 0; for (m in _agg.maximum) { sum += m }; return sum;",
            "reduce_script": "sum = 0; for (a in _aggs) { sum += a }; return sum;"
          }
        }
      }
    }
  }
}

这里是弹性搜索对我的查询的响应:

{
    "error": {
        "root_cause": [
            {
                "type": "aggregation_execution_exception",
                "reason": "Invalid terms aggregation order path [totalViews]. Terms buckets can only be sorted on a sub-aggregator path that is built out of zero or more single-bucket aggregations within the path and a final single-bucket or a metrics aggregation at the path end."
            }
        ],
        "type": "search_phase_execution_exception",
        "reason": "all shards failed",
        "phase": "query",
        "grouped": true,
        "failed_shards": [
            {
                "shard": 0,
                "index": "datacollection",
                "node": "PS0_HLzxRk-jO_C-_x8ivw",
                "reason": {
                    "type": "aggregation_execution_exception",
                    "reason": "Invalid terms aggregation order path [totalViews]. Terms buckets can only be sorted on a sub-aggregator path that is built out of zero or more single-bucket aggregations within the path and a final single-bucket or a metrics aggregation at the path end."
                }
            }
        ]
    },
    "status": 500
}

【问题讨论】:

    标签: sorting elasticsearch


    【解决方案1】:

    我自己找到了解决方案。我阅读了这个 elasticsearch 文档 page 并找到了 script_metric 聚合的替代方法。所以改变内部聚合,一切都很好,工作得很好。

    {
      "query": {
        "range": {
          "creationTimestamp": {
            "gte": 1475613000,
            "lte": 1475699400
          }
        }
      },
      "size": 0,
      "aggs": {
        "messages_count": {
          "terms": {
            "field": "sourceId",
            "order": {
              "totalViews": "desc"
            },
            "size" : 10
          },
          "aggs": {
            "totalViews": {
              "sum": {<---- using sum aggregation instead of script_metric, and write my previous script just like this
                "script": "_source.histories[_source.histories.values.size()-1].views"
              }
            }
          }
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-07-12
      • 2019-04-02
      • 1970-01-01
      • 2014-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-22
      • 2016-04-02
      相关资源
      最近更新 更多