AND-ing a 'must' and 'filter' query


#1

I'm trying to fetch data from an elasticsearch index when the following criteria are met.

  1. When the stream_id is an empty string (e.g. "")
  2. When the source_id is a specific string (e.g. "unknown_source")

I can use the two following queries separately:
For checking empty string:

`{
"query": {
    "filtered": {
        "filter": {
             "script": {
                 "script": "_source.stream_id.length() == 0"
             }
        }
    }
}
}`

For checking source_id

`{
"query": { 
    "bool" : {
        "must" : {
            "term" : { "source_id" : "unknown_source" }
        }
    }
}
}`

But now I see the 'filtered' query is deprecated in ES version 2.0 and I'm using version 2.1. So, how do I AND these two conditions? I've looked into the filtered query page documentation and it says

Use the bool query instead with a must clause for the query and a filter
clause for the filter.

So, then I was trying the following query but it's not working.

` {
"query": {
    "bool": {
        "must" : {
            "term" : { "source_id" : "unknown_source" }
       },
    "filter": {
        "script": {
            "script": "_source.stream_id.length() == 0"
        }
    }
}
}
}`

#2

Okay, it's solved. please refer to this answer.


#3

The syntax above looks correct and jives with the documentation. The bool query takes "must", "should", 'must_not" and "filter." "filter" is treated as a "must" that has no scoring.

Can you explain what you mean by 'not working'? Does it cause an error or is it not working logically as you expect?

The answers on the linked page don't seem to be going in the right direction to me. They seem more convoluted than should be required here.


#4

I was getting some parsing failures. The second answer in that link works fine for me.


(system) #5