【问题标题】:Filter on multi match ES PHP过滤多匹配 ES PHP
【发布时间】:2021-06-06 05:48:39
【问题描述】:

开始在 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]

【问题讨论】:

    标签: php json elasticsearch


    【解决方案1】:

    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);
    }
    

    【讨论】:

    • 感谢您的回答,但您的代码仍然有错误。 {"error":{"root_cause":[{"type":"parsing_exception","re​​ason":"[must] query malformed, no start_object after query name" 再次感谢
    【解决方案2】:

    您需要使用boolean query 组合多个查询

    在 JSON 格式中,您的查询将如下所示

    {
      "query": {
        "bool": {
          "must": {
            "multi_match": {
              "query": "react",
              "fields": [$array[2]]
            }
          },
          "filter": {
            "geo_distance": {
              "distance": "300m",
              "location": [
                "-25, -49"
              ]
            }
          }
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-15
      • 2016-01-08
      • 2023-03-13
      • 1970-01-01
      相关资源
      最近更新 更多