天天看點

更改ElasticSearch的相關度算法,以BM25為例(非修改源碼)

之前幾次要調節相關度算法都是直接修改的lucene的源碼包, 需要重新開機es。 随着深度使用es ,叢集重新開機又很麻煩,經過閱讀官網文檔終于找到了解決方案(建立mapping時,指定相關度)

官網說明https://www.elastic.co/guide/en/elasticsearch/reference/6.5/index-modules-similarity.html#bm25

{
  "settings": {
    "analysis": {
      "analyzer": {
        "comma": {
          "type": "pattern",
          "pattern": ","
        },
        "char": {
          "type": "pattern",
          "pattern": ""
        }
      }
    },
    "index" : {
            "similarity" : {
              "my_similarity" : { //自定義相關度算法名稱
                "type" : "BM25",//相關度算法
                "b" : "0.75",
                "k1" : "1"
              }
            }
        },
        "number_of_replicas": 1,
        "number_of_shards": 1
  },
  "mappings": {
    "info": {
      "properties": {
        "message": {
          "type": "text",
          "analyzer": "ik_max_word"
          , "similarity": "my_similarity" //使用自定義的相關度
        }
      }
    }
  }
}
           

效果展示

{
                      "value": 1.7618352,
                      "description": "tfNorm, computed as (freq * (k1 + 1)) / (freq + k1 * (1 - b + b * fieldLength / avgFieldLength)) from:",
                      "details": [
                        {
                          "value": 19,
                          "description": "termFreq=19.0",
                          "details": []
                        },
                        {
                          "value": 1,
                          "description": "parameter k1", //自定義的值
                          "details": []
                        },
                        {
                          "value": 0.75,
                          "description": "parameter b", // 自定義的值
                          "details": []
                        },
                        {
                          "value": 432.66666,
                          "description": "avgFieldLength",
                          "details": []
                        },
                        {
                          "value": 1337.4694,
                          "description": "fieldLength",
                          "details": []
                        }
                      ]
                    }
           

繼續閱讀