match_phrase和match_phrase_prefix针对于短语匹配场景
match_phrase是用于精确匹配整个短语的查询,而match_phrase_prefix是在查询末尾添加前缀以匹配短语的一部分。例如,如果短语是“the quick brown fox jumping over the lazy dog”,那么一个match_phrase查询需要输入“brown fox”才能匹配,输入"brown fo则不能匹配",而一个match_phrase_prefix查询可以匹配“quick b”,“quick br”,“quick bro”等前缀。简单来说,match_phrase要求完全匹配,而match_phrase_prefix允许前缀匹配。
match_phrase查询:
GET /job/_search
{
"query": {
"match_phrase": {
"content": "brown fox"
}
}
}
结果:
{
"took" : 13,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 2.7316709,
"hits" : [
{
"_index" : "job",
"_type" : "_doc",
"_id" : "jaOtEYcBL5xiQSCxz8-d",
"_score" : 2.7316709,
"_source" : {
"_class" : "com.bj58.chr.springdataes.use.domain.Job",
"name" : "the quick brown fox jumping over the lazy dog",
"content" : "the quick brown fox jumping over the lazy dog",
"address" : "河南省郑州市金水区",
"suggest" : {
"input" : [
"河南省郑州市金水区"
]
},
"createTime" : "20230324",
"updateTime" : "20230324"
}
}
]
}
}
GET /job/_search
{
"query": {
"match_phrase": {
"content": "brown fo"
}
}
}
结果
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
}
}
match_phrase_prefix查询
GET /job/_search
{
"query": {
"match_phrase_prefix": {
"content": "brown fox"
}
}
}
结果:
{
"took" : 6,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 2.7316709,
"hits" : [
{
"_index" : "job",
"_type" : "_doc",
"_id" : "jaOtEYcBL5xiQSCxz8-d",
"_score" : 2.7316709,
"_source" : {
"_class" : "com.bj58.chr.springdataes.use.domain.Job",
"name" : "the quick brown fox jumping over the lazy dog",
"content" : "the quick brown fox jumping over the lazy dog",
"address" : "河南省郑州市金水区",
"suggest" : {
"input" : [
"河南省郑州市金水区"
]
},
"createTime" : "20230324",
"updateTime" : "20230324"
}
}
]
}
}
GET /job/_search
{
"query": {
"match_phrase_prefix": {
"content": "brown fo"
}
}
}
结果:
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 2.7316709,
"hits" : [
{
"_index" : "job",
"_type" : "_doc",
"_id" : "jaOtEYcBL5xiQSCxz8-d",
"_score" : 2.7316709,
"_source" : {
"_class" : "com.bj58.chr.springdataes.use.domain.Job",
"name" : "the quick brown fox jumping over the lazy dog",
"content" : "the quick brown fox jumping over the lazy dog",
"address" : "河南省郑州市金水区",
"suggest" : {
"input" : [
"河南省郑州市金水区"
]
},
"createTime" : "20230324",
"updateTime" : "20230324"
}
}
]
}
}