【ElasticSearch(六)進階】match匹配,match_phrase 短語匹配
一、match進行 基本類型(非字符串)精確匹配
查詢 account_number 是 20 的所有結果:
GET /bank/_search
{
"query": {
"match": {
"account_number": 20
}
}
}
返回內容:
此時max_score為1.0,該條記錄的_score就是1.0,這是精確匹配。
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "bank",
"_type" : "account",
"_id" : "20",
"_score" : 1.0,
"_source" : {
"account_number" : 20,
"balance" : 16418,
"firstname" : "Elinor",
"lastname" : "Ratliff",
"age" : 36,
"gender" : "M",
"address" : "282 Kings Place",
"employer" : "Scentric",
"email" : "elinorratliff@scentric.com",
"city" : "Ribera",
"state" : "WA"
}
}
]
}
}
二、match進行 字符串模糊匹配查詢(全文檢索)
查詢所有 address 中包含 Kings 的數據:
GET bank/_search
{
"query": {
"match": {
"address": "Kings"
}
}
}
返回結果:
可以看到"address" : "282 Kings Place"
和"address" : "305 Kings Hwy"
兩條記錄都返回了
{
"took" : 6,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 5.990829,
"hits" : [
{
"_index" : "bank",
"_type" : "account",
"_id" : "20",
"_score" : 5.990829,
"_source" : {
"account_number" : 20,
"balance" : 16418,
"firstname" : "Elinor",
"lastname" : "Ratliff",
"age" : 36,
"gender" : "M",
"address" : "282 Kings Place",
"employer" : "Scentric",
"email" : "elinorratliff@scentric.com",
"city" : "Ribera",
"state" : "WA"
}
},
{
"_index" : "bank",
"_type" : "account",
"_id" : "722",
"_score" : 5.990829,
"_source" : {
"account_number" : 722,
"balance" : 27256,
"firstname" : "Roberts",
"lastname" : "Beasley",
"age" : 34,
"gender" : "F",
"address" : "305 Kings Hwy",
"employer" : "Quintity",
"email" : "robertsbeasley@quintity.com",
"city" : "Hayden",
"state" : "PA"
}
}
]
}
}
*全文檢索就是利用了 倒排索引表,會按照評分進行排序。
三、match_phrase 短語匹配
默認的match搜索會對搜索內容進行分詞,比如:mill lane 會分成 mill 和 lane 之后搜索的結果可能包含僅有其中一項的結果,但是此類結果分數較低。
如果不希望被分詞而是直接查詢短語,可以使用 match_phrase 進行搜索
例子:
查詢 地址包含 mill lane 的數據:
GET /bank/_search
{
"query": {
"match_phrase": {
"address": "mill lane"
}
}
}
返回結果:
{
"took" : 18,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 9.507477,
"hits" : [
{
"_index" : "bank",
"_type" : "account",
"_id" : "136",
"_score" : 9.507477,
"_source" : {
"account_number" : 136,
"balance" : 45801,
"firstname" : "Winnie",
"lastname" : "Holland",
"age" : 38,
"gender" : "M",
"address" : "198 Mill Lane",
"employer" : "Neteria",
"email" : "winnieholland@neteria.com",
"city" : "Urie",
"state" : "IL"
}
}
]
}
}