ES 加权重自定义排序

一、需求
es搜索keyWord字段,需要给es指定字段weight加上权重进行排序
es版本:elasticsearch 6.5.0
二、建立测试索引

 curl -XDELETE 'http://flxapp01:9200/local_service'
curl -X PUT '192.168.1.96:9200/local_service?pretty' \
-H 'Accept: application/json,text/plain, */*' \
-H 'Content-Type: application/json;charset=UTF-8' \
-d '{
 "settings": {
  "number_of_shards": 3,
  "number_of_replicas": 2
 },
 "mappings": {
  "local_basic":{
   "properties": {
    "keyWord": {"type": "text"},
    "weight": {"type": "long"},
    "gmtCreate": {"type": "long"},
    "gmtModified": {"type": "text"},
    "type": {"type": "text"}
   }
 }
 }
}'

二、加入测试数据

curl -X POST '192.168.1.59:9200/local_service/local_basic/1' \
-H 'Accept: application/json,text/plain, */*' \
-H 'Content-Type: application/json;charset=UTF-8' \
-d '
{
    "keyWord":"c",
    "weight":1,
    "gmtCreate":10011,
    "gmtModified":"liyingjiea",
     "type":"es"
    }'

三、排序代码
利用painless脚本,利用doc找到对应的索引字段值加上权重,doc[‘weigth’]

curl -XGET 'http://192.168.1.59:9200/es_local_service/_search/?pretty' \
-H 'Accept: application/json,text/plain, */*' \
-H 'Content-Type: application/json;charset=UTF-8' \
-d '{
"query": {
        "match_phrase": {
            "keyWord": "c"
        }
    },
    "sort":[ {
           "_script":{
           "type": "number",
            "script": {
                "source": "def s=0.0;s =  doc['\''weight'\''].value+params.score_percent;return s;",
                "params": {
                    "score_percent": 0.7, 
                    "course_percent": 0.3
                }
        },
        "order": "asc"
     }
  }
  ]
}'

四、问题
在利用linux 的curl 测试过程中出现如下错误

{
  "error" : {
    "root_cause" : [
      {
        "type" : "script_exception",
        "reason" : "compile error",
        "script_stack" : [
          "def s=0.0;s =  doc[weight].value+params.scor ...",
          "                   ^---- HERE"
        ],
        "script" : "def s=0.0;s =  doc[weight].value+params.score_percent;return s;",
        "lang" : "painless"
      }
    ],
    "type" : "search_phase_execution_exception",
    "reason" : "all shards failed",
    "phase" : "query",
    "grouped" : true,
    "failed_shards" : [
      {
        "shard" : 0,
        "index" : "local_service",
        "node" : "sFvc-I-CRSqUw1qlGFiLrA",
        "reason" : {
          "type" : "script_exception",
          "reason" : "compile error",
          "script_stack" : [
            "def s=0.0;s =  doc[weight].value+params.scor ...",
            "                   ^---- HERE"
          ],
          "script" : "def s=0.0;s =  doc[weight].value+params.score_percent;return s;",
          "lang" : "painless",
          "caused_by" : {
            "type" : "illegal_argument_exception",
            "reason" : "Variable [weight] is not defined."
          }
        }
      }
    ]
  },
  "status" : 500
}

在请求参数中,用的是doc[‘weight’] ,没用对单引号进行转义

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在给定的引用内容中,有一段关于自定义排序权重的代码片段。根据这段代码,我们可以得出以下结论:在SQL中,可以使用自定义排序规则来实现根据权重进行排序。根据引用的描述,你可以通过对索引的keyword属性进行排序来实现自定义排序。具体的实现方法是,在order by子句中使用arc.weight进行排序。例如,如果要按升序排序,可以使用"order by arc.weight ASC",如果要按降序排序,可以使用"order by arc.weight DESC"。所以,对于给定的问题,你可以按照这种方式使用自定义排序权重排序SQL查询结果。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [dede文章列表根据权重自定义排序](https://blog.csdn.net/weixin_30878361/article/details/96797792)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* [ElasticSearch painless脚本实现自定义打分排序](https://blog.csdn.net/qq_41935885/article/details/129952498)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值