Elasticsearch-2个字段的乘法,然后求和聚合

TVC*_*TVC 3 multiplication elasticsearch

我正在尝试在Elasticsearch中进行简单的查询,但是我不知道该怎么做。我在整个互联网上进行了搜索,没有关于这种情况的讨论。

假设我有类似的物品:

{
  "item_id": 1,
  "item_price": 100,
  "item_quantity": 2
},
{
  "item_id": 2,
  "item_price": 200,
  "item_quantity": 3
},
{
  "item_id": 3,
  "item_price": 150,
  "item_quantity": 1
},
{
  "item_id": 4,
  "item_price": 250,
  "item_quantity": 5
}
Run Code Online (Sandbox Code Playgroud)

我想进行查询,以得到股票总价的结果。

例如:100 * 2 + 200 * 3 + 150 * 1 + 250 * 5

该查询的结果应该是2,200


最后一个数据的答案查询正在运行,但是这种复杂的情况呢:

POST tests/test2/
{
  "item_category": "aaa",
  "items": 
  [
    {
      "item_id": 1,
      "item_price": 100,
      "item_quantity": 2
    },
    {
      "item_id": 2,
      "item_price": 150,
      "item_quantity": 4
    }
  ]
}

POST tests/test2/
{
  "item_category": "bbb",
  "items": 
  [
    {
      "item_id": 3,
      "item_price": 200,
      "item_quantity": 3
    },
    {
      "item_id": 4,
      "item_price": 200,
      "item_quantity": 5
    }
  ]
}

POST tests/test2/
{
  "item_category": "ccc",
  "items": 
  [
    {
      "item_id": 5,
      "item_price": 300,
      "item_quantity": 2
    },
    {
      "item_id": 6,
      "item_price": 150,
      "item_quantity": 8
    }
  ]
}

POST tests/test2/
{
  "item_category": "ddd",
  "items": 
  [
    {
      "item_id": 7,
      "item_price": 80,
      "item_quantity": 10
    },
    {
      "item_id": 8,
      "item_price": 250,
      "item_quantity": 4
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,下一个查询不起作用,给我一个错误的答案(1,420而不是6,000):

GET tests/test2/_search
{
  "query": {
    "match_all": { }
  },
    "aggs": {
        "total_price": {
            "sum": {
                "script": {
                    "lang": "painless",
                    "inline": "doc['items.item_price'].value * doc['items.item_quantity'].value"
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Ran*_*dom 6

您可以将sum聚合用于使用以下方法计算的值script

{
    "aggs": {
        "total_price": {
            "sum": {
                "script": {
                    "lang": "painless",
                    "inline": "doc['item_price'].value * doc['item_quantity'].value"
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在此处查看https://www.elastic.co/guide/zh-CN/elasticsearch/reference/current/search-aggregations-metrics-sum-aggregation.html#_script_9了解更多信息

更新资料

至于高级情况,最好将items字段映射为nested类型,然后再使用此聚合

{
    "aggs": {
        "nested": {
            "nested": {
                "path": "items"
            },
            "aggs": {
                "total_price": {
                    "sum": {
                        "script": {
                            "inline": "doc['items.item_price'].value * doc['items.item_quantity'].value"
                        }
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是问题中示例数据库的映射查询:

PUT tests
{
  "mappings": {
    "test2": {
      "properties": {
        "items": {
          "type": "nested" 
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

只是为了澄清一下,您必须在创建索引之前进行映射查询。(不允许更改现有字段的映射)。