[Recipes] Elasticsearch Query DSL - Basic Queries (Query Context)

Problem: 

Using Elasticsearch query DSL, demo simple queries that match any of the words, all of the words and phrases. 

Also, selectively return only some fields in the response.

Solution Summary: 

Elasticsearch provides a full Query DSL (Domain Specific Language) based on JSON to define queries. We can execute queries either within query context or within a filter context. Query context is suitable for full-text queries, while filter contexts are suitable for queries against exact values.

Query context will have additional capabilities such as relevance checking while filter contexts don't. Filter context query results may also be cached. Hence, filter contexts may be better in terms of performance.

Within the Query DSL JSON, "query" denotes a query context whereas "filter" denote a filter context. We will look into query context here. Within "query", you can use "match" to specify the matches we require. By default, the "match" query split its search string and OR them. You can override it to do AND. If you want to match a whole string, you will need to use "match_phrase" instead.

We can use "_source" to selectively return some fields in the response. By default, the full JSON document is returned as part of all searches. This is referred to as the source (_source field in the search hits). If we don’t want the entire source document returned, we can request only a few fields from within source to be returned.

Prerequisites: 

Set up accounts index from accounts.json as explained here.

Solution Steps: 

Case 1 - Return accounts with account number 20

GET /accounts/_search
{
  "query": { 
    "match": { "account_number": 20 } 
  }
}

 

Case 2 - Return all accounts containing the term "mill" or "lane" in the address

GET /accounts/_search
{
  "query": { 
    "match": { "address": "mill lane" } 
  }
}

 

Case 3 - Return all accounts containing the term "mill" and "lane" in the address

GET /accounts/_search
{
  "query": { 
    "match": { 
      "address": {
        "query": "mill lane",
        "operator":"and"
      }
    } 
  }
}

 

Case 4 - Return all accounts containing the phrase "mill lane" in the address

GET /accounts/_search
{
  "query": { 
    "match_phrase": { 
      "address": "mill lane"
    } 
  }
}

 

Case 5 - Using "_source" to return two fields, account_number and balance

GET /accounts/_search
{
  "query": { "match_all": {} },
  "_source": ["account_number", "balance"]
}

Recipe Tags: 

Learn Serverless from Serverless Programming Cookbook

Contact

Please first use the contact form or facebook page messaging to connect.

Offline Contact
We currently connect locally for discussions and sessions at Bangalore, India. Please follow us on our facebook page for details.
WhatsApp (Primary): (+91) 7411174113
Phone (Escalations): (+91) 7411174114

Business newsletter

Complete the form below, and we'll send you an e-mail every now and again with all the latest news.

About

CloudMaterials is my blog to share notes and learning materials on Cloud and Data Analytics. My current focus is on Microsoft Azure and Amazon Web Services (AWS).

I like to write and I try to document what I learn to share with others. I believe that knowledge is useless unless you share it; the more you share, the more you learn.

Recent comments

Photo Stream