9710. Basic Queries in Elasticsearch
Elasticsearch


Notes for Elastic Search.

2. Usage

2.1 Creating

POST shop/customer/1 
{ "name": "Johnny", "address": "1234 Hillview Ave, Palo Alto", "level": "Silver" } 

Response:

{ "_index": "shop", "_type": "customer", "_id": "1", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "created": true } 

Create another two users.

POST shop/customer/2 
{ "name": "Sean", "address": "1010 Steven Creek Ave, Cupertino", "level": "Bronze" } 
POST shop/customer/3 
{ "name": "Norah", "address": "340 Lawrence Station Rd, Sunnyvale", "level": "Bronze" } 

2.2 Getting

GET shop/customer/1 

Response:

{ "_index": "shop", "_type": "customer", "_id": "1", "_version": 1, "found": true, "_source": { "name": "Johnny", "address": "1234 Hillview Ave, Palo Alto", "level": "Silver" } } 

2.3 Updating

PUT shop/customer/1 
{ "name": "Johnny", "address": "1234 Hillview Ave, Palo Alto", "level": "Golden" } 

Response:

{ "_index": "shop", "_type": "customer", "_id": "1", "_version": 2, "result": "updated", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "created": false } 

2.4 Deleting

DELETE shop/customer/3 

Response:

{ "found": true, "_index": "shop", "_type": "customer", "_id": "3", "_version": 2, "result": "deleted", "_shards": { "total": 2, "successful": 1, "failed": 0 } } 

Search

Get shop/customer/3 

Response:

{ "_index": "shop", "_type": "customer", "_id": "3", "found": false } 

2.5 Searching

a) Search all customers

GET /shop/customer/_search 

Response:

{ "took": 4, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 2, "max_score": 1, "hits": [ { "_index": "shop", "_type": "customer", "_id": "2", "_score": 1, "_source": { "name": "Sean", "address": "1010 Steven Creek Ave, Cupertino", "level": "Bronze" } }, { "_index": "shop", "_type": "customer", "_id": "1", "_score": 1, "_source": { "name": "Johnny", "address": "1234 Hillview Ave, Palo Alto", "level": "Golden" } } ] } } 

a) Search the customer named ‘Johnny’

GET /shop/customer/_search?q=name:Johnny 

Response:

{ "took": 3, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 1, "max_score": 0.2876821, "hits": [ { "_index": "shop", "_type": "customer", "_id": "1", "_score": 0.2876821, "_source": { "name": "Johnny", "address": "1234 Hillview Ave, Palo Alto", "level": "Golden" } } ] } } 

3. Reference