1. 程式人生 > 其它 >Elasticsearch 論壇實戰-基於tie_breaker引數優化dis_max搜尋效果

Elasticsearch 論壇實戰-基於tie_breaker引數優化dis_max搜尋效果

技術標籤:Elasticsearch實戰elasticsearch

Elasticsearch實戰

準備資料

PUT /forum/post/_bulk
{"index":{"_id":1}}
{"title":"java php", "content":"forum open MIjMReACTGaN564AnCZuHg"}
{"index":{"_id":2}}
{"title":"elasticsearch java apm-agent-configuration KvkaTyKFT-2zocQNC9j5nA", "content":"kibana post open MIjMReACTGaN564AnCZuHg  4508327 NfcOxURmuyoBIxJBSbvw MIjMReACTGaN564AnCZuHg"}
{"index":{"_id":3}}
{"title":"elasticsearch hadoop", "content":"kibana green open"}

繼續上一課時dis_max查詢上面的資料

GET /forum/post/_search
{
  "query": {
    "dis_max": {
      "queries": [
        {
          "match": {
            "title": "java kibana"
          }
        },
        {
          "match": {
            "content": "java kibana"
          }
        }
      ]
    }
  }
}

#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {

"value" : 3,
"relation" : "eq"
},
"max_score" : 0.5773649,
"hits" : [
{
"_index" : "forum",
"_type" : "post",
"_id" : "1",
"_score" : 0.5773649,
"_source" : {
"title" : "java php",
"content" : "forum open MIjMReACTGaN564AnCZuHg"
}
},
{
"_index" : "forum",
"_type" : "post",
"_id" : "3",
"_score" : 0.5376842,
"_source" : {
"title" : "elasticsearch hadoop",
"content" : "kibana green open"
}
},
{
"_index" : "forum",
"_type" : "post",
"_id" : "2",
"_score" : 0.3754778,
"_source" : {
"title" : "elasticsearch java apm-agent-configuration KvkaTyKFT-2zocQNC9j5nA",
"content" : "kibana post open MIjMReACTGaN564AnCZuHg 4508327 NfcOxURmuyoBIxJBSbvw MIjMReACTGaN564AnCZuHg"
}
}
]
}
}

我們不難發現文件1和3分別是title和content命中了,而文件2兩項都命中了卻排在了最後~為什麼呢?

學習過前面課時的同學應該不難發現,文件2的長度都特別長,所以單項的title或者content得分都會低,而dis_max只取最高分,所以出現上面的結果。

tie_breaker

將其他query的分乘以tie_breaker然後再綜合計算,也就是說不但看最高分,其他的也會考慮

GET /forum/post/_search
{
  "query": {
    "dis_max": {
      "queries": [
        {
          "match": {
            "title": "java kibana"
          }
        },
        {
          "match": {
            "content": "java kibana"
          }
        }
      ],
      "tie_breaker": 0.7
    }
  }
}

#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 0.61529297,
"hits" : [
{
"_index" : "forum",
"_type" : "post",
"_id" : "2",
"_score" : 0.61529297,
"_source" : {
"title" : "elasticsearch java apm-agent-configuration KvkaTyKFT-2zocQNC9j5nA",
"content" : "kibana post open MIjMReACTGaN564AnCZuHg 4508327 NfcOxURmuyoBIxJBSbvw MIjMReACTGaN564AnCZuHg"
}
},
{
"_index" : "forum",
"_type" : "post",
"_id" : "1",
"_score" : 0.5773649,
"_source" : {
"title" : "java php",
"content" : "forum open MIjMReACTGaN564AnCZuHg"
}
},
{
"_index" : "forum",
"_type" : "post",
"_id" : "3",
"_score" : 0.5376842,
"_source" : {
"title" : "elasticsearch hadoop",
"content" : "kibana green open"
}
}
]
}
}

如果有疑問,歡迎諮詢公眾號《小馬JAVA》