官方文档:https://www.elastic.co/guide/cn/elasticsearch/php/current/_search_operations.html
1、一个简单的匹配语法(match):
match匹配,如果检索内容是:
满足字段httpRefer为"aaa/bbb"的内容
类似于"aaa/bbb",则会由于默认的mapping会进行分词,会匹配到包含"aaa" 或者包含“bbb”的内容,不能完全匹配
$query['bool']['must'][]['match'] = array(
'httpRefer' => "aaa/bbb"
);
// 官方示例;
$params = [
'index' => 'my_index',
'type' => 'my_type',
'body' => [
'query' => [
'match' => [
'testField' => 'abc'
]
]
]
];
$results = $client->search($params);
2、解决上述问题进行严格匹配(match_phrase)
$params = [
'index' => 'my_index',
'type' => 'my_type',
'query' => [
'match_phrase' => [
'testField' => 'abc'
]
]
];
$results = $client->search($params);
3、完全匹配(match_phrase)有个缺陷,不能使用should进行多字段匹配(即同时满足多个条件)
一旦使用了should,匹配结果则不再精确,仍然是有个分数级别控制的结果
如下方代码,匹配的结果就不是完全满足条件的
foreach ($uris as $uri) {
$query['bool']['should'][]['match_phrase'] = array(
'httpRefer' => $uri
);
}
4、过滤语法(filter)
对字段logTime进行过滤,gte是greaterthanequal 大于等于;lte小于等于;
$query['bool']['filter']['range']['logTime'] = array(
'gte' => $filters['startTime'],
'lte' => $filters['endTime']
);
5、对返回结果判断字段是否存在进行筛选(exists)
意思是:字段为uname必须存在
$query['bool']['must'][]['exists'] = array(
"field" => "uname"
);
6、其他检索语法见es官网,均可用
组合查询:must、must_not、should
多字符串查询:should match
多个字段匹配同一个值:multi_match
短语精确匹配:match_phrase
可调整精确程度:slop、minimum_should_match