Lab 3.2: More queries
Objective:
In this lab, you will write more advanced search requests on the blogs_fixed2 using the Elasticsearch Query DSL.
-
Write a query to get the blogs written since 2018. Filter the
_sourceto display only thetitleandpublish_date. You should get 2770 hits.Solution
GET blogs_fixed2/_search { "_source": ["publish_date", "title"], "query": { "range": { "publish_date": { "gte": "2018-01-01" } } } } -
Update the previous query to get the blogs written in 2018. You should get 701 hits.
Solution
GET blogs_fixed2/_search { "_source": ["publish_date", "title"], "query": { "range": { "publish_date": { "gte": "2018-01-01", "lt": "2019-01-01" } } } } -
Run a
matchquery on theblogs_fixed2index for the terms "Shay Banon" in thecontentfield. You should get 165 hits.Solution
GET blogs_fixed2/_search { "query": { "match": { "content": "Shay Banon" } } } -
Now run a second
matchquery for "Shay Banon" on theauthors.full_namefield. You should get 204 hits.Solution
GET blogs_fixed2/_search { "query": { "match": { "authors.full_name": "Shay Banon" } } } -
Run a
multi_matchquery that searches both theauthors.full_nameandcontentfields for "Shay Banon". Does themulti_matchdeliver more or fewer hits?Solution
TheGET blogs_fixed2/_search { "query": { "multi_match": { "query": "Shay Banon", "fields": [ "content", "authors.full_name" ] } } }multi_matchdelivered many more hits. -
EXAM PREP: Write a query on the
blogs_fixed2index that satisfies the following requirements:- the term "meetups" appears in either the
titleorcontentfields of the blog - the blog was published within two years of today's date
You should get 14 hits.
Solution
GET blogs_fixed2/_search { "query": { "bool": { "must": [ { "multi_match": { "query": "meetups", "fields": [ "content", "title" ] } } ], "filter": [ { "range": { "publish_date": { "gte": "now/d-2y" } } } ] } } } - the term "meetups" appears in either the
-
EXAM PREP: Write a query on the
blogs_fixed2index that satisfies the following requirements:- get the blogs that mention "ingestion" in the
contentfield. - the term "logstash" must not be in the
contentfield. - the blogs are written in French (use the
fr-frlocale)
You should get 18 hits.
Solution
GET blogs_fixed2/_search { "query": { "bool": { "must": [ { "match": { "content": "ingestion" } } ], "must_not": [ { "match": { "content": "logstash" } } ], "filter": [ { "match": { "locale": "fr-fr" } } ] } } } - get the blogs that mention "ingestion" in the
Summary:
In this lab, you learned about range, multi_match and bool queries.