【发布时间】: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