0

I have to query elasticsearch for sum of two fields in a doc, that fall in a particular range.

I'm trying to create a Java API for the below elastic search query, but not able to make out how to include script tags and select doc elements i.e doc['maintenanceCost']. Any help would be appreciated.

{
   "query": {
       "bool" : {
           "filter" : {
               "script" : {
                   "script" : "doc['price'].value + doc['maintenanceCost'].value  > 1000 && doc['price'].value + doc['maintenanceCost'].value  < 10000"     
           }
           }
       }
   }
}

I basically need docs in which sum of price andenter code here maintenance cost fall between 1000 to 10000

I have tried out few queries like ScriptBuilders, but none of them worked

1 Answer 1

1

You can use QueryBuilders.scriptQuery() with the Script object for this. In your case it would be something like this:

QueryBuilders.scriptQuery(
        new Script("doc['price'].value + doc['maintenanceCost'].value  > 1000 && doc['price'].value + doc['maintenanceCost'].value  < 10000")                  
    );

More detailed information about this method can be found here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.