Kibana ElasticSearch脚本得分|轻松查询

hjzp0vay  于 2022-12-09  发布在  Kibana
关注(0)|答案(1)|浏览(201)

我正在玩脚本查询,这里有一个问题我想问社区,我不是很明白
下面是一个存储在ElasticSearch中的示例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"
      }
   ]
 
}

一旦我添加了这一行或者if语句,它就失败了

"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;
               """
           
            }
         }

如果有人能指出问题并提出可能的解决方案,那就太好了

c86crjj0

c86crjj01#

请尝试以下查询,您的查询完全正确。我在我的系统上复制了您的方案,它工作正常。为了获得更多的许可,我还参加了else if条件。可能是它的状态字段Map问题,其中一些可能是文本,给你这个错误。
您的查询如下所示:

相关问题