美文网首页
Elaticsearch 自定义得分查询

Elaticsearch 自定义得分查询

作者: chenxk | 来源:发表于2019-04-17 14:48 被阅读0次

    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
    }
    
    

    相关文章

      网友评论

          本文标题:Elaticsearch 自定义得分查询

          本文链接:https://www.haomeiwen.com/subject/vwknwqtx.html