Elastic Search - Basic API

Recap :

Analogy to Relation Database :

  1. Index - DB
  2. Type - table
  3. document - row in table
  4. Fields - column 
  5. Mapping - schema

Here is an Example explaining the creation of a index and type and inserting records into the same and querying based on id and some other fields.

Below curl creates a index : test and employee type and the first record has the below fields.

Create a index and type and insert the first record

curl -X POST \
  http://localhost:9200/test/employee/001 \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -H 'postman-token: 0f26b0d3-aaa8-f36a-e34a-c88891dcb937' \
  -d '{
  "id": "001",
  "name":"priyanka",
  "dept":"cse"
}'
When we follow the above procedure the new type is created and the data type of the field is that of the first record. Incase you try to insert a different type of record this will fail in the subsequent insert. This feature is called dynamic mapping.
In order to avoid this it is better to have a custom mapping.

Mapping is to define how a document is the fields are stored and indexed.

While creating an index you can also specify the setting that defines the no of shards and replicas.
"settings" : {
    "index" : {
        "number_of_shards" : 3,
        "number_of_replicas" : 2
    }
}

Delete an existing Index :

To delete an index in elastic search
curl -X DELETE \
  'http://localhost:9200/accounting_transaction?pretty=&pretty=' \
  -H 'cache-control: no-cache' \
  -H 'postman-token: 6f47e1a4-58ab-4f53-bf74-c910bcf620df'

Insert a new record into ES 

You can also insert another document into the table with some additional fields. 

curl -X POST \

  http://localhost:9200/test/employee/002 \

  -H 'cache-control: no-cache' \

  -H 'content-type: application/json' \

  -H 'postman-token: 5dc10fd8-876d-6c3b-020f-c72706d9ec82' \
  -d '{
  "id": "001",
  "name":"priyanka",
  "dept":"cse"
}'

Search By id : 

Simple get api 

curl -X GET \
  http://localhost:9200/test/employee/002 \
  -H 'cache-control: no-cache' \
  -H 'postman-token: 903bd02d-82b6-7ba5-f261-60b038053d9d'

Search - get all records in the given type 

curl -X GET \
  http://localhost:9200/test/employee/_search \
  -H 'cache-control: no-cache' \
  -H 'postman-token: dcafd108-f53b-c3a7-ce2a-d8422e15cc11'

Search - get all records by query param id 

curl -X GET \
  'http://localhost:9200/test/employee/_search?q=_id%3A002' \
  -H 'cache-control: no-cache' \
  -H 'postman-token: 0fe9bc41-4f1f-5286-65d1-92136d214807'

Search - get all records with a specific query string :

Return all records matching the following search pattern.
{
  "query": { "match": { "name": "xyz" } }
}

curl -X POST \
  http://localhost:9200/test/employee/_search \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -H 'postman-token: 9864b2f6-61fa-825c-573f-7a1f5f6b6b03' \
  -d '{
  "query": { "match": { "name": "xyz" } }
}'

Update : update an existing record. Put api 

Sample curl 
curl -X PUT \
  http://localhost:9200/test/employee/002 \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -H 'postman-token: 44f20baf-42e9-a088-9f1a-05d2707a66dd' \
  -d '{
  "id": "002",
  "name":"xyz",
  "dept":"EAC"
}'

Response will have the following on success full updation 
 "result": "updated",


Some useful links : 

http://www.mkyong.com/elasticsearch/elasticsearch-hello-world-example/
https://dzone.com/articles/23-useful-elasticsearch-example-queries
https://www.elastic.co/guide/en/elasticsearch/reference/1.4/_executing_searches.html



Comments