Elasticsearch-6:在多层嵌套对象上排序

时间:2019-06-07 09:03:05

标签: sorting elasticsearch nested multi-level elasticsearch-6

在Elastic search 6.5中,我的数据带有这样的嵌套对象

    {  
        "_routing" : "City1",
        "_source" : {
        "city_name" : "City1",
        "brands" : [
            {
                "brand_code" : "BRCD01",
                "brand_name" : "Brand 1",
                "products_groups" : [
                    {
                        "group_id" : "001",
                        "products" : [
                            {
                                "pid" : "20",
                                "title" : "Product 1",
                                "mrp" : 100,
                                "sp" : 90,
                                "display_order" : 1
                            },
                            {
                                "pid" : "21",
                                "title" : "Product 2",
                                "mrp" : 100,
                                "sp" : 90,
                                "display_order" : 2
                            }
                        ]
                    },
                    {
                        "group_id" : "002",
                        "products" : [
                            {
                                "pid" : "22",
                                "title" : "Product 3",
                                "mrp" : 150,
                                "sp" : 130,
                                "display_order" : 3
                            },
                            {
                                "pid" : "23",
                                "title" : "Product 4",
                                "mrp" : 50,
                                "sp" : 20,
                                "display_order" : 4
                            }
                        ]
                    }
                ]
            },
            {
                "brand_code" : "BRCD02",
                "brand_name" : "Brand 2",
                "products_groups" : [
                    {
                        "group_id" : "003",
                        "products" : [
                            {
                                "pid" : "24",
                                "title" : "Product 5",
                                "mrp" : 100,
                                "sp" : 90,
                                "display_order" : 1
                            },
                            {
                                "pid" : "25",
                                "title" : "Product 6",
                                "mrp" : 100,
                                "sp" : 90,
                                "display_order" : 2
                            }
                        ]
                    },
                    {
                        "group_id" : "004",
                        "products" : [
                            {
                                "pid" : "26",
                                "title" : "Product 7",
                                "mrp" : 150,
                                "sp" : 130,
                                "display_order" : 3
                            },
                            {
                                "pid" : "27",
                                "title" : "Product 8",
                                "mrp" : 50,
                                "sp" : 20,
                                "display_order" : 4
                            }
                        ]
                    }
                ]
            }
        ]
    }
}

我想在嵌套对象产品(brands.products_groups.products)内的display_order字段上使用brand_code条件和路由进行排序。

我已经尝试过像这样的嵌套查询

    {
        "_source": false,
        "query" : {
            "bool" : {
            "must": [
                {
                    "term": {
                    "_routing" : "City1"
                    }
                },
                {
                    "nested": {
                        "path": "brands",
                        "inner_hits": {},
                        "query": {
                            "bool" : {
                                "must": [
                                    {
                                        "term" : {
                                                        brands.brand_code.keyword": "BRCD01"
                                        }                                   
                                                },                                  
                                                {
                                                    "nested": {                                 
                                                        "path": "brands.products_groups",                                       
                                                        "inner_hits": {                                       
                                                            "name": "brands_products_groups"                                        
                                                        },                      
                                                        "query": {
                                                "match_all": {}
                                            }                                 
                                                    }                                   
                                                }                               
                                            ]                           
                                        }
                            }
                        }
                        }
                    ]
                }
            },
                "sort" : [
                    {
                        "brands.products_groups.products.display_order": {
                            "order": "desc",
                            "mode" : "max",
                            "nested_path": "brands.products_groups.products"
                        }
                    }
                ]
            }

排序应适用于inner_hits品牌_产品_组。但这不是基于排序查询的排序。

有什么想法我们可以基于多层嵌套对象字段进行排序吗?

0 个答案:

没有答案