

继续跟中华石杉老师学习ES,第十三篇
课程地址: https://www.roncoo.com/view/55
https://www.elastic.co/guide/en/elasticsearch/reference/7.2/query-dsl-multi-match-query.html


上一节 我们演示了best_fileds的用法 白话Elasticsearch12-基于multi_match + bestfields语法实现dis_max+tie_breaker ,这里我们继续来看下 most fields 的用法
POST /forum/_mapping/article
{
"properties": {
"sub_title": {
"type": "text",
"analyzer": "english",
"fields": {
"std": {
"type": "text",
"analyzer": "standard"
}
}
}
}
}增加一个字段sub_title,使用english分词器(会进行stemmer), 然后在这对sub_title增加一个子field ,使用standard分词器(不会进行stemmer).
批量更新数据
POST /forum/article/_bulk
{ "update": { "_id": "1"} }
{ "doc" : {"sub_title" : "learning more courses"} }
{ "update": { "_id": "2"} }
{ "doc" : {"sub_title" : "learned a lot of course"} }
{ "update": { "_id": "3"} }
{ "doc" : {"sub_title" : "we have a lot of fun"} }
{ "update": { "_id": "4"} }
{ "doc" : {"sub_title" : "both of them are good"} }
{ "update": { "_id": "5"} }
{ "doc" : {"sub_title" : "haha, hello world"} }GET /forum/article/_search
{
"query": {
"match": {
"sub_title": "learning courses"
}
}
}sub_title用的是enligsh analyzer,所以还原了单词.
因为如果我们用的是类似于english analyzer这种分词器的话,就会将单词还原为其最基本的形态,stemmer
learning --> learn
learned --> learn
courses --> coursesub_titile: learning coureses 就变成了 learn course
返回结果

GET /forum/article/_search
{
"query": {
"multi_match": {
"query": "learning courses",
"type": "most_fields",
"fields": [
"sub_title",
"sub_title.std"
]
}
}
}结果

best_fields,是对多个field进行搜索,挑选某个field匹配度最高的那个分数,同时在多个query最高分相同的情况下,在一定程度上考虑其他query的分数。简单来说,你对多个field进行搜索,就想搜索到某一个field尽可能包含更多关键字的数据
minimum_should_match支持,可以尽可能精准地将匹配的结果推送到最前面实际的例子:百度之类的搜索引擎,最匹配的到最前面,但是其他的就没什么区分度了
most_fields,综合多个field一起进行搜索,尽可能多地让所有field的query参与到总分数的计算中来,此时就会是个大杂烩,出现类似best_fields案例最开始的那个结果,结果不一定精准,某一个document的一个field包含更多的关键字,但是因为其他document有更多field匹配到了,所以排在了前面;所以需要建立类似sub_title.std这样的field,尽可能让某一个field精准匹配query string,贡献更高的分数,将更精准匹配的数据排到前面
实际的例子:wiki,明显的most_fields策略,搜索结果比较均匀,但是的确要翻好几页才能找到最匹配的结果