我正在使用Elasticsearch 6和PHP。
我的文档有一个嵌套字段:
"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]]
]
]
]
]
]
]
];
我得到了正确的结果,但文档没有按价格排序。我怎样才能正确排序它们?文件应按与过滤器匹配的一个价格进行分类。
答案 0 :(得分:0)
显然我使用的是旧文档。上述查询的正确排序部分是:
'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]]
]
]
]
]
]
]