ES 的查询关键词解释
2021-05-22
1.match
查询语法如下:title是需要查询的字段名,可以被替换成任何字段。query对应的是所需的查询。比如这里会被拆分成‘php’和‘后台’,因为operator是or,所以ES会去所有数据里的title字段查询包含‘后台’和‘php’的,如果operator为and,这查询的是即包含‘后台’又有‘php’的数据,这应该很好理解。
$response = $client->get('localhost:9200/accounts/person/_search', [ 'json' => [ 'query' => [ 'match' => [ 'title' => [ 'query' => '后台php', 'operator' => 'or', ] ] ] ]]);
2.multi_match
如果想在多个字段中查找,那就需要用到multi_match查询,语法如下:
$response = $client->get('localhost:9200/accounts/person/_search', [ 'json' => [ 'query' => [ 'multi_match' => [ 'query' => '张三 php', 'fields' => ['title', 'desc', 'user'] ] ] ]]);
3.query_string
查询语法如下:类似match查询的operator,在这里需要在query中用OR或AND实现。
$response = $client->get('localhost:9200/accounts/person/_search', [ 'json' => [ 'query' => [ 'query_string' => [ 'query' => '(张三) OR (php)', 'default_field' => 'title', ] ] ]]);
多字段查询如下:
$response = $client->get('localhost:9200/accounts/person/_search', [ 'json' => [ 'query' => [ 'query_string' => [ 'query' => '(张三) OR (php)', 'fields' => ['title', 'user'], ] ] ]]);
4.range query
这是范围查询,例如查询年龄在10到20岁之间的。查询语法如下:
$response = $client->get('localhost:9200/accounts/person/_search', [ 'json' => [ 'query' => [ 'range' => [ 'age' => [ 'gte' => 10, 'lte' => 20, ], ] ] ]]);
注:gte表示>=,lte表示<=,gt表示>,lt表示<。
5.bool查询
bool查询的语法都是一样的。如下:
$response = $client->get('localhost:9200/accounts/person/_search', [ 'json' => [ 'query' => [ 'bool' => [ 'must/filter/should/must_not' => [ [ 'query_string' => [ 'query' => '研发', ] ], [ 'range' => [ 'age' => [ 'gt' => 20 ] ] ], ], ] ] ]]);