0
votes

How to filter documents in elasticsearch 2.2 before executing query. I refered this page, but still couldn't frame correct query.

https://www.elastic.co/guide/en/elasticsearch/reference/2.2/query-filter-context.html

{
"size" : 9999,
"query" : {
"bool" : {     
    "filter" : {
    "bool" : {
      "must" : [ {
        "exists" : {
          "field" : "dvd"
        }
      } ]
    }
  },
  "should" : [{

    "bool" : {
      "must" : [ {
        "match" : {
          "rec" : {
            "query" : "CKS",
            "type" : "boolean"
          }
        }
      }, {
        "match" : {
          "date" : {
            "query" : "2016-03-09",
            "type" : "boolean"
          }
        }
      } ]
    }

  }]
}
  }
}

This is what i am trying to do.. I am having lot of Should clauses with each having 2 Must clause. Any one of should clause should match to retrieve the document. I have included only one should clause to make it simple. Query is working perfect. I want to apply filter over this. Filter documents before querying. But this is not working.

I am using Elasticsearch 2.2 ... Please let me know how to filter documents in this version of elasticsearch.

I think post filter is for different purpose.

2

2 Answers

1
votes

would suggest making a change to the filter part of the bool query. You can pass an array of queries to the filter item. It would than become: { "filter" : [ { "exists": { "field" : "dvd" } } ] }

0
votes

Regarding your comment under Jettro's question:

You can use a bool query/filter inside the filter block of bool query like so:

{
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "should": [
            {
              "exists": {
                "field": "dvd"
              }
            },
            {
              "exists": {
                "field": "album"
              }
            }
          ]
        }
      }
    }
  }
}

Now documents with either the DVD field or the album field will be returned.