【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" : "daleadams@boink.com",
"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" : "daleadams@boink.com",
"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" : "kathrynpayne@katakana.com",
"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" : "josienelson@emtrac.com",
"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" : "daleadams@boink.com",
"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加.keyword
,其他非text字段匹配用term