Search code examples
elasticsearchkibanaelastic-stackelasticsearch-5

Elastic search Script Score | Painless Query


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


Solution

  • 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:

    enter image description here