Elastic Search: Most Frequent Commands

Standard
  1. To check the cluster health
localhost:9200/_cat/health?v

2. List of nodes in our cluster

localhost:9200/_cat/nodes?v

3. List all Indices

localhost:9200/_cat/indices?v

4. Create an Index

PUT Customer

5. Insert Document into Index

PUT customer/external/1 
{
 "name": "John Doe"
}

PUT customer/external/2 
{
 "name": "Test User"
}

6. Retrieve Document from Index

GET customer
GET customer/external/1

7. Delete an Index

DELETE customer

8. Replace a Document

PUT customer/external/1 
{
 "name": "Test User"
}

Again, the above will index the specified document into the customer index, external type, with the ID of 1. If we then executed the above command again with a different (or same) document, Elasticsearch will replace (i.e. reindex) a new document on top of the existing one with the ID of 1:

PUT customer/external/1 
{
 "name": "Test User1"
}

10. Update a Document

Whenever we do an update, Elasticsearch deletes the old document and then indexes a new document with the update applied to it in one shot.

POST customer/external/1/_update
{
 "doc": {"name" : "Will Ham" , "country" : "UK"}
}

11. Delete a Document

DELETE customer/external/1

12. Batch Processing

Insertion. update, delete and mix of all operations can be done using _bulk api

13. Search a Document without Filter (SQL without Where clause)

http://localhost:9200/account/_search?q=*
GET account/_search?q=*
GET account/_search
{
 "query": { "match_all": {} }
}

13. Pagination

GET account/_search
{
 "query": { "match_all": {} },
 "from": 1,
 "size": 2
}

The from parameter (0-based) specifies which document index to start from and the size parameter specifies how many documents to return starting at the from parameter. This feature is useful when implementing paging of search results. Note that if from is not specified, it defaults to 0. Note that if size is not specified, it defaults to 10.

13. Sorting the Documents (SQL Order By clause)

GET account/_search
{
 "query": { "match_all": {} },
 "sort": { "age": { "order": "desc" } }
}

14. Selecting the Fields in Document (SQL Select clause)

GET account/_search
{
 "query": { "match_all": {} },
 "_source": ["firstname", "age"]
}

15. Filter on Fields in Document  (SQL Where clause)

GET account/_search
{
 "query": { "match": { "address": "Street" } }
}
GET account/_search
{
 "query": { "match": { "account_number": 6 } }
}

This example returns all accounts containing the term “street” or “lane” in the address:

GET account/_search
{
 "query": { "match": { "address": "street lane" } }
}

This example returns all accounts containing the term “street lane” in the address:

GET account/_search
{
 "query": { "match_phrase": { "address": "street lane" } }
}
  • Conditional OR statement in filters
GET account/_search
{
 "query": {
   "bool": {
     "should": [
       { "match": { "address": "street" } },
       { "match": { "address": "lane" } }
     ]
    }
  }
}
  • Conditional AND statement in filters
GET account/_search
{
 "query": {
   "bool": {
     "must": [
       { "match": { "address": "street" } },
       { "match": { "address": "lane" } }
     ]
    }
  }
}
  • Conditional NOT statement in filters
GET account/_search
{
 "query": {
   "bool": {
     "must_not": [
       { "match": { "address": "street" } },
       { "match": { "address": "lane" } }
     ]
    }
  }
}

bool must_not clause specifies a list of queries none of which must be true for a document to be considered a match.

  • Conditional AND + OR statement in filters
GET account/_search
{
 "query": {
   "bool": {
     "must": [
       { "match": { "age": "36" } }
      ],
     "should": [
       { "match": { "address": "street" } },
       { "match": { "address": "lane" } }
      ]
    }
  }
}

This example returns all accounts of anybody who is 40 years old and lives at address with street OR lane value in it.

  • Range filters
    GET account/_search
    {
     "query": {
       "bool": {
         "must": { "match_all": "36" } }
         "range": {
            "age": {
            "gte": 28,
            "lte": 33
           }
          }
        }
      }
    }

    This example return all account holder with age between 28 and 33, inclusive.

16. Aggregations on Fields in Document  (SQL Group By clause)

GET account/_search
{
 "size": 0,
 "aggs": {
   "group_by_gender": {
     "terms": {
     "field": "gender"
     }
    }
  }
}

Note that we set size=0 to not show search hits because we only want to see the aggregation results in the response. You can nest aggregations inside aggregations arbitrarily.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s