【问题标题】:Elastic DSL: Aggregate and order by keys as numeric弹性 DSL:按数字键聚合和排序
【发布时间】:2015-05-22 06:08:28
【问题描述】:

在我的数据集中,持续时间字段是字符串,我试图通过以下方式查找持续时间的聚合计数:

"aggs": {
    "duration": {
      "terms": {
            "script": "Math.ceil(doc[\"duration\"].value as double)",
            "order" : { "_term" : "asc"}
          }
    }
  }

这将返回结果为:

"buckets" : [ {
        "key" : "1.0",
        "doc_count" : 4561
      }, {
        "key" : "10.0",
        "doc_count" : 117
      }, {
        "key" : "2.0",
        "doc_count" : 6004
      } ]

问题:我想根据按键的数值排序。基于documentation我找不到办法。

【问题讨论】:

    标签: querydsl elasticsearch


    【解决方案1】:

    我看到了三种可能性:

    A.将您的 durationfield 索引为数字类型,即 integerdouble 或任何对您的值有意义的内容

    B.如果 duration 没有太多不同的值(并且如果您的持续时间是自然数),您还可以使用 range aggregation 指定所有范围(1-2、2-3、3-4、4-5等)

    {
      "query": {
        "match_all": {}
      },
      "aggs": {
        "durations": {
          "range": {
            "script": "Math.ceil(doc.duration.value as double)",
            "ranges": [
              {
                "to": 1
              },
              {
                "from": 1,
                "to": 2
              },
              {
                "from": 2,
                "to": 3
              },
              ...
            ]
          }
        }
      }
    }
    

    C.使用 avg metric 子聚合来推断 duration 的数值并使用该值对顶部聚合进行排序。

    {
      "size": 0,
      "query": {
        "match_all": {}
      },
      "aggs": {
        "duration": {
          "terms": {
            "script": "Math.ceil(doc[\"duration\"].value as double)",
            "order": {
              "avgduration": "asc"
            }
          },
          "aggs": {
            "avgduration": {
              "avg": {
                "script": "doc.duration.value as double"
              }
            }
          }
        }
      }
    }
    

    【讨论】:

    • 为了他人的利益,您能说出您选择了哪个选项吗?谢谢!
    • 我现在无法更改我的映射。我需要密钥中的绝对值。 Soi 为此问题选择了应用程序级排序。
    猜你喜欢
    • 2016-06-04
    • 2015-12-30
    • 2017-12-31
    • 2020-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-15
    • 2017-02-03
    相关资源
    最近更新 更多