[Recipes] Controlling Query Results and Formats in Elasticsearch

Problem: 

Show how to:

  1. Return yml instead of JSON in response.
  2. Format JSON to look nice.
  3. Do not include source in search result.
  4. Select fields that needs to be returned.

Solution Summary: 

We can use various parameters to controll the query results.

  1. Return yml instead of JSON in response.
    1. We can use format=yaml
  2. Format JSON to look nice.
    1. We can use pretty. Makes more sense when working from clients other than Kibana (e.g. terminal in Mac).
  3. Do not include source in search result.
    1. We can set _source to false.
  4. Select fields that needs to be returned.

Prerequisites: 

Set up accounts index from accounts.json as explained in the link

For case 5, we will use the department index and data created here.

Solution Steps: 

Case 1 - Returning YAML instead of JSON

GET /accounts/_search?format=yaml
{
  "query": { "match_all": {} }
}

 

Case 2 - Formatting results using pretty

GET /accounts/_search?pretty
{
  "query": { "match_all": {} }
}

 

Case 3 - Do not include any soure fields in result

GET /accounts/_search?pretty
{
  "query": { "match_all": {} },
  "_source": false
}

 

Case 4 - Selecting fields that needs to be returned

GET /accounts/_search?pretty
{
  "query": { "match_all": {} },
  "_source": ["account_number", "age"]
}

 

Case 5 - Using wildcard, includes and excludes for source filtering

GET /department/_search?pretty
{
  "query": { "match_all": {} },
  "_source": {
    "includes": "employees.*",
    "excludes": "employees.age"
  }
}

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