【ElasticSearch】精確匹配text字段 用match加.keyword 或 term
1.錯誤示范
由於記憶混淆,記成了使用match_phrase
對text字段精確匹配。
#測試match_phrase
GET /test/external/_search
{
"query":{
"bool": {
"must": [
{
"match_phrase": {
"nodealias": "92新增"
}
}
]
}
}
}
結果
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 3.7249355,
"hits" : [
{
"_index" : "test",
"_type" : "external",
"_id" : "EcTGtHsBO1AHMH7HW",
"_score" : 3.7249355,
"_source" : {
"key_id" : "url_http_18536276217",
"nodealias" : "92新增app配置",
"taskFinishTime" : "1630825478282",
"result" : {
"http_request" : {
"responseTime" : 89.0
}
}
}
}
]
}
}
2.使用match
加字段+.keyword
GET /test/external/_search
{
"query":{
"bool": {
"must": [
{
"match": {
"nodealias.keyword": "92新增app配置"
}
}
]
}
}
}
結果
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.9924302,
"hits" : [
{
"_index" : "test",
"_type" : "external",
"_id" : "EcTGtHsBO1AHMH7HW",
"_score" : 1.9924302,
"_source" : {
"key_id" : "url_http_18536276217",
"nodealias" : "92新增app配置",
"taskFinishTime" : "1630825478282",
"result" : {
"http_request" : {
"responseTime" : 89.0
}
}
}
}
]
}
}
3.將字段設為keyword類型后,就可以使用term精確匹配text字段
可以看到key_id
是keyword類型的:
GET /test/external/_search
{
"query":{
"bool": {
"must": [
{
"term": {
"key_id": "url_http_18536276217"
}
}
]
}
}
}
結果
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.9924302,
"hits" : [
{
"_index" : "test",
"_type" : "external",
"_id" : "EcTGtHsBO1AHMH7HW",
"_score" : 1.9924302,
"_source" : {
"key_id" : "url_http_18536276217",
"nodealias" : "92新增app配置",
"taskFinishTime" : "1630825478282",
"result" : {
"http_request" : {
"responseTime" : 89.0
}
}
}
}
]
}
}