对比
之前我们写了best-fields策略,本文将使用most-fields来搜索,那么两者有什么区别呢?
best-fields策略:主要是说,将某一个field匹配尽可能多的关键词document优先返回回来
most-fields策略:主要是说将更多filed匹配到某个关键词的document优先返回回来
举例
现在有两个document,如下:
document1:1
2
3
4{
"title":"China people",
"content":"i am a good person"
}
document2:1
2
3
4{
"title":"China person",
"content":"i am a good people"
}
一个搜索请求,搜索的关键字是China person,那么来看一下两种搜索策略的返回结果是怎样的
best-fields:会优先将document2返回回来,因为document2的title匹配了两个关键字
most-fields:会优先将document1返回回来,因为document1匹配了两个field
实战案例
先来准备数据,添加一个sub_title字段,手动创建mapping1
2
3
4
5
6
7
8
9
10
11
12
13
14
15POST /forum/_mapping/article
{
"properties": {
"sub_title":{
"type": "string",
"analyzer": "english",
"fields": {
"std":{
"type": "string",
"analyzer": "standard"
}
}
}
}
}
添加数据1
2
3
4
5
6
7
8
9
10
11POST /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"} }
搜索
查询sub_title中包含learning courses的document1
2
3
4
5
6
7
8GET /forum/article/_search
{
"query": {
"match": {
"sub_title": "learning courses"
}
}
}
返回值:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56{
"took": 4,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1.219939,
"hits": [
{
"_index": "forum",
"_type": "article",
"_id": "2",
"_score": 1.219939,
"_source": {
"articleID": "KDKE-B-9947-#kL5",
"userID": 1,
"hidden": false,
"postDate": "2017-01-02",
"tag": [
"java"
],
"tag_cnt": 1,
"view_cnt": 50,
"title": "this is java blog",
"content": "i think java is the best programming language",
"sub_title": "learned a lot of course"
}
},
{
"_index": "forum",
"_type": "article",
"_id": "1",
"_score": 0.5063205,
"_source": {
"articleID": "XHDK-A-1293-#fJ3",
"userID": 1,
"hidden": false,
"postDate": "2017-01-01",
"tag": [
"java",
"hadoop"
],
"tag_cnt": 2,
"view_cnt": 30,
"title": "this is java and elasticsearch blog",
"content": "i like to write best elasticsearch article",
"sub_title": "learning more courses"
}
}
]
}
}
来看一下返回值,这里有个问题,为什么learned a lot of course 排在了 learning more courses 的前面?
在我们手动创建sub_title的mapping映射的时候,使用的是English分词器,所以会还原单词,将单词还原为其最基本的形态(stemmer),比如
learning –> learn
learned –> learn
courses –> course
所以,我们的搜索条件也会变, learning courses –> learn course,这时候去搜索对于这两个sub_title来说就是一样的,所以就会出现learned a lot of course 排在了 learning more courses 的前面这样的情况
most-fields搜索
我们上面在sub_title中还创建了个子field sub_title.std,然后我们用这两个field来进行most-field搜索.
请求1
2
3
4
5
6
7
8
9
10GET /forum/article/_search
{
"query": {
"multi_match": {
"query": "learning courses",
"type": "most_fields",
"fields": ["sub_title","sub_title.std"]
}
}
}
返回值:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1.219939,
"hits": [
{
"_index": "forum",
"_type": "article",
"_id": "2",
"_score": 1.219939,
"_source": {
"articleID": "KDKE-B-9947-#kL5",
"userID": 1,
"hidden": false,
"postDate": "2017-01-02",
"tag": [
"java"
],
"tag_cnt": 1,
"view_cnt": 50,
"title": "this is java blog",
"content": "i think java is the best programming language",
"sub_title": "learned a lot of course"
}
},
{
"_index": "forum",
"_type": "article",
"_id": "1",
"_score": 1.012641,
"_source": {
"articleID": "XHDK-A-1293-#fJ3",
"userID": 1,
"hidden": false,
"postDate": "2017-01-01",
"tag": [
"java",
"hadoop"
],
"tag_cnt": 2,
"view_cnt": 30,
"title": "this is java and elasticsearch blog",
"content": "i like to write best elasticsearch article",
"sub_title": "learning more courses"
}
}
]
}
}
再来看一下返回值,依然是learned a lot of course排在了前面,但是learning more courses的分数有了大幅度的提高,可以对比一下第一个搜索时候的分数
区别和优缺点
best-fields,是对多个field进行搜索,挑选某个filed匹配度最高的那个分数,同时在多个query最高分相同的情况下,在一定程度上考虑其他query的分数. 简单来说就是,对多个filed进行搜索,就想搜索到某一个field尽可能包含更多关键字的数据
优点:通过best_fields策略,以及综合考虑其他field,还有minimum_should_match支持,可以尽可能精准的将匹配的结果推送到最前面
缺点:除了那些精准匹配的结果,其他差不多大的结果,排序结果不是太均匀,没有什么区分度了
most-fields,综合多个field一起进行搜索,尽可能多地让所有的field的query参与到总分数的计算中来,此时就会是个大杂烩,数显类似best_fields案例最开始的那个结果,结果不一定精准,某一个document的一个field包含更多的关键字,但是因为其他document有更多field匹配到了,所以排在前面, 所以就需要建立类似sub_title.std这样的field,尽可能让某一个field精准匹配query string,贡献更高的分数,将更精准匹配的数据排到前面
优点:将尽可能匹配更多的field的结果推送到前面,整个排序的结果都是比较均匀的
缺点:可能那些精准匹配的结果无法排在最前面
实际的例子:wiki,明显的most_fields策略,搜索结果比较均匀,但是的确要翻好几页才能找到最匹配的结果