首页 > 解决方案 > 过滤多匹配 ES PHP

问题描述

开始在 ES 上工作。我希望能够使用 PHP 中的过滤器进行 multi_match。我遵循了官方 ES 文档,但我不明白我的错误。

这是代码:

public function search_data_into_index($array)
{
    $params = [
        'index' => 'skills',
        'type' => 'people',
        'body' => [
            'query' => [
                'multi_match' => [
                    'query' => 'react',
                    'fields' => [$array[2]],
                    'fuzziness' => 'AUTO',
                ],
                'filter' => [
                    'geo_distance' => [
                        'distance' => '300m',
                        'location' => '-25, -49'
                    ]
                ]
            ]
        ]
    ];
    $response = $this->client->search($params);
    print_r($response);
}

这是我的错误:

{"error":{"root_cause":[{"type":"parsing_exception","reason":"[multi_match] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":94}],"type":"parsing_exception","reason":"[multi_match] malformed query, expected [END_OBJECT] but found [FIELD_NAME]

标签: phpjsonelasticsearch

解决方案


multi_match查询必须位于内部bool/must

public function search_data_into_index($array)
{
    $params = [
        'index' => 'skills',
        'type' => 'people',
        'body' => [
            'query' => [
               'bool' => [
                  'must' => [  
                     'multi_match' => [
                        'query' => 'react',
                        'fields' => [$array[2]],
                        'fuzziness' => 'AUTO',
                     ]
                  ],
                  'filter' => [
                     'geo_distance' => [
                        'distance' => '300m',
                        'location' => '-25, -49'
                     ]
                  ]
               ]
            ]
        ]
    ];
    $response = $this->client->search($params);
    print_r($response);
}

推荐阅读