4

I have documents with a boolean field Say

 {
    "field1" : true
}

Now I want to check, whether the "field1" is true or not, and run a query if it is true. How can I do that using scripts in Elasticsearch?

3 Answers 3

4

Daniel's approach is superior to literally using scripts because it will be significantly faster, but if you had a need to do it within a script, then you need to check if the value indexed is 'T' or 'F' in Elasticsearch 1.x. In Elasticsearch 2.x and above, Lucene changed to using the more traditional 1 and 0.

ES 2.x (and above):

{
  "query" : {
    "bool" : {
      "filter" : [ {
        "script" : {
          "inline" : "doc['field1'].value == 1"
        }
      } ]
    }
  }
}

ES 1.x:

{
  "query" : {
    "filtered" : {
      "filter" : {
        "script" : {
          "script" : "doc['field1'].value == 'T'"
        }
      }
    }
  }
}

It's a bad idea to ever use a script unless you are required to do something that cannot be done without one, such as compare to fields simultaneously to ensure that they have the same value.

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

4 Comments

T and F don't seem working with ES2.X. You should use 1 or 0
Good point! Lucene changed boolean to 0 / 1 in Lucene 5.x.
@pickypg : Can you pls tell me that why using script is bad ? I am using inline script for comparison based sorting. Does it have security threat like SQL injection ?
@JayShah The performance is the main reason. Running a script is always slower than using the built-in queries whenever possible. For older versions of Elasticsearch, using Groovy scripting (the default from ES 1.4 through ES 2.4) could lead to security issues (and it's slower than the new default: Painless, beginning with ES 5.0).
1

It sounds like you wish to use a filtered query, to query only on documents where field1 is true.

If you create a filtered query, you apply a filter before you run a query, so you will be querying only those documents that pass your filter, you can setup a filtered query as below.

{
  "query":{
    "filtered":{
      "query":{
        //Your query
      },
      "filter":{
        "term":{"field1":true}
      }
    }
  }
}

Comments

0

You should use a bool query to match a boolean condition as you want. Look at my example.

{
   "query" : {
      "bool" : {
         "must" : 
            {
            "term" : {
               "field1" : true
            }
         }            
      }
   }
}

Hope it helps!

1 Comment

Using a term filter, which is cached, is generally superior to a term query, which is scored. If for no other reason, the scoring is largely irrelevant for exact matches, but it can make sense inside of should queries to add to the score.

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.