【问题标题】:Elasticsearch multi_match in combination with normal matchElasticsearch multi_match 结合普通匹配
【发布时间】:2020-01-03 15:30:08
【问题描述】:

我有以下在 elasticsearch 中搜索结果的函数。

我想用 PHP 和 Guzzle 做以下请求。

  /**
   * {@inheritdoc}
   */
  public function sendSearchRequest($es_node, $request) {
    try {
      if (isset($es_node)) {
        $ssl = $es_node->get('field_ess_verify_ssl')->value;
        $ssl_val = $ssl ? 'https://' : 'http://';

        $body = [
          'json' => [
            'query' => [
              'bool' => [
                'should' => [
                  [
                    'multi_match' => [
                      'query' => $request->get('search'),
                      'fields' => ['message', 'event.type'],
                      'operator' => 'AND',
                    ],
                  ],
                  [
                    'match' => [
                      'event.type' => $request->get('type'),
                    ],
                  ],
                  [
                    'match' => [
                      'event.labels' => $request->get('label'),
                    ],
                  ],
                ],
              ],
            ],
          ],
        ];

        $response = $this->httpClient->request('POST', $ssl_val . $es_node->get('field_ess_host')->value . ':' . $es_node->get('field_ess_port')->value . '/***/***/_search?pretty', $body)
          ->getBody()
          ->getContents();

        return json_decode($response, TRUE)['hits']['hits'] ?? '';
      }
    } catch (Exception $exception) {
      \Drupal::logger(__METHOD__)->error('No ES node has been found.');
      return FALSE;
    }
  }

但是这样我得到一个解析异常,这意味着 multi_match 不能以这种方式工作。如果我在那个地方使用“正常”匹配,它工作正常,但我仅限于 1 个字段。

其他匹配字段使用其他表单字段进行输入,并且始终具有一个值。

但是对于表单字段“描述搜索”,我想使用 AND 运算符查看多个字段。

有人知道如何解决这个问题吗?

表格截图:

【问题讨论】:

  • 尝试在 multi_match 中使用双引号。 "multi_match" => [ "query" => $request->get('search'), "fields" => ["message", "event.type"], "operator" => "AND", ]
  • 不幸的是,这也不起作用。
  • 我测试了您的查询,它似乎确实有效。你能附加确切的解析错误吗?还有你目前使用的是什么 Elasticsearch 版本?
  • 嗨帕特,犯了一个愚蠢的错误。查询确实有效。感谢测试

标签: php elasticsearch


【解决方案1】:

您的查询对我来说看起来不错,并在我的测试中运行。它应该按原样工作。

但为了未来读者的利益,multi-match queries work with elastic search 是这样的。

GET /_search
{
  "query": {
    "multi_match" : {
      "query":      "Will Smith",
      "type":       "best_fields",
      "fields":     [ "first_name", "last_name" ],
      "operator":   "and" 
    }
  }
}

请注意,弹性搜索文档中的此示例在本质上与您上面的代码部分相同:

...
                    'multi_match' => [
                      'query' => $request->get('search'),
                      'fields' => ['message', 'event.type'],
                      'operator' => 'AND',
                    ],
...

这就是多匹配查询的形成方式,而您做到了。

【讨论】:

    猜你喜欢
    • 2018-10-19
    • 2013-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-02
    • 1970-01-01
    相关资源
    最近更新 更多