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.

  1. Using Console, write and execute a query that finds all the documents in the blogs_fixed2 index. 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
    

  2. 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 size parameter 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
    

  3. The _source parameter 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 previous match_all query, but have the response only contain each hit's title field.

    Solution
    GET blogs_fixed2/_search
    {
      "size": 50, 
      "_source": "title", 
      "query": {
        "match_all": {}
      }
    }
    
  4. If you just want specific fields from hits, the fields parameter is more efficient than using _source. Modify your match_all query so that the fields parameter is title and set _source to false (so that _source does not get returned).

    Solution
    GET blogs_fixed2/_search
    {
      "size": 50, 
      "_source": false,
      "fields": ["title"],
      "query": {
        "match_all": {}
      }
    }
    
  5. Run a match query on the blogs_fixed2 index for the terms "open source" in the content field. You should get 1725 hits.

    Solution
    GET blogs_fixed2/_search
    {
      "query": {
        "match": {
          "content": "open source"
        }
      }
    }
    
  6. 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"
          }
        }
      }
    }
    
  7. 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"
        }
      }
    }
    
  8. EXAM PREP: By default, the hits are sorted by score. Update the previous query to satisfy the following requirements:

    • Sort the results by publish_date with the most recent first.
    • Get the five most recent blogs
    • Filter the _source to display only the title and the publish_date.
    Solution
    GET blogs_fixed2/_search
    {
      "_source": ["title", "publish_date"], 
      "size": 5,
      "sort": [
        {
          "publish_date": {
            "order": "desc"
          }
        }
      ], 
      "query": {
        "match_phrase": {
          "content": "open source"
        }
      }
    }
    
  9. 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.