Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using filter beside query_string in Elastic Search

How to full text search and have filter? I want to search for a text among documents with language_id=10. I've tried it this way:

{
  "query": {
    "query_string": {
      "query": "Declared"
    },
    {
      "filtered": {
        "filter": {
          "term": {
            "language_id": 10
          }
        }
      }
    }
  }
}

but seems like it's not correct. How to correct it?

like image 427
ehsan shirzadi Avatar asked Sep 29 '14 08:09

ehsan shirzadi


People also ask

What are filters in Elasticsearch?

A filter in Elasticsearch is all about applying some conditions inside the query that are used to narrow down the matching result set.

What is Elasticsearch post filter?

The post_filter is applied to the search hits at the very end of a search request, after aggregations have already been calculated.


1 Answers

In version 5.2, filtered query is replaced by the bool query, and returns error on my Elastic 5.2 instance. See here.

The new syntax is:

{
   "query":{
      "bool":{
         "must":{
            "query_string":{
               "query":"Declared"
            }
         },
         "filter":{
            "term":{
               "language_id":10
            }
         }
      }
   }
}
like image 200
snw Avatar answered Oct 14 '22 22:10

snw