使用 man ascii 来查看 ASCII 表。

multi_match与match_phrase的区别。

Elasticsearch | 作者 CharlesX | 发布于2018年10月22日 | 阅读数:8136

用一段代码实现搜索sublibs字段为“地下空间”的所有图书。搜索字段为summary字段。我的实现方式有如下两种。
第一种:
client.search({
index: "engineer",
type: "books",
size: 10,
body: {
query: {
bool: {
must: [
{
match_phrase: {
summary: searchContent
}
}
],
filter: [
{
term: {
"sublibs.keyword": "地下空间"
}
}
]
}
},
highlight: {
pre_tags: ["<span>"],
post_tags: ["</span>"],
fields: {
summary: {}
}
}
}
});
第二种:
client
.search({
index: "engineer",
type: "books",
body: {
query: {
bool: {
must: [
{
multi_match: {
query: searchContent,
fields: ["summary"]
}
},
{
multi_match: {
query: "地下空间",
fields: ["sublibs"]
}
}
]
}
},
}
})
 
这两种搜索出来的结果是不一样的, 比较疑惑两种方式的实现哪种是对的呢?
已邀请:

rochy - rochy_he

赞同来自: hapjin

multi_match 是对 boolQuery().should(matchQuery(field, keyword)) 的一种简化,简单说就是一个关键词,匹配多个字段,但是匹配方式是 matchQuery,正常的全文匹配
 
match_phrase 简单说就是要匹配一个短语,例如你输入的文本为:中国人,如果被分词为:中国/人,那么查找时候会在指定的字段先查找到 "中国"这个term,然后在"中国"这个 term 后面去查找 "人"这个term(有顺序要求),如果匹配到则认为匹配成功;所以更像是在匹配一个短语(连贯的句子)。
 
上面的例子对于 matchQuery 而言,只需要查到 "中国"这个term 或者 "人"这个term ,则表示匹配成功,而且对顺序没有要求。
 

laoyang360 - 《一本书讲透Elasticsearch》作者,Elastic认证工程师 [死磕Elasitcsearch]知识星球地址:http://t.cn/RmwM3N9;微信公众号:铭毅天下; 博客:https://elastic.blog.csdn.net

赞同来自:

推荐第一种。
1、multi_match的解析如下:
    "description": "title:上海 title:海 title:自来水 title:自来 title:来 title:水来 title:来自 title:来 title:自 title:海上 title:海 title:上",
                "time": "156.7610030ms",
             
                "children": [
                  {
                    "type": "TermQuery",
                    "description": "title:上海",
                    "time": "0.8837980000ms",
                
                  {
                    "type": "TermQuery",
                    "description": "title:海",
                    "time": "2.232900000ms",
小结:是多个term的bool组合。
 
2、match_phrase解析如下:
 "type": "PhraseQuery",
                "description": """title:"上海 海 自来水 自来 来 水来 来自 来 自 海上 海 上"""",
小结:是一整串拆解的组合。

要回复问题请先登录注册