【问题标题】:filterby, groupby unique field value, sum aggregation, orderby chain of queries in elasticsearchfilterby、groupby 唯一字段值、sum 聚合、elasticsearch 中的 orderby 查询链
【发布时间】:2015-11-25 13:08:46
【问题描述】:

我正在尝试向 elasticsearch 发出查询,该查询确实过滤、分组、总和聚合和排序。我有 2 个问题:查询应该是什么样子以及对弹性搜索的性能影响是什么?

让我举一个示例数据集来支持我的问题。假设我有一组销售额:

document type: 'sales' with the following fields and data:
sale_datetime    | sold_product  | sold_at_price
-----------------|---------------|--------------
2015-11-24 12:00 | some product  | 100
2015-11-24 12:30 | some product  | 100
2015-11-24 12:30 | other product | 100
2015-11-24 13:00 | other product | 100
2015-11-24 12:30 | some product  | 200
2015-11-24 13:00 | some product  | 200

我想发出一个查询:

  • 仅考虑 2015 年 11 月 24 日 12:15 到 2015 年 11 月 24 日 12:45 时间间隔内的销售额
  • 按 sold_product 字段对结果进行分组
  • 计算“每个产品的已售价格总和”
  • 按“每个产品的已售价格总和”最大的顺序返回行,其次是第二个,依此类推。

将其应用于上面的示例数据集,它将返回以下结果:

sold_product  | sum of sold_at_price
--------------|--------------
some product  | 300     // takes into account rows 2 and 5
other product | 100     // takes into account row 3

如果可以发出这样的查询,对弹性搜索有哪些重要的性能影响?如果需要考虑:

  • 有许多(数十万,未来可能是数百万)独特的产品
  • 产品名称可能包含多个(几十个)单词/术语(可以生成仅包含 1 个单词的唯一产品名称,但它几乎会使数据量翻倍)
  • 通常有很多(百万)条记录满足时间范围过滤器(在某些情况下过滤器可以缩小到一个时间范围内的几万条记录,但不能保证)

提前感谢您的帮助!

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    这是aggregations 的典型用例。让我们从创建索引和建模数据映射开始。我们有一个普通的date field for sold_datetime,另一个numeric field for sold_at_price 和一个multi-field of type string for sold_product。您会注意到这个多字段有一个名为raw 的子字段,即not_analyzed,将用于在产品名称上创建聚合:

    curl -XPUT localhost:9200/sales -d '{
      "mappings": {
        "sale": {
          "properties": {
            "sale_datetime": {
              "type": "date"
            },
            "sold_product": {
              "type": "string",
              "fields": {
                "raw": {
                  "type": "string",
                  "index": "not_analyzed"
                }
              }
            },
            "sold_at_price": {
              "type": "double"
            }
          }
        }
      }
    }'
    

    现在让我们使用新索引的_bulk 端点为您的样本数据集建立索引:

    curl -XPOST localhost:9200/sales/sale/_bulk -d '
    {"index": {}}
    {"sold_datetime": "2015-11-24T12:00:00.000Z", "sold_product":"some product", "sold_at_price": 100}
    {"index": {}}
    {"sold_datetime": "2015-11-24T12:30:00.000Z", "sold_product":"some product", "sold_at_price": 100}
    {"index": {}}
    {"sold_datetime": "2015-11-24T12:30:00.000Z", "sold_product":"other product", "sold_at_price": 100}
    {"index": {}}
    {"sold_datetime": "2015-11-24T13:00:00.000Z", "sold_product":"other product", "sold_at_price": 100}
    {"index": {}}
    {"sold_datetime": "2015-11-24T12:30:00.000Z", "sold_product":"some product", "sold_at_price": 200}
    {"index": {}}
    {"sold_datetime": "2015-11-24T13:00:00.000Z", "sold_product":"some product", "sold_at_price": 200}
    '
    

    最后,让我们创建您需要的查询和聚合:

    curl -XPOST localhost:9200/sales/sale/_search -d '{
      "size": 0,
      "query": {
        "filtered": {
          "filter": {
            "range": {
              "sold_datetime": {
                "gt": "2015-11-24T12:15:00.000Z",
                "lt": "2015-11-24T12:45:00.000Z"
              }
            }
          }
        }
      },
      "aggs": {
        "sold_products": {
          "terms": {
            "field": "sold_product.raw",
            "order": {
              "total": "desc"
            }
          },
          "aggs": {
            "total": {
              "sum": {
                "field": "sold_at_price"
              }
            }
          }
        }
      }
    }'
    

    如您所见,我们正在过滤 sold_datetime 字段的特定日期间隔(11 月 24 日 12:15-12:45)。聚合部分在sold_product.raw 字段上定义了一个terms aggregation,对于每个桶,我们sum 定义了sold_at_price 字段的值。

    请注意,如果您有数百万个可能匹配的文档,为了使其高效,您需要首先应用最激进的过滤器,可能是您正在运行查询的企业的 ID,或其他一些在运行聚合之前排除尽可能多的文档的标准。

    结果如下:

    {
      ...
      "aggregations" : {
        "sold_products" : {
          "doc_count_error_upper_bound" : 0,
          "sum_other_doc_count" : 0,
          "buckets" : [ {
            "key" : "some product",
            "doc_count" : 2,
            "total" : {
              "value" : 300.0
            }
          }, {
            "key" : "other product",
            "doc_count" : 1,
            "total" : {
              "value" : 100.0
            }
          } ]
        }
      }
    }
    

    【讨论】:

    • 谢谢!这就是我所需要的。我会考虑如何应用更多过滤器来减少要处理的记录总数。
    猜你喜欢
    • 2015-12-11
    • 1970-01-01
    • 1970-01-01
    • 2013-01-06
    • 2020-12-21
    • 2018-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多