Well,不要刷屏了

Elasticsearch script score问题

Elasticsearch | 作者 wjxing | 发布于2016年10月18日 | 阅读数:8119

想根据文档中的多个字段进行排序,使用script score报错,No such property: w1 for class: 0d34f35ce7dcd19d5942f7f52352cfbb6627c6d0"
 
1、 文档结构,其中w1-w5都在[0-10]的范围内
curl -XPUT 'http://localhost:9200/stars/star/1' -d '{
    "user" : "test",
    "title" : "明星1",
    "w1" : 10,
    "w2" : 1,
    "w3": 5,
    "w4": 3,
    "w5": 8
}'
 
2,检索请求,分别设置w1-w5的权重,然后综合排序,请求如下
curl -XGET 'http://localhost:9202/stars/star/_search' -d '{
  "query": {
    "function_score": {
      "query": {
        "match": {
          "title": "明星"
        }
      },
      "script_score": {
        "script":"doc['w1'].value * w1_factor + doc['w2'].value * w2_factor + doc['w3'].value * w3_factor + doc['w4'].value * w4_factor + doc['w5'].value * w5_factor",
         "params" : {
             "w1_factor" : 90,
             "w2_factor" : 90,
             "w3_factor" : 90,
             "w4_factor" : 80,
             "w5_ctr" : 0.10
             }
      }
    }
  }
}'
 
 
3.返回错误结果,No such property: w1 for class: 0d34f35ce7dcd19d5942f7f52352cfbb6627c6d0"
{
"took":19,
"timed_out":false,
"_shards":{
"total":5,
"successful":4,
"failed":1,
"failures":[
{
"shard":3,
"index":"stars",
"node":"9eUXBp57SqaDWwUS_pCK2A",
"reason":{
"type":"script_exception",
"reason":"failed to run inline script [doc[w1].value * w1_factor + doc[w2_factor].value * w2_factor + doc[w3].value * w3_factor + doc[w4_factor].value * w4_factor + doc[w5].value * w5_factor] using lang [groovy]",
"caused_by":{
"type":"missing_property_exception",
"reason":"No such property: w1 for class: 0d34f35ce7dcd19d5942f7f52352cfbb6627c6d0"
}
}
}
]
},
"hits":{
"total":0,
"max_score":null,
"hits":{
}
}
}
已邀请:

zpzkit

赞同来自:

我也出现同样错误,但是我这边有一个自己内部写的执行es查询语句的页面,在那个页面上运行就是没有问题的,但是我用curl方式访问HTTP接口就出这个错误,应该是自己哪里有问题,再看看吧,希望如果有遇到同样问题的人能提供一点解决思路。

zpzkit

赞同来自:

我的这个错误找到问题的原因并解决了:
我的es版本是2.1.2有点旧的版本了,在写script的时候是
"script": {
  "inline": "if (doc['inspQtty'].value == 0) val = 0; else val = doc['outWhSaleQtty'].value / doc['inspQtty'].value; return val"
                                }
注意doc取值是通过单引号取值的,后来改成了这样:
"script": {
"inline": "if (doc[\"inspQtty\"].value == 0) val = 0; else val = doc[\"outWhSaleQtty\"].value / doc[\"inspQtty\"].value; return val"
}
这个可能是es内部对脚本语言的符号解析问题。

要回复问题请先登录注册