gpt4 book ai didi

sorting - Elasticsearch 排序嵌套字段

转载 作者:行者123 更新时间:2023-12-02 22:22:28 27 4
gpt4 key购买 nike

我在 PHP 中使用 Elasticsearch 6。

我的文档有一个这样的嵌套字段:

    "prices" : {
"type" : "nested",
"properties" : {
"date" : {
"type" : "date"
},
"duration" : {
"type" : "short"
},
"persons" : {
"type" : "short"
},
"pets" : {
"type" : "short"
},
"price" : {
"type" : "float"
}
}

基本上每个文档都有很多价格,但我知道每个文档只有一个价格与过滤器/查询匹配。

我用它来搜索和排序,改编自这里的教程: https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-sorting.html (对不起,PHP 数组格式):
$params = [
'index' => 'my_index',
'type' => 'my_type',
'body' => [
'query' => [
'nested' => [
'path' => 'prices',
'query' => [
'bool' => [
'must' => [
['match' => ['prices.duration' => 14]],
['match' => ['prices.date' => '2018-09-01']],
['match' => ['prices.pets' => 2]],
['match' => ['prices.persons' => 2,]]
]
]
]
]
],
'sort' => [
'prices.price' => [
'order' => 'asc',
'mode' => 'min',
'nested_filter' => [
'bool' => [
'must' => [
['match' => ['prices.duration' => 14]],
['match' => ['prices.date' => '2018-09-01']],
['match' => ['prices.pets' => 2]],
['match' => ['prices.persons' => 2]]
]
]
]
]
]
]
];

我得到了正确的结果,但文件没有按价格排序。我怎样才能正确地对它们进行排序?文件应按与过滤器匹配的一个价格排序。

最佳答案

显然我使用的是旧文档。上述查询的正确排序部分是:

    'sort' => [
'prices.price' => [
'order' => 'asc',
'mode' => 'avg',
'nested' => [
'path' => 'prices',
'filter' => [
'bool' => [
'must' => [
['match' => ['prices.duration' => 14]],
['match' => ['prices.date' => '2018-09-01']],
['match' => ['prices.pets' => 2]],
['match' => ['prices.persons' => 2]]
]
]
]
]
]
]

关于sorting - Elasticsearch 排序嵌套字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49236342/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com