To use the query DSL, we're going to send a post request and we're going to pass the query in the body of our request. We can start with an empty query which is the same as matching every document in Elasticsearch. You'll also notice that I've switched from using cURL to using Postman, and cURL will still work just fine, but Postman is just going to make the queries easier to type and for you to read.
Execute that, and you can see 600 documents are returned. So query's using the query DSL, take the form of the query name, field name and value. We're going to specify a query, and we want a match our title to the word "Homer," and if I run that in the results here, you can see all of our results have the word Homer in the title.
One thing I want to show you real quick is I can also do this with a lowercase h, and we still get the same results back, so query matches our case insensitive in Elasticsearch. This simple query is known as a leaf clause meaning it just compares a field to a query string. We can build more complex queries with compound clauses to combine multiple queries together.
For example, we can do a bool clause and say that it must match the title to Homer and must not...We're going to use a range query here, have an IMDB rating of greater than eight. When we run that, we get the Colonel Homer episode IMDB rating of 7.9, 7.2 for that rating. This one's equal to eight. This one's a seven, and so as the name implies, the bool type query uses Boolean logic to determine whether or not the documents are match for the query.
You've already seen the match in operation. It's a type of bool query that can analyze text, numbers and dates in a query, and we're also using a range query here to exclude results with an IMDB rating of greater than eight, and a range behaves as you might expect. we can modify our range query a bit to return episodes with an IMDB rating of greater than four but less than eight.
When we run that, we've got a 7.9 here, 7.2, a 7.
We can also use a filter here instead of a must, and the results are going to be the same, but what's going to be different is the relevance score. In our previous query, it looked at documents that match the title of Homer and had the IMDB rating in the range that we specified and that used both of those to calculate the relevance of how likely that was a match for our search.
Using this filter, what it does first is it uses the filter to exclude documents that don't match the filter criteria and then it only bases the relevance based on the match criteria here. Filter is a good way to remove documents and reduce the number of documents that are going to match, and then base your relevance only on the search criteria that's important to you.
So that's enough to get you started with the basics of querying data using the query DSL. I'd encourage you to look at the Elasticsearch documentation to find out more about it. There's a lot of different query clauses that you can use to produce different results, and they all follow the same basic structure that I've shown you here.