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.

  1. Write a query to get the blogs written since 2018. Filter the _source to display only the title and publish_date. You should get 2770 hits.

    Solution
    GET blogs_fixed2/_search
    {
      "_source": ["publish_date", "title"],
      "query": {
        "range": {
          "publish_date": {
            "gte": "2018-01-01"
          }
        }
      }
    }
    
  2. 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"
          }
        }
      }
    }
    
  3. Run a match query on the blogs_fixed2 index for the terms "Shay Banon" in the content field. You should get 165 hits.

    Solution
    GET blogs_fixed2/_search
    {
      "query": {
        "match": {
          "content": "Shay Banon"
        }
      }
    }
    
  4. Now run a second match query for "Shay Banon" on the authors.full_name field. You should get 204 hits.

    Solution
    GET blogs_fixed2/_search
    {
      "query": {
        "match": {
          "authors.full_name": "Shay Banon"
        }
      }
    }
    
  5. Run a multi_match query that searches both the authors.full_name and content fields for "Shay Banon". Does the multi_match deliver more or fewer hits?

    Solution

    GET blogs_fixed2/_search
    {
      "query": {
        "multi_match": {
          "query": "Shay Banon",
          "fields": [
            "content",
            "authors.full_name"
          ]
        }
      }
    }
    
    The multi_match delivered many more hits.

  6. EXAM PREP: Write a query on the blogs_fixed2 index that satisfies the following requirements:

    • the term "meetups" appears in either the title or content fields 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"
                }
              }
            }
          ]
        }
      }
    }
    
  7. EXAM PREP: Write a query on the blogs_fixed2 index that satisfies the following requirements:

    • get the blogs that mention "ingestion" in the content field.
    • the term "logstash" must not be in the content field.
    • the blogs are written in French (use the fr-fr locale)

    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"
              }
            }
          ]
        }
      }
    }
    

Summary:

In this lab, you learned about range, multi_match and bool queries.