Elasticsearch排序嵌套字段。

4

我使用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]]
                        ]
                    ]
                ]
            ]
        ]
    ]
];

我得到了正确的结果,但是文档没有按价格排序。我应该如何正确地对它们进行排序?文档应该按照与筛选器匹配的价格进行排序。
1个回答

3

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

    '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]]
                        ]
                    ]
                ]
            ]
        ]
    ]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接