Elasticsearch in filter context and query context - How to retrieve documents using filter

In previous posts Boolean compound queriesFull Text QueryQuery term and Source filtering,  we performed search operation in query context and retrieved documents using "query" keyword. Elasticsearch also provides search capability in "filter" context.

Filter context search operation

When query is run in filter context there is no relevance of document score. Each document in response has score of 1.0 and filter context queries responds with Yes(given document included) or No (given document excluded) from response.

Filter with boolean compound queries: Below query filter documents based on age and only allow document in response with age range <30 to 35>.
➜  Desktop curl -XGET 'localhost:9200/customers/_search?pretty' -d'
{
  "query": {
    "bool": {
      "must": { "match_all": {} },
      "filter": {
        "range": {
          "age": {
            "gte": 30,
            "lte": 35
          }
        }
      }
    }
  },
  "size": 2,
  "_source" :["nam*", "age"]
}
' -H 'Content-Type: application/json'
{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 97,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "customers",
        "_type" : "vendors",
        "_id" : "7n_g6mABB3_D7Pc85hFw",
        "_score" : 1.0,
        "_source" : {
          "name" : "Green Boyer",
          "age" : 32
        }
      },
      {
        "_index" : "customers",
        "_type" : "vendors",
        "_id" : "G3_g6mABB3_D7Pc85hJw",
        "_score" : 1.0,
        "_source" : {
          "name" : "Randall Sutton",
          "age" : 31
        }
      }
    ]
  }
}

Using filters along with search terms  : 
Below query uses filter within query context and filters documents with gender= female and age greater than 45. Response contains all records with gender female and age > 45.

➜  Desktop curl -XGET 'localhost:9200/customers/_search?pretty' -d'
{
  "query": {
    "bool": {
      "must":
        { "match": {
             "state":   "mississippi"
        }
      },
      "filter": [
        { "term":  { "gender": "female" }},
        { "range": { "age": { "gte": "45" }}}
      ]
    }
  },
  "size": 2,
  "_source" :["nam*", "age", "gend*"]
}
' -H 'Content-Type: application/json'
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 6,
    "max_score" : 4.326261,
    "hits" : [
      {
        "_index" : "customers",
        "_type" : "vendors",
        "_id" : "P3_g6mABB3_D7Pc85hJw",
        "_score" : 4.326261,
        "_source" : {
          "gender" : "female",
          "name" : "Regina Frederick",
          "age" : 49
        }
      },
      {
        "_index" : "customers",
        "_type" : "vendors",
        "_id" : "J3_g6mABB3_D7Pc85hNx",
        "_score" : 4.326261,
        "_source" : {
          "gender" : "female",
          "name" : "Alfreda Kent",
          "age" : 75
        }
      }
    ]
  }
}
 =======*****======

1 Comments

Previous Post Next Post