Elasticsearch Boolean Clauses

GET movies/_search

{

          “query”: {

                   “bool”: {

                            “must”: {

                                     “term”: { “text”: “comedy” }

                            },

                            “should”: [

                                     {“term”: { “text”: “Academy Award”   }},

                                     {“term”: { “text”: “Golden Globe”   }},

                                     {“term”: { “text”: “People’s Choice”   }}

                            ],

                            **“minimum_should_match”: 2**

                   }

          }

}

Filter Query for Faster Search Performance

If there is a query that is commonly used, then writing that query with the filter clause is ideal because filtered queries are automatically stored in memory (cached) for fast retrieval.

For some searches it makes sense to use scoring to rank the relevancy of matches. Scored searches aren’t compatible with caching though so keep that in mind. One way of handling this limitation is that you can create a filtered cache for the aspects of the query that don’t require scoring, and then run the scored portion of the query on the filtered subset of the database. With that, let’s dive into scoring.

GET customers/_search

{

          “query”: {

                   “bool”: {

                            “filter”: [

                                     {“term”: { “state”: “Texas” }},

                                     {“range”: { “total_spend”: {“gte”: “200.00” }}}

                           ],

                   }

          }

}

Scoring of Matches

Elasticsearch ascribes scores to matches that rank the matched documents by their relevance to the search parameters. All scores are represented by a floating number greater than zero, and the greater the score the better matched it is to the search query.

Filters do not affect scoring.

Get all indices from elasticsearch

GET /_cat/indices