- boost权重的控制
- 基于dis_max实现best_fields进行搜索
- 基于tie_breaker参数来优化dis_max搜索效果
- 基于multi_match语法来实现dis_max+tie_breaker
- 基于most_fields策略进行搜索
- 使用copy-to去解决cross-fields带来的弊端
测试数据:
POST /forum/article/_bulk{ "update": { "_id": "1"} }{ "doc" : {"title" : "this is java and elasticsearch blog"} }{ "update": { "_id": "2"} }{ "doc" : {"title" : "this is java blog"} }{ "update": { "_id": "3"} }{ "doc" : {"title" : "this is elasticsearch blog"} }{ "update": { "_id": "4"} }{ "doc" : {"title" : "this is java, elasticsearch, hadoop blog"} }{ "update": { "_id": "5"} }{ "doc" : {"title" : "this is spark blog"} }POST /forum/article/_bulk{ "update": { "_id": "1"} }{ "doc" : {"content" : "i like to write best elasticsearch article"} }{ "update": { "_id": "2"} }{ "doc" : {"content" : "i think java is the best programming language"} }{ "update": { "_id": "3"} }{ "doc" : {"content" : "i am only an elasticsearch beginner"} }{ "update": { "_id": "4"} }{ "doc" : {"content" : "elasticsearch and hadoop are all very good solution, i am a beginner"} }{ "update": { "_id": "5"} }{ "doc" : {"content" : "spark is best big data solution based on scala ,an programming language similar to java"} }POST /forum/_mapping/article{ "properties": { "sub_title": { "type": "text", "analyzer": "english", "fields": { "std": { "type": "text", "analyzer": "standard" } } } }}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"} }POST /forum/article/_bulk{ "update": { "_id": "1"} }{ "doc" : {"author_first_name" : "Peter", "author_last_name" : "Smith"} }{ "update": { "_id": "2"} }{ "doc" : {"author_first_name" : "Smith", "author_last_name" : "Williams"} }{ "update": { "_id": "3"} }{ "doc" : {"author_first_name" : "Jack", "author_last_name" : "Ma"} }{ "update": { "_id": "4"} }{ "doc" : {"author_first_name" : "Robbin", "author_last_name" : "Li"} }{ "update": { "_id": "5"} }{ "doc" : {"author_first_name" : "Tonny", "author_last_name" : "Peter Smith"} }
- 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
- 57
- 58
- 59
- 60
- 61
- 62
- 63
1.如果我们想要搜索title中包含blog,同时java、elasticsearch、hadoop、spark只要包含都将搜索出来,但是我们希望拥有spark的评分最高,优先返回。我们可以使用boost,当匹配这个搜索条件计算relevance score时,将会有有更高的score
GET /forum/article/_search{ "query": { "bool": { "must": [ { "match": { "title": "blog" } } ], "should": [ { "match": { "title": { "query": "java" } } }, { "match": { "title": { "query": "hadoop" } } }, { "match": { "title": { "query": "elasticsearch" } } }, { "match": { "title": { "query": "spark", "boost": 5 } } } ] } }}
- 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
2.我们想搜索到title或者content中含有java或者solution的结果
GET /forum/article/_search{ "query": { "bool": { "should": [ { "match": { "title": "java solution" } }, { "match": { "content": "java solution" } } ] } }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
我们想要的结果是_id=5同时在content含有java和solution的doc.但是返回的结果却不是这样。返回doc的score顺序是_id=2 > _id=4 > _id=5,计算每个document的relevance score大致过程是:每个query的分数(每一个query对应每个document,如果满足则会算出一个score,否则没有score)乘以matched query数量,除以总query数量。我们举例来说明doc4和doc5的大致评分过程。(具体的score为一个estimate time)
Doc | field | each query score | matched num | all query num | score |
---|---|---|---|---|---|
doc4 | “title”: “this is java blog”,”content”: “i think java is the best programming language” | 1.1+1.2 | 2 | 2 | (1.1+1.2)*2/2=2.3 |
doc5 | “title”: “this is spark blog”,”content”: “spark is best big data solution based on scala ,an programming language similar to java” | 0+2.3 | 1 | 2 | (0+2.3)*1/2=1.15 |
best fields策略就是让某一个被搜索的field匹配到了尽可能多的关键词作为结果返回,也就是针对多个query的搜索,直接取score最高的那一个query(也就是上述表中的doc=5中的2.3大于doc=4中的1.2和1.1)而不会考虑其他query的相关分数,如果想让搜索更相关,可以考虑下面的第3步来加入更多的score因素:
GET /forum/article/_search{ "query": { "dis_max": { "queries": [ { "match": { "title": "java solution" } }, { "match": { "content": "java solution" } } ] } }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
返回的结果将是我们想要的doc=5中content即包含java也包含solution的document.
3.在上述2的基础上,如果我们有两个docment针对多个query计算出来的最大score是相同的,此时我们只使用dis_max策略是无法更进一步获得更相关的doc,所以我们可以使用tie_breaker参数,来将其他匹配的query乘以一个权重来计算到总的score中。
GET /forum/article/_search{ "query": { "dis_max": { "queries": [ { "match": { "title": "java solution" } }, { "match": { "content": "java solution" } } ], "tie_breaker": 3 } }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
4.我们有如下的搜索,综合上面的参数来优化结果
GET /forum/article/_search{ "query": { "dis_max": { "queries": [ { "match": { "title": { "query": "java beginner", "minimum_should_match": "50%", "boost": 2 } } }, { "match": { "body": { "query": "java beginner", "minimum_should_match": "30%" } } } ], "tie_breaker": 0.3 } } }
- 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
minimun_shoule_match是用来去长尾,比如你想搜索5个关键词,但是很多结果只是匹配1个关键词,这样的结果会和想要的相差甚远,这些结果就是长尾。用minimun_shoule_match可以控制结果的精准度,只有匹配一定数量的关键词的数据才能够返回。
同样上述的搜索可以使用multi_match
GET /forum/article/_search{ "query": { "multi_match": { "query": "java solution", "fields": [ "title^2", "content" ], "type": "best_fields", "tie_breaker": 0.3, "minimum_should_match": "50%" } }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
5.most_fields策略匹配更多的fields的doc将会有更大的score,对于搜索:
GET /forum/article/_search{ "query": { "match": { "sub_title": "learning courses" } }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
我们可以看到结果是如下图:
当我们使用most_fields来查询sub_title和sub_title.std分词和不分词的field的时候可以看出most_fields的作用:
尽管顺序没有变化(因为有其他因素的评分影响),但是匹配更多字段的doc的评分大幅度提升了
6.跨多个fields搜索同一个标识,例如搜索一个人名,可能会去first-name和last-name两个field中进行搜索,此时most_fields或者best_fields将不能够满足我们的最佳需求
对于搜索:GET /forum/article/_search{ "query": { "multi_match": { "query": "Petre Smith", "fields": ["author_first_name","author_last_name"], "type": "most_fields" } }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
返回的结果score最高的是
“author_first_name”: “Smith”,
“author_last_name”: “Williams”
而我们希望得到的结果是
“author_first_name”: “Tonny”,
“author_last_name”: “Peter Smith”
针对多个field一些细微的relevence score算法会影响,多方面的影响是很复杂的,但是我们可以通过结果明确知道我们的结果却是被影响了
我们的解决方法是,将一个标识在跨fields的情况下,让多个field能够合并成一个field就能够解决我们的问题,比如说,一个人名会出现在first_name,last_name,现在能够合并成一个full_name就能够解决我们面临的问题
我们定义mapping来使用copy_to将多个field拷贝到一个field中去,并建立倒排索引供搜索:
PUT /forum/_mapping/article{ "properties": { "new_author_first_name": { "type": "text", "copy_to": "new_author_full_name" }, "new_author_last_name": { "type": "text", "copy_to": "new_author_full_name" }, "new_author_full_name": { "type": "text" } }}POST /forum/article/_bulk{"update":{"_id":"1"}}{"doc":{"new_author_first_name":"Peter","new_author_last_name":"Smith"}}{"update":{"_id":"2"}}{"doc":{"new_author_first_name":"Smith","new_author_last_name":"Williams"}}{"update":{"_id":"3"}}{"doc":{"new_author_first_name":"Jack","new_author_last_name":"Ma"}}{"update":{"_id":"4"}}{"doc":{"new_author_first_name":"Robbin","new_author_last_name":"Li"}}{"update":{"_id":"5"}}{"doc":{"new_author_first_name":"Tonny","new_author_last_name":"Peter Smith"}}
- 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
需要注意的是new_author_full_name是一个隐藏field,直接search并不会显示出来。然后对这个field直接进行搜索即可:
GET /forum/article/_search{ "query": { "match": { "new_author_full_name": "Peter Smith" } } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
总结:
问题1:之前的cross_fields只是尽可能找到多的field匹配的doc,而不是某个field完全匹配的doc 解决:合并成一个field之后是最匹配的doc被先返回 问题2:most_fields无法使用minimum_should_match来去长尾 解决:合并成一个field之后,在搜索的时候就可以进行去长尾 问题3:relevance score被TF/IDF因为多个field所影响而最终不是我们期望的结果 解决:合并之后参与评分的field相对于搜有的doc匹配的数量是相同的,数据相对均匀,不会有极端的偏差使用原生cross_fields来实现我们的需求(推荐):
GET /forum/article/_search{ "query": { "multi_match": { "query": "Peter Smith", "operator":"and", "fields": ["author_first_name","author_last_name"], "type": "cross_fields" } }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
上述搜索条件为:
Peter必须在author_first_name或者author_last_name中出现 author_last_name必须在author_first_name或者author_last_name中出现 针对上述问题1,2的解决可以很好的理解,但是对于问题3,cross_field会取多个query针对每一个field的idf评分最小值,而不会出现极端的情况。(举例:Smith针对author_first_name出现的频率很少最后的idf分数会很小,而author_last_name却很大,最后取小的值会避免极端的情况)