如何创建将包含数组中最新值的动态字段?

时间:2018-09-27 14:55:34

标签: elasticsearch

我有类似的对象

"hits": {
    "total": 1,
    "max_score": 1.0,
    "hits": [
        {
            "_index": "products",
            "_type": "product",
            "_id": "1",
            "_score": 1.0,
            "_source": {
                "name": "Test product",
                "prices": [
                    {
                        "price": 2.36,
                        "timestamp": "2018-09-27T10:56:45.916945"
                    },
                    {
                        "price": 4.26,
                        "timestamp": "2018-09-24T10:56:45.916945"
                    },
                    {
                        "price": 1.66,
                        "timestamp": "2018-09-26T10:56:45.916945"
                    }
                ],
                "category": "Category_1"
                //...
            }
        }

我通过简单查询得到的信息:

{
    "query" : {
        "term" : { 
            "name" : "product" 
            }
        }
}

我想扩展查询以获取名为latest_pricelatest_price_timestamp的新动态字段,这些字段将包含prices数组中总是最新条目的值。例如:

"hits": {
    "total": 1,
    "max_score": 1.0,
    "hits": [
        {
            "_index": "products",
            "_type": "product",
            "_id": "1",
            "_score": 1.0,
            "_source": {
                "name": "Test product",
                "latest_price": 2.36",
                "latest_price_timestamp": "2018-09-27T10:56:45.916945",
                "prices": [
                    {
                        "price": 2.36,
                        "timestamp": "2018-09-27T10:56:45.916945"
                    },
                    {
                        "price": 4.26,
                        "timestamp": "2018-09-24T10:56:45.916945"
                    },
                    {
                        "price": 1.66,
                        "timestamp": "2018-09-26T10:56:45.916945"
                    }
                ],
                "category": "Category_1"
                //...
            }
        }

0 个答案:

没有答案