Elasticsearch中的multi_match与普通match结合使用

huangapple go评论64阅读模式
英文:

Elasticsearch multi_match in combination with normal match

问题

I have the following function which searches for results in elasticsearch.

我有以下函数,用于在Elasticsearch中搜索结果。

I want to do the following request with PHP and Guzzle.

我想使用PHP和Guzzle执行以下请求。

But with this, I get a parsing exception, which means that the multi_match doesn't work this way. If I use a 'normal' match in that place, it's working fine, but then I am limited to 1 field.

但是,使用这种方法会导致解析异常。如果我在那个位置使用'正常'的匹配,它可以正常工作,但是我将受到字段数量的限制。

The other match fields use other form fields for input, and they always have one value.

其他匹配字段使用其他表单字段进行输入,它们始终只有一个值。

But with the form field 'Description search,' I want to look in multiple fields with the AND operator.

但是,对于表单字段'描述搜索',我想使用AND运算符在多个字段中进行搜索。

Anyone knows how to fix this?

有人知道如何修复这个问题吗?

英文:

I have the following function which searches for results in elasticsearch.

I want to do the following request with PHP and 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;
    }
  }

But with this i get a parsing exception which means that the multi_match doesn't work this way. If i use a 'normal' match in that place it's working fine but then i am limited to 1 field.

The other match fields uses other form fields for input and that has always one value.

But with form field 'Description search' i want to look in multiple fields with the AND operator.

Anyone knows how to fix this?

Screenshot of the form:

Elasticsearch中的multi_match与普通match结合使用

答案1

得分: 2

Your query looks good to me and ran in my test. It should work as is.

But in the interest of future readers, this is how 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" 
    }
  }
}

Note that this example from the elastic search documentation is identical in nature to your section of the code above:

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

This is how multi-match queries are formed, and you did it.

英文:

Your query looks good to me and ran in my test. It should work as is.

But in the interest of future readers, this is how 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" 
    }
  }
}

Note that this example from the elastic search documentation is identical in nature to your section of the code above:

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

This is how multi-match queries are formed, and you did it.

huangapple
  • 本文由 发表于 2020年1月3日 23:30:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581212.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定