ElasticSearch 比對查詢(match、match_phrase)
match查詢屬于全文查詢,在查詢時,ES會先分析查詢字元串,然後根據分詞建構查詢。
match_phrase在查詢時也會先分析查詢字元串,然後對這些詞項進行搜尋,不同的是match_phrase查詢隻會保留包含全部查詢字元串的文檔
1、先向ES查詢倆個文檔,以便測試:
PUT test2/_doc/1
{
"name": "寶馬法拉利蘭博基尼"
}
PUT test2/_doc/2
{
"name": "寶馬蘭博基尼布加迪威龍"
}
2、使用match查詢
GET test2/_search
{
"query": {
"match": {
"name": "寶馬法拉利"
}
}
}
查詢結果為全部文檔:
"hits" : {
"total" : 2,
"max_score" : 1.4384104,
"hits" : [
{
"_index" : "test2",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.4384104,
"_source" : {
"name" : "寶馬法拉利蘭博基尼"
}
},
{
"_index" : "test2",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.5753642,
"_source" : {
"name" : "寶馬蘭博基尼布加迪威龍"
}
}
]
}
3、使用match_phrase查詢
GET test2/_search
{
"query": {
"match_phrase": {
"name": "寶馬法拉利"
}
}
}
查詢結果隻保留了包含“寶馬法拉利”的文檔,雖然第二個文檔也有這倆詞,但不是連着的
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.4384104,
"hits" : [
{
"_index" : "test2",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.4384104,
"_source" : {
"name" : "寶馬法拉利蘭博基尼"
}
}
]
}
}