Elaticsearch 自定义得分查询

2019-04-17  本文已影响0人  chenxk

1.概述

本文主要是利用painless 脚本的方式实现ElasticSearch的自定义得分查询,类似SQL中的 case when .... end

2.编写painless 脚本

GET _scripts/matrix_search_score_V2

POST _scripts/matrix_search_score_V2
{
  "script": {
    "lang": "painless",
    "source": "String name = doc['dsspname.keyword'].value;Double hot_score = doc['hot_score'].value; HashMap map = params.scoreMap; if(map.containsKey(name)) return map.get(name); return 0;"
  }
}

3.查询条件拼接

GET /st_mining_v2-2/_search
{
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "filter": [
            {
              "bool": {
                "should": [
                  {
                    "match_phrase": {
                      "dsspname.keyword": {
                        "query": "qq音速"
                      }
                    }
                  },
                  {
                    "match_phrase": {
                      "dsspname.keyword": {
                        "query": "qq音乐"
                      }
                    }
                  },
                  {
                    "match_phrase": {
                      "dsspname.keyword": {
                        "query": "qq音速4"
                      }
                    }
                  }
                ]
              }
            },
            {
              "bool": {
                "should": [
                  {
                    "match_phrase": {
                      "type": {
                        "query": 10
                      }
                    }
                  }
                ]
              }
            }
          ]
        }
      },
      "script_score": {
        "script": {
          "params": {
            "scoreMap": {
              "qq音速": 3.4234074020394,
              "qq音乐": 1.7488613892483,
              "qq音速4": 1.5
            }
          },
          "id": "matrix_search-score"
        }
      },
      "boost_mode": "replace"
    }
  },
  "from": 0,
  "size": 16
}

上一篇下一篇

猜你喜欢

热点阅读