这是我参与8月更文挑战的第12天,活动详情查看:8月更文挑战
本Elasticsearch相关文章的版本为:7.4.2markdown
Elasticsearch的match_phrase_prefix查询是基于match_phrase查询的基础上再添加一个prefix查询组合而成的。 返回包含提供的文本的单词且以相同顺序出现的文档。提供的文本的最后一个分词被视为前缀,匹配以该分词开头的任何单词。app
假设有如下文档:post
POST /match_phrase_prefix_test/_doc/1
{
"message": "my name is ridingroad"
}
POST /match_phrase_prefix_test/_doc/2
{
"message": "my last name is ridingroad"
}
复制代码
如下查询会返回message
字段包含短语my name
且紧跟着是以i
开头的文档:spa
POST /match_phrase_prefix_test/_search
{
"query": {
"match_phrase_prefix": {
"message": "my name i"
}
}
}
复制代码
返回的数据:code
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.5730107,
"hits" : [
{
"_index" : "match_phrase_prefix_test",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.5730107,
"_source" : {
"message" : "my name is ridingroad"
}
}
]
}
}
复制代码
同理,如下查询会返回message
字段包含短语my last
且紧跟着是以n
开头的文档:orm
POST /match_phrase_prefix_test/_search
{
"query": {
"match_phrase_prefix": {
"message": "my last n"
}
}
}
复制代码
返回的数据:排序
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0117995,
"hits" : [
{
"_index" : "match_phrase_prefix_test",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0117995,
"_source" : {
"message" : "my last name is ridingroad"
}
}
]
}
}
复制代码
和match_pharse同样,能够经过slop
参数控制容许调换分词位置的数量,具体可见match_phrase查询。索引
经过添加slop
参数,下面的查询将能够匹配包含my last
短语且紧跟着以n
开头的doc2:ip
POST /match_phrase_prefix_test/_search
{
"query": {
"match_phrase_prefix": {
"message": {
"query": "my name last",
"slop": 2
}
}
}
}
复制代码
返回的数据:文档
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.47492626,
"hits" : [
{
"_index" : "match_phrase_prefix_test",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.47492626,
"_source" : {
"message" : "my last name is ridingroad"
}
}
]
}
}
复制代码
一样,也能够为查询的字段在进行查询前指定analyzer进行分词。默认为使用建立mapping时的查询字段的analyzer,若是建立mapping没有显示指定analyzer,则将使用索引级别的默认analyzer。
注意: 假设你查询my last n
,最后的一个分词n
,将会进行查询已n
开头的分词,可是有可能你想要的那个以n
开头的单词并不在你的查询结果里面。由于Elasticsearch会去以n
开头的已排序的分词里面取前50个放到查询中,加入你想查的是my last nzz
, 那么颇有可能nzz不在前50个分词里面。