弹性搜索脚本分数|无痛的查询
我正在使用脚本查询,这是我想向社区提出的问题,我不太了解
这里是一个存储在弹性搜索中的示例JSON
{
"_index" : "XXXXX",
"_type" : "_doc",
"_id" : "XXXX==",
"_score" : 28.134966,
"_source" : {
"recruiter_id" : "XXXXX",
"jobseeker_id" : "XXX",
"profile_id" : "XXXXX",
"isBlocked" : "",
"isConnected" : "",
"connection_time" : "2022-06-14 20:17:01",
"status" : "pending",
"isPending" : "True"
}
的查询
GET XXX/_search
{
"query":{
"function_score":{
"query":{
"match_all": {}
},
"script_score":{
"script":{
"lang": "painless",
"source":"""
double total = 0.0;
total = total + Math.log(doc['connection_time'].value.millis);
return total;
"""
}
}
}
},
"sort":[
{
"_score":"desc"
}
]
}
,这是我编写 行或如果说明失败
"script_score":{
"script":{
"lang": "painless",
"source":"""
double total = 0.0;
total = total + Math.log(doc['connection_time'].value.millis);
if(doc['status'].value == 'accepted'){
total = total +100;
}
return total;
"""
}
}
错误,我遇到的是
"org.elasticsearch.index.mapper.TextFieldMapper$TextFieldType.fielddataBuilder(TextFieldMapper.java:823)",
"org.elasticsearch.index.fielddata.IndexFieldDataService.getForField(IndexFieldDataService.java:120)",
"org.elasticsearch.index.query.QueryShardContext.lambda$lookup$1(QueryShardContext.java:328)",
"org.elasticsearch.search.lookup.SearchLookup.lambda$new$1(SearchLookup.java:68)",
"org.elasticsearch.search.lookup.LeafDocLookup$1.run(LeafDocLookup.java:95)",
"org.elasticsearch.search.lookup.LeafDocLookup$1.run(LeafDocLookup.java:92)",
"java.base/java.security.AccessController.doPrivileged(Native Method)",
"org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:92)",
"org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:40)",
"if(doc['status'].value == 'accepted'){\n
方案,那么似乎不会到达该字段
"script_score":{
"script":{
"lang": "painless",
"source":"""
double total = 0.0;
total = total + Math.log(doc['connection_time'].value.millis);
if(doc['status.keyword'].value == 'accepted'){
total = total +100;
}
return total;
"""
}
}
我所做的,我本质上将字段更改为关键字,但是如果有人可以指出问题并建议可能的解决
i was playing with script query and here is a question I wanted to ask to the community which I didn't quite understand well
Here is a sample JSON stored in elastic search
{
"_index" : "XXXXX",
"_type" : "_doc",
"_id" : "XXXX==",
"_score" : 28.134966,
"_source" : {
"recruiter_id" : "XXXXX",
"jobseeker_id" : "XXX",
"profile_id" : "XXXXX",
"isBlocked" : "",
"isConnected" : "",
"connection_time" : "2022-06-14 20:17:01",
"status" : "pending",
"isPending" : "True"
}
Here is the query I wrote which works great
GET XXX/_search
{
"query":{
"function_score":{
"query":{
"match_all": {}
},
"script_score":{
"script":{
"lang": "painless",
"source":"""
double total = 0.0;
total = total + Math.log(doc['connection_time'].value.millis);
return total;
"""
}
}
}
},
"sort":[
{
"_score":"desc"
}
]
}
As soon as I add this line or if statement it fails
"script_score":{
"script":{
"lang": "painless",
"source":"""
double total = 0.0;
total = total + Math.log(doc['connection_time'].value.millis);
if(doc['status'].value == 'accepted'){
total = total +100;
}
return total;
"""
}
}
Error I get is this
"org.elasticsearch.index.mapper.TextFieldMapper$TextFieldType.fielddataBuilder(TextFieldMapper.java:823)",
"org.elasticsearch.index.fielddata.IndexFieldDataService.getForField(IndexFieldDataService.java:120)",
"org.elasticsearch.index.query.QueryShardContext.lambda$lookup$1(QueryShardContext.java:328)",
"org.elasticsearch.search.lookup.SearchLookup.lambda$new$1(SearchLookup.java:68)",
"org.elasticsearch.search.lookup.LeafDocLookup$1.run(LeafDocLookup.java:95)",
"org.elasticsearch.search.lookup.LeafDocLookup$1.run(LeafDocLookup.java:92)",
"java.base/java.security.AccessController.doPrivileged(Native Method)",
"org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:92)",
"org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:40)",
"if(doc['status'].value == 'accepted'){\n
what I did is I essentially changed the field to keywords but it seems like it's not going to if the statement
"script_score":{
"script":{
"lang": "painless",
"source":"""
double total = 0.0;
total = total + Math.log(doc['connection_time'].value.millis);
if(doc['status.keyword'].value == 'accepted'){
total = total +100;
}
return total;
"""
}
}
if anyone can point the issue and suggest possible fix that would be great
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请尝试以下查询,您的查询都很好。我在系统上复制了您的方案,它可以正常工作。有关更多的许可,我参加了其他条件。可能是状态字段映射的问题,其中一些可能是给您这个错误的文本。
您的查询看起来像:
Please try below query, your query is all good. I replicated your scenario on my system and it works fine. For more clearance I have attended else if condition along with it. May be its issue with the status field mapping, some of it might be text which gave you this error.
Your query looks like: