Lab 3.1: Searching with the Query DSL
Objective:
In this lab, you will write search requests on the blogs_fixed2 using the Elasticsearch Query DSL.
-
Using Console, write and execute a query that finds all the documents in the
blogs_fixed2index. You should have a total of 4719 hits.Solution
Either of the following two queries does a "match all":
GET blogs_fixed2/_search { "query": { "match_all": {} } } GET blogs_fixed2/_search -
Your previous query matched all documents, but Elasticsearch returned not all blogs - only ten documents are returned by default. Rerun your last query, but this time have it return 50 documents.
Solution
The
sizeparameter can be specified as a query parameter in the JSON or as a URL parameter:GET blogs_fixed2/_search { "size": 50, "query": { "match_all": {} } } GET blogs_fixed2/_search?size=50 -
The
_sourceparameter filters the fields that get returned in the hits, but keep in mind that this is done by parsing the JSON document source, resulting in additional overhead for Elasticsearch (although the amount of data transferred over the network decreases). Modify your previousmatch_allquery, but have the response only contain each hit'stitlefield.Solution
GET blogs_fixed2/_search { "size": 50, "_source": "title", "query": { "match_all": {} } } -
If you just want specific fields from hits, the
fieldsparameter is more efficient than using_source. Modify yourmatch_allquery so that thefieldsparameter istitleand set_sourceto false (so that_sourcedoes not get returned).Solution
GET blogs_fixed2/_search { "size": 50, "_source": false, "fields": ["title"], "query": { "match_all": {} } } -
Run a
matchquery on theblogs_fixed2index for the terms "open source" in thecontentfield. You should get 1725 hits.Solution
GET blogs_fixed2/_search { "query": { "match": { "content": "open source" } } } -
Update the previous query to search for blogs that contains "open" and "source" (instead of "open" or "source). You should get 772 hits.
Solution
GET blogs_fixed2/_search { "query": { "match": { "content": { "query": "open source", "operator": "and" } } } } -
In the previous query, you will match blogs where the terms "open" and "source" are not necessarily next to each other. Run a query to get the blogs that contain the exact phrase "open source". You should get 604 hits.
Solution
GET blogs_fixed2/_search { "query": { "match_phrase": { "content": "open source" } } } -
EXAM PREP: By default, the
hitsare sorted by score. Update the previous query to satisfy the following requirements:- Sort the results by
publish_datewith the most recent first. - Get the five most recent blogs
- Filter the
_sourceto display only thetitleand thepublish_date.
Solution
GET blogs_fixed2/_search { "_source": ["title", "publish_date"], "size": 5, "sort": [ { "publish_date": { "order": "desc" } } ], "query": { "match_phrase": { "content": "open source" } } } - Sort the results by
-
Get the five following blogs.
Solution
GET blogs_fixed2/_search { "_source": ["title", "publish_date"], "from": 5, "size": 5, "sort": [ { "publish_date": { "order": "desc" } } ], "query": { "match_phrase": { "content": "open source" } } }
Summary:
In this lab, you learned the basics of search using the Query DSL. You learned about the match and how to control the output using size, fields, and _source.