带过滤器的elasticsearch嵌套排序

5jdjgkvh  于 2021-06-09  发布在  ElasticSearch
关注(0)|答案(1)|浏览(361)

我有一些文档包含特定键的价格列表,例如
文档1

{
        "name":"doc1",
        "cheapestPrices": [{
                "key": "10000_BB",
                "value": 50
            }, {
                "key": "10000_LO",
                "value": 10
            }, {
                "key": "10000",
                "value": 10
            }, {
                "key": "",
                "value": 10
            }
        ]
    }

文档2

{
    "name":"doc2",
    "cheapestPrices": [{
            "key": "10000_BB",
            "value": 15
        }, {
            "key": "10000_LO",
            "value": 30
        }, {
            "key": "10000",
            "value": 15
        }, {
            "key": "",
            "value": 15
        }
    ]
}

现在我发送一个查询,我想按给定的键排序,顺序应该是从最低到最高。我创建了此查询:

{
    "size": 10000,
    "sort": [
        {
            "cheapestPrices.value": {
                "mode": "min",
                "nested": {
                    "filter": {
                        "bool": {
                            "should": [
                                {
                                    "term": {
                                        "cheapestPrices.key": {
                                            "value": "10000_BB"
                                        }
                                    }
                                }
                            ]
                        }
                    },
                    "path": "cheapestPrices"
                },
                "order": "asc"
            }
        }
    ]
}

希望我先得到doc2(该键的值15),然后得到doc1(该键的值50)。。。但结果是doc1和doc2,排序分数完全相同。
结果:

{
    "took": 10,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": null,
        "hits": [{
                "_index": "test_sortbyprice",
                "_type": "_doc",
                "_id": "doc1",
                "_score": null,
                "_source": {
                    "cheapestPrices": [{
                            "key": "10000_BB",
                            "value": 50
                        }, {
                            "key": "10000_LO",
                            "value": 10
                        }, {
                            "key": "10000",
                            "value": 10
                        }, {
                            "key": "",
                            "value": 10
                        }
                    ],
                    "name": "doc1"
                },
                "sort": [
                    9223372036854775807
                ]
            }, {
                "_index": "test_sortbyprice",
                "_type": "_doc",
                "_id": "doc2",
                "_score": null,
                "_source": {
                    "cheapestPrices": [{
                            "key": "10000_BB",
                            "value": 15
                        }, {
                            "key": "10000_LO",
                            "value": 30
                        }, {
                            "key": "10000",
                            "value": 15
                        }, {
                            "key": "",
                            "value": 15
                        }
                    ],
                    "name": "doc2"
                },
                "sort": [
                    9223372036854775807
                ]
            }
        ]
    }
}

Map如下:

{
    "properties": {
        "cheapestPrices": {
            "type": "nested",
            "properties": {
                "value": {
                    "type": "integer"
                },
                "key": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "ignore_above": 256,
                            "type": "keyword"
                        }
                    }
                }
            }
        },
        "name": {
            "type": "text",
            "fields": {
                "keyword": {
                    "ignore_above": 256,
                    "type": "keyword"
                }
            }
        }
    }
}
0x6upsns

0x6upsns1#

热释光;博士
更改 term 以字段为目标的查询 cheapestPrices.key.keyword 而不是 cheapestPrices.key .
由于使用,排序查询与任何文档都不匹配 term (精确匹配)由于标准分析器而被小写的字段,标准分析器默认应用于 text 现场无额外分析仪。这意味着它永远都不会平等 10000_BB (大写)。但幸运的是你有 .keyword 可确保无值修改。
排序分数是相同的(我假设 9223372036854775807 a、 康涅狄格州。 Long.MAX_VALUE )因为这是默认的es行为。仔细想想,这并不是那么牵强:如果sort查询与任何内容都不匹配,它将分配可能的最高值。
如果你的 orderdesc ,它会回来的 -Long.MAX_VALUE

相关问题