ES 的查詢(xún)關(guān)鍵詞解釋
2021-05-22
1.match
查詢(xún)語(yǔ)法如下:title是需要查詢(xún)的字段名,可以被替換成任何字段。query對(duì)應(yīng)的是所需的查詢(xún)。比如這里會(huì)被拆分成‘php’和‘后臺(tái)’,因?yàn)閛perator是or,所以ES會(huì)去所有數(shù)據(jù)里的title字段查詢(xún)包含‘后臺(tái)’和‘php’的,如果operator為and,這查詢(xún)的是即包含‘后臺(tái)’又有‘php’的數(shù)據(jù),這應(yīng)該很好理解。
$response = $client->get('localhost:9200/accounts/person/_search', [ 'json' => [ 'query' => [ 'match' => [ 'title' => [ 'query' => '后臺(tái)php', 'operator' => 'or', ] ] ] ]]);
2.multi_match
如果想在多個(gè)字段中查找,那就需要用到multi_match查詢(xún),語(yǔ)法如下:
$response = $client->get('localhost:9200/accounts/person/_search', [ 'json' => [ 'query' => [ 'multi_match' => [ 'query' => '張三 php', 'fields' => ['title', 'desc', 'user'] ] ] ]]);
3.query_string
查詢(xún)語(yǔ)法如下:類(lèi)似match查詢(xún)的operator,在這里需要在query中用OR或AND實(shí)現(xiàn)。
$response = $client->get('localhost:9200/accounts/person/_search', [ 'json' => [ 'query' => [ 'query_string' => [ 'query' => '(張三) OR (php)', 'default_field' => 'title', ] ] ]]);
多字段查詢(xún)?nèi)缦拢?/strong>
$response = $client->get('localhost:9200/accounts/person/_search', [ 'json' => [ 'query' => [ 'query_string' => [ 'query' => '(張三) OR (php)', 'fields' => ['title', 'user'], ] ] ]]);
4.range query
這是范圍查詢(xún),例如查詢(xún)年齡在10到20歲之間的。查詢(xún)語(yǔ)法如下:
$response = $client->get('localhost:9200/accounts/person/_search', [ 'json' => [ 'query' => [ 'range' => [ 'age' => [ 'gte' => 10, 'lte' => 20, ], ] ] ]]);
注:gte表示>=,lte表示<=,gt表示>,lt表示<。
5.bool查詢(xún)
bool查詢(xún)的語(yǔ)法都是一樣的。如下:
$response = $client->get('localhost:9200/accounts/person/_search', [ 'json' => [ 'query' => [ 'bool' => [ 'must/filter/should/must_not' => [ [ 'query_string' => [ 'query' => '研發(fā)', ] ], [ 'range' => [ 'age' => [ 'gt' => 20 ] ] ], ], ] ] ]]);