【ElasticSearch(九)進階】Term精確數值查詢,match keyword精確文字查詢
阿新 • • 發佈:2021-08-24
【ElasticSearch(九)進階】Term精確數值查詢,match keyword精確文字查詢
一、Term精確數值查詢
-
term查詢,會返回那些 在提供的欄位中包含確切資訊 的文件內容。
-
查詢text欄位值,使用match。查詢精確數值,使用term。
-
為什麼避免使用term對text欄位進行查詢?
預設情況下,ES更改text欄位的值作為詞法分析的一部分。這會使查詢text欄位值的精確匹配變得困難。
查詢年齡是33歲的資料:
GET bank/_search { "query":{ "term":{ "age": 33 } } }
返回結果:
{ "took" : 1, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 50, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "bank", "_type" : "account", "_id" : "18", "_score" : 1.0, "_source" : { "account_number" : 18, "balance" : 4180, "firstname" : "Dale", "lastname" : "Adams", "age" : 33, "gender" : "M", "address" : "467 Hutchinson Court", "employer" : "Boink", "email" : "[email protected]", "city" : "Orick", "state" : "MD" } }, 。。。 ] } }
二、match keyword精確文字查詢
下面對比下match
,match_phrase
,match中的屬性加.keyword
的區別
1.match 模糊查詢文字
會將address
的文字拆分成詞,只要結果中包含有任意詞的文件,都可以被篩選出來。
GET bank/_search
{
"query":{
"match":{
"address": "467 Hutchinson Court"
}
}
}
返回結果:
{ "took" : 17, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 120, "relation" : "eq" }, "max_score" : 14.617203, "hits" : [ { "_index" : "bank", "_type" : "account", "_id" : "18", "_score" : 14.617203, "_source" : { "account_number" : 18, "balance" : 4180, "firstname" : "Dale", "lastname" : "Adams", "age" : 33, "gender" : "M", "address" : "467 Hutchinson Court", "employer" : "Boink", "email" : "[email protected]", "city" : "Orick", "state" : "MD" } }, { "_index" : "bank", "_type" : "account", "_id" : "53", "_score" : 5.990829, "_source" : { "account_number" : 53, "balance" : 28101, "firstname" : "Kathryn", "lastname" : "Payne", "age" : 29, "gender" : "F", "address" : "467 Louis Place", "employer" : "Katakana", "email" : "[email protected]", "city" : "Harviell", "state" : "SD" } }, { "_index" : "bank", "_type" : "account", "_id" : "56", "_score" : 2.1248586, "_source" : { "account_number" : 56, "balance" : 14992, "firstname" : "Josie", "lastname" : "Nelson", "age" : 32, "gender" : "M", "address" : "857 Tabor Court", "employer" : "Emtrac", "email" : "[email protected]", "city" : "Sunnyside", "state" : "UT" } }, 。。。 ] } }
2.match_phrase 短語查詢
將address
的文字視為一個短語,不進行文字拆分,只要結果中包含這個短語的文件,都能被篩選出來。
GET bank/_search
{
"query":{
"match_phrase":{
"address": "467 Hutchinson"
}
}
}
返回結果:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 12.492344,
"hits" : [
{
"_index" : "bank",
"_type" : "account",
"_id" : "18",
"_score" : 12.492344,
"_source" : {
"account_number" : 18,
"balance" : 4180,
"firstname" : "Dale",
"lastname" : "Adams",
"age" : 33,
"gender" : "M",
"address" : "467 Hutchinson Court",
"employer" : "Boink",
"email" : "[email protected]",
"city" : "Orick",
"state" : "MD"
}
}
]
}
}
3.match中的屬性加.keyword
keyword精確查詢,只有結果中address
屬性和address.keyword
的值完全一致的文件,才能被篩選出來。
結合2和3,可以發現同樣的值,match_phrase 和 keyword的區別。
GET bank/_search
{
"query":{
"match":{
"address.keyword": "467 Hutchinson"
}
}
}
返回結果:
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
}
}
*我們一般規定:全文檢索欄位用 match,其他非text欄位匹配用term