In Elasticsearch, searches are done using a query component. There are different types of queries described below:
- Match_All Query
This is the most basic query, it returns all the documents stored & the configurations of that index. Here, It will return all documents inside the vehicles index.
- Match Query
This command will check for the same field pattern. Here, It will return only those documents where the field “make” matches to “ford” keyword.
- Multi_match Query
This command will check match patterns for multiple fields. It is used when multiple fields are having the same keywords as a value. Here, It will check match patterns for “make” and “make_desc” fields and returns matched documents.
- Exists Query
It will return only those documents in which the particular field has appeared. Here, we’re counting how many documents are having field “condition” using the “_count ” endpoint. (we can retrieve the documents if we want, simply use “_search” endpoint.)
- Match_phrase Query
It allows you to check for a particular sentence/phrase in documents and returns the documents containing that particular phrase. Here, we’re searching for the documents which consist of “This car has very good” value in the “cars_desc” field.
- Range Query
This command will return you documents that have appeared in that given range. We can use “gt”, ”lt”, “gte” and “lte” fields to specify the range. Here, we’re fetching documents that have field “price” greater than or equal to 30000 and less than or equal to 100000.
- Sort along with query component
“sort” clause allows the sorting of documents that are going to be searched shown below. here, we’re sorting the “price” field by specifying the “order” clause as desc.
- Must, Must_not and Should Queries (Boolean)
must/must_not/should are Boolean queries in elastic search. So we have to use must/must_not/should clauses inside “bool“. We can use any one of these three clauses as per our requirement.
- must: must appear in matching documents & also contributes to score.
- Must_not: must not appear in matching documents & score 0 is returned to all documents.
- Should: should appear in matching documents, basically nice to have
In this case, we are performing all three operations on the” vehicles “ index. here, condition inside “must” must appear in output documents, likewise condition inside “must_not” clause must not appear in output documents and condition inside “should” clause will be nice to have. (notice that we have to specify these clauses inside “bool” )
- Filter Query (Boolean)
conditions within the filter component must appear in matching documents. Unlike “must” the score of the query will be ignored. Filter clauses are faster than any other components because they are executed in filter context, means scoring is ignored and clauses are considered for caching. “filter” clause must be specified within the “bool” component.
We can combine any query component with the “filter” component to perform some complex queries. “must” and “must_not” operations are performed within the “filter” clause. This GET command will retrieve documents matched with these “must” & “must_not” conditions by ignoring their scores and outputs will be cached into memory.