ElasticSearch教程——best fields,most fields策略
ElasticSearch彙總請檢視:ElasticSearch教程——彙總篇
基於dis_max實現best fields策略進行多欄位搜尋
1、為帖子資料增加content欄位
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"} }
2、搜尋title或content中包含java或solution的帖子
下面這個就是multi-field搜尋,多欄位搜尋
GET /forum/article/_search { "query": { "bool": { "should": [ { "match": { "title": "java solution" }}, { "match": { "content": "java solution" }} ] } } }
3、結果分析
期望的是doc5,結果是doc2,doc4排在了前面
計算每個document的relevance score:每個query的分數,乘以matched query數量,除以總query數量
算一下doc4的分數
{ "match": { "title": "java solution" }},針對doc4,是有一個分數的
{ "match": { "content": "java solution" }},針對doc4,也是有一個分數的
所以是兩個分數加起來,比如說,1.1 + 1.2 = 2.3
matched query數量 = 2
總query數量 = 2
2.3 * 2 / 2 = 2.3
算一下doc5的分數
{ "match": { "title": "java solution" }},針對doc5,是沒有分數的
{ "match": { "content": "java solution" }},針對doc5,是有一個分數的
所以說,只有一個query是有分數的,比如2.3
matched query數量 = 1
總query數量 = 2
2.3 * 1 / 2 = 1.15
doc5的分數 = 1.15 < doc4的分數 = 2.3
4、best fields策略,dis_max
best fields策略,就是說,搜尋到的結果,應該是某一個field中匹配到了儘可能多的關鍵詞,被排在前面;而不是儘可能多的field匹配到了少數的關鍵詞,排在了前面
dis_max語法,直接取多個query中,分數最高的那一個query的分數即可
{ "match": { "title": "java solution" }},針對doc4,是有一個分數的,1.1
{ "match": { "content": "java solution" }},針對doc4,也是有一個分數的,1.2
取最大分數,1.2
{ "match": { "title": "java solution" }},針對doc5,是沒有分數的
{ "match": { "content": "java solution" }},針對doc5,是有一個分數的,2.3
取最大分數,2.3
然後doc4的分數 = 1.2 < doc5的分數 = 2.3,所以doc5就可以排在更前面的地方,符合我們的需要
GET /forum/article/_search
{
"query": {
"dis_max": {
"queries": [
{ "match": { "title": "java solution" }},
{ "match": { "content": "java solution" }}
]
}
}
}
基於tie_breaker引數優化dis_max
1、搜尋title或content中包含java beginner的帖子
GET /forum/article/_search
{
"query": {
"dis_max": {
"queries": [
{ "match": { "title": "java beginner" }},
{ "match": { "body": "java beginner" }}
]
}
}
}
可能在實際場景中出現的一個情況是這樣的:
(1)某個帖子,doc1,title中包含java,content不包含java beginner任何一個關鍵詞
(2)某個帖子,doc2,content中包含beginner,title中不包含任何一個關鍵詞
(3)某個帖子,doc3,title中包含java,content中包含beginner
(4)最終搜尋,可能出來的結果是,doc1和doc2排在doc3的前面,而不是我們期望的doc3排在最前面
dis_max,只是取分數最高的那個query的分數而已。
2、dis_max只取某一個query最大的分數,完全不考慮其他query的分數
3、使用tie_breaker將其他query的分數也考慮進去
tie_breaker引數的意義,在於說,將其他query的分數,乘以tie_breaker,然後綜合與最高分數的那個query的分數,綜合在一起進行計算
除了取最高分以外,還會考慮其他的query的分數
tie_breaker的值,在0~1之間,是個小數,就ok
GET /forum/article/_search
{
"query": {
"dis_max": {
"queries": [
{ "match": { "title": "java beginner" }},
{ "match": { "body": "java beginner" }}
],
"tie_breaker": 0.3
}
}
}
基於multi_match語法實現dis_max和tie_breaker
這邊的title^2表示把title這個欄位設定boost為2(權重預設是1)
minimum_should_match在之前的ElasticSearch教程——精準全文檢索一文中已經說過,是用來控制搜尋結果的精準度,只有匹配一定數量的關鍵詞的資料,才能返回
GET /forum/article/_search
{
"query": {
"multi_match": {
"query": "java solution",
"type": "best_fields",
"fields": [ "title^2", "content" ],
"tie_breaker": 0.3,
"minimum_should_match": "50%"
}
}
}
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
}
}
}
most fields
從best-fields換成most-fields策略
best-fields策略,主要是說將某一個field匹配儘可能多的關鍵詞的doc優先返回回來
most-fields策略,主要是說盡可能返回更多field匹配到某個關鍵詞的doc,優先返回回來
POST /forum/_mapping/article
{
"properties": {
"sub_title": {
"type": "string",
"analyzer": "english",
"fields": {
"std": {
"type": "string",
"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"} }
GET /forum/article/_search
{
"query": {
"match": {
"sub_title": "learning courses"
}
}
}
{
"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": 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"
}
}
]
}
}
sub_title欄位用的是enligsh analyzer,所以還原了單詞
為什麼,因為如果我們用的是類似於english analyzer這種分詞器的話,就會將單詞還原為其最基本的形態,stemmer
learning --> learn
learned --> learn
courses --> course
sub_titile: learning coureses --> learn course
{ "doc" : {"sub_title" : "learned a lot of course"} },就排在了{ "doc" : {"sub_title" : "learning more courses"} }的前面
GET /forum/article/_search
{
"query": {
"match": {
"sub_title": "learning courses"
}
}
}
GET /forum/article/_search
{
"query": {
"multi_match": {
"query": "learning courses",
"type": "most_fields",
"fields": [ "sub_title", "sub_title.std" ]
}
}
}
返回結果
{
"took": 2,
"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"
}
}
]
}
}
most fields 與 best_fields的區別
(1)best_fields,是對多個field進行搜尋,挑選某個field匹配度最高的那個分數,同時在多個query最高分相同的情況下,在一定程度上考慮其他query的分數。簡單來說,你對多個field進行搜尋,就想搜尋到某一個field儘可能包含更多關鍵字的資料
優點:通過best_fields策略,以及綜合考慮其他field,還有minimum_should_match支援,可以儘可能精準地將匹配的結果推送到最前面
缺點:除了那些精準匹配的結果,其他差不多大的結果,排序結果不是太均勻,沒有什麼區分度了
實際的例子:百度之類的搜尋引擎,最匹配的到最前面,但是其他的就沒什麼區分度了
(2)most_fields,綜合多個field一起進行搜尋,儘可能多地讓所有field的query參與到總分數的計算中來,此時就會是個大雜燴,出現類似best_fields案例最開始的那個結果,結果不一定精準,某一個document的一個field包含更多的關鍵字,但是因為其他document有更多field匹配到了,所以排在了前面;所以需要建立類似sub_title.std這樣的field,儘可能讓某一個field精準匹配query string,貢獻更高的分數,將更精準匹配的資料排到前面
優點:將盡可能匹配更多field的結果推送到最前面,整個排序結果是比較均勻的
缺點:可能那些精準匹配的結果,無法推送到最前面
實際的例子:wiki,明顯的most_fields策略,搜尋結果比較均勻,但是的確要翻好幾頁才能找到最匹配的結果。