elasticsearch - match、match_phrase和match_phrase_prefix区别
创建索引
PUT /article
{
"settings": {
"number_of_replicas": 2,
"number_of_shards": 2
},
"mappings": {
"properties": {
"context":{
"type": "text"
}
}
}
}
//插入数据
POST /article/_doc/1
{
"content":"hello world"
}
POST /article/_doc/2
{
"content":"hello me world"
}
POST /article/_doc/3
{
"content":"world war"
}
POST /article/_doc/4
{
"content":"hello me other world"
}
POST /article/_doc/5
{
"content":"hello me other world jack"
}
POST /article/_doc/6
{
"content":"hello me other world jacce"
}
match
GET /article/_search
{
"query": {
"match": {
"content": "hello world"
}
}
}
match 会对 content 分词,会查询出包含 hello 或 world 的数据,如:
hello me other world
hello world
hello me world
hello me other world jack
world war
match_phrase
会分词,但有顺序
GET /article/_search
{
"query": {
"match_phrase": {
"content":{
"query":"hello world"
}
}
}
}
查询结果:
hello world
还可以传 slop
属性,用于控制中间可以隔多少个单词
GET /article/_search
{
"query": {
"match_phrase": {
"content":{
"query":"hello world",
"slop":2
}
}
}
}
结果:
hello world
hello me other world
hello me world
hello me other world jack
match_phrase_prefix
这种查询的行为与 match_phrase 查询一致,但是它将查询字符串的最后一个词作为前缀(prefix)使用
GET /article/_search
{
"query": {
"match_phrase_prefix": {
"content":{
"query":"hello world ja",
"slop":2
}
}
}
}
结果:
hello me other world jack
hello me other world jacce