Как использовать агрегацию суммы для возврата десятичного значения в elasticsearch - PullRequest
0 голосов
/ 29 мая 2020

это мой запрос для суммирования количества

{
"aggs": {
    "products": {
        "nested": {
            "path": "items"
        },
        "aggs": {
            "product": {
                "terms": {
                    "field": "items.title_keyword.keyword",
                    "size": 10000,
                    "order": {
                        "qty": "desc"
                    }
                },
                "aggs": {
                    "hits": {
                        "top_hits": {
                            "_source": {
                                "includes": [
                                    "items.product_category_name",
                                    "items.sku",
                                    "items.qty",
                                    "items.modifiers.title",
                                    "items.modifiers.gross_modifier",
                                    "items.modifiers.qty_modifier",
                                    "items.modifiers.qty",
                                    "items.modifiers.discount_product_modifier"
                                ]
                            },
                            "size": 1000
                        }
                    },
                    "qty": {
                        "sum": {
                            "field": "items.qty"
                        }
                    },
                    "amount": {
                        "sum": {
                            "field": "items.amount"
                        }
                    },
                    "discount": {
                        "sum": {
                            "field": "items.discount_product"
                        }
                    },
                    "final_amount": {
                        "bucket_script": {
                            "buckets_path": {
                                "amount": "amount",
                                "discount": "discount"
                            },
                            "script": "params.amount - params.discount"
                        }
                    }
                }
            }
        }
    }
}
}

это пример результата:

{
        "key": "Produk Komposit - XXL - Hijau",
        "doc_count": 2,
        "hits": {
          "hits": {
            "total": {
              "value": 2,
              "relation": "eq"
            },
            "max_score": 9.394474,
            "hits": [
              {
                "_index": "sales_transaction",
                "_type": "_doc",
                "_id": "52ab47c0-8dba-11ea-b460-3dd494ae2dbe",
                "_nested": {
                  "field": "items",
                  "offset": 0
                },
                "_score": 9.394474,
                "_source": {
                  "product_category_name": "Produk",
                  "qty": 1.0,
                  "sku": ""
                }
              },
              {
                "_index": "sales_transaction",
                "_type": "_doc",
                "_id": "a4abb680-8dbe-11ea-b55b-1593eff2662b",
                "_nested": {
                  "field": "items",
                  "offset": 0
                },
                "_score": 9.394474,
                "_source": {
                  "product_category_name": "Produk",
                  "qty": 3.06,
                  "sku": ""
                }
              }
            ]
          }
        },
        "amount": {
          "value": 406000.0
        },
        "qty": {
          "value": 4.0
        },
        "discount": {
          "value": 0.0
        },
        "final_amount": {
          "value": 406000.0
        }
      }

как получить агрегирование количества до 4,06, а не 4,0?

...