ElasticSearch Hands On
ElasticSearch Overview at http://www.slideshare.net/ABCTalks/elastic-search-overview
Datasets: https://github.com/ramky1982/ee-datasets/blob/master/datasets/cars
ElasticSearch vs. RDBMS
ElasticSearch RDBMS
Field Column
Document Row
Mapping Schema
Points
 Shard is a group of Segments
 Default no of Shards = 5
 Once Index is created, the Shards cannot be changed
 Shards is per Index; Replica Set is per Shard
 Kibana is a User Interface for Search
Installation
1. Download ElasticSearch (elasticsearch-1.6.0.zip)
2. Unzip to D:  elasticsearch  (ES_HOME)
3. Install Marvel
ES_HOME bin> plugin -i elasticsearch/marvel/latest
4. Run ElasticSearch
ES_HOME bin> elasticsearch.bat
5. Test ElasticSearch Status
Page 1 of 7
http://localhost:9200/?pretty
6. Marvel Web Browser
http://localhost:9200/_plugin/marvel/
Marvel - Sense
Sample
PUT meetup/test/1
{
"name" : "arvind",
"age" : 38
}
Query Response
GET _search
{
"query": {
"match_all": {}
}
}
PUT meetup/vehicle/1
{
"name" : "figo",
"brand" : "ford",
"type" : "hatchback",
"price" : 600000
}
{
"_index": "meetup",
"_type": "vehicle",
"_id": "1",
"_version": 1,
"created": true
}
GET meetup/vehicle/1 {
"_index": "meetup",
"_type": "vehicle",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"name": "figo",
"brand": "ford",
"type": "hatchback",
"price": 600000
}
}
POST meetup/vehicle/1/_update
{
"doc" :
{
{
"_index": "meetup",
"_type": "vehicle",
"_id": "1",
Page 2 of 7
"price" : 660000
}
}
"_version": 2
}
POST meetup/vehicle
{
"name" : "santro",
"brand" : "hyundai",
"type" : "hatchback",
"price" : 500000
}
{
"_index": "meetup",
"_type": "vehicle",
"_id": "AU4PxI-36MsC0zl6aXJO",
"_version": 1,
"created": true
}
PUT meetup/vehicle
{
"name" : "verna",
"brand" : "maruti",
"type" : "sedan",
"price" : 850000
}
No handler found for uri [/meetup/vehicle] and
method [PUT]
Using POST
{
"_index": "meetup",
"_type": "vehicle",
"_id": "AU4Pxm5Y6MsC0zl6aXKL",
"_version": 1,
"created": true
}
GET _cat/indices?v
Note: “v” denotes verbose
health status index pri rep docs.count docs.deleted store.size pri.store.size
yellow open meetup 5 1 3 0 9.5kb 9.5kb
yellow open .marvel-2015.06.20 1 1 998 0 4mb 4mb
To get all the documents from the Index
GET meetup/vehicle/_search
{
"query": {
"match_all": {}
}
}
Get the "verna" vehicle details
GET meetup/vehicle/_search
{
"query": {
"match": {
"name": "verna"
}
}
}
GET meetup/vehicle/_mapping {
"meetup": {
"mappings": {
"vehicle": {
"properties": {
"brand": {
"type": "string"
},
"doc": {
"type": "string"
},
"name": {
"type": "string"
},
Page 3 of 7
"price": {
"type": "long"
},
"type": {
"type": "string"
}
}
}
}
}
}
POST meetup/vehicle/1/_update
{
"doc" :
{
"year" : 2000
}
}
{
"_index": "meetup",
"_type": "vehicle",
"_id": "1",
"_version": 3
}
GET meetup/vehicle/_mapping {
"meetup": {
"mappings": {
"vehicle": {
"properties": {
"brand": {
"type": "string"
},
"doc": {
"type": "string"
},
"name": {
"type": "string"
},
"price": {
"type": "long"
},
"type": {
"type": "string"
},
"year": {
"type": "long"
}
}
}
}
}
}
DELETE meetup {
"acknowledged": true
}
PUT meetup {
"acknowledged": true
}
PUT meetup/vehicle/_mapping
{
"vehicle" : {
{
"acknowledged": true
}
Page 4 of 7
"properties" : {
"name": {
"type": "string"
},
"brand": {
"type": "string"
},
"type": {
"type": "string"
},
"price": {
"type": "long"
}
}
}
}
Dynamic Mapping
PUT meetup/vehicle/_mapping
{
"vehicle" : {
"dynamic" : "strict",
"properties" : {
"name": {
"type": "string"
},
"brand": {
"type": "string"
},
"type": {
"type": "string"
},
"price": {
"type": "long"
}
}
}
}
PUT meetup/vehicle/1
{
"name" : "figo",
"brand" : "ford",
"type" : "hatchback",
"price" : "600000",
"year" : 2000
}
{
"error": "StrictDynamicMappingException[mapping
set to strict, dynamic introduction of [year] within
[vehicle] is not allowed]",
"status": 400
}
Bulk Insertion of Data from Datasets
POST /cars/transactions/_bulk
{ "index": {}}
{ "price" : 10000, "color" : "red", "make" : "honda", "sold" : "2015-01-28" }
{ "index": {}}
{ "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2015-02-05" }
{ "index": {}}
{ "price" : 30000, "color" : "green", "make" : "ford", "sold" : "2015-05-18" }
{ "index": {}}
Page 5 of 7
{ "price" : 15000, "color" : "blue", "make" : "toyota", "sold" : "2015-06-02" }
{ "index": {}}
{ "price" : 12000, "color" : "green", "make" : "toyota", "sold" : "2014-08-19" }
{ "index": {}}
{ "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2014-11-05" }
{ "index": {}}
{ "price" : 80000, "color" : "red", "make" : "bmw", "sold" : "2014-01-01" }
{ "index": {}}
{ "price" : 25000, "color" : "blue", "make" : "ford", "sold" : "2014-02-12" }
Retrieve Records
GET cars/transactions/_search
{
"query": {
"match_all": {}
}
}
Term
GET cars/transactions/_search
{
"query": {
"term": {
"color": {
"value": "blue"
}
}
}
}
Get documents from index "cars" with price between 9999 and 19999
GET cars/transactions/_search
{
"query": {
"range": {
"price": {
"gte": 9999,
"lte": 19999
}
}
}
}
Match and Range
GET cars/transactions/_search
{
"query": {
"filtered": {
"query": {"match": {
"make": "honda"
}},
"filter": {"range": {
"price": {
"gte": 9999,
"lte": 19999
}
}}
}
Page 6 of 7
}
}
Aggregation
GET /cars/transactions/_search?search_type=count
{
"aggs": {
"test": {
"terms": {
"field": "make"
}
}
}
}
Analyzers
GET /_analyze?analyzer=whitespace&text=Text to analyze(1)
GET /_analyze?analyzer=standard&text=Text to analyze(1)
GET /_analyze?analyzer=simple&text=Text to analyze(1)
Page 7 of 7

ElasticSearch Hands On

  • 1.
    ElasticSearch Hands On ElasticSearchOverview at http://www.slideshare.net/ABCTalks/elastic-search-overview Datasets: https://github.com/ramky1982/ee-datasets/blob/master/datasets/cars ElasticSearch vs. RDBMS ElasticSearch RDBMS Field Column Document Row Mapping Schema Points  Shard is a group of Segments  Default no of Shards = 5  Once Index is created, the Shards cannot be changed  Shards is per Index; Replica Set is per Shard  Kibana is a User Interface for Search Installation 1. Download ElasticSearch (elasticsearch-1.6.0.zip) 2. Unzip to D: elasticsearch (ES_HOME) 3. Install Marvel ES_HOME bin> plugin -i elasticsearch/marvel/latest 4. Run ElasticSearch ES_HOME bin> elasticsearch.bat 5. Test ElasticSearch Status Page 1 of 7
  • 2.
    http://localhost:9200/?pretty 6. Marvel WebBrowser http://localhost:9200/_plugin/marvel/ Marvel - Sense Sample PUT meetup/test/1 { "name" : "arvind", "age" : 38 } Query Response GET _search { "query": { "match_all": {} } } PUT meetup/vehicle/1 { "name" : "figo", "brand" : "ford", "type" : "hatchback", "price" : 600000 } { "_index": "meetup", "_type": "vehicle", "_id": "1", "_version": 1, "created": true } GET meetup/vehicle/1 { "_index": "meetup", "_type": "vehicle", "_id": "1", "_version": 1, "found": true, "_source": { "name": "figo", "brand": "ford", "type": "hatchback", "price": 600000 } } POST meetup/vehicle/1/_update { "doc" : { { "_index": "meetup", "_type": "vehicle", "_id": "1", Page 2 of 7
  • 3.
    "price" : 660000 } } "_version":2 } POST meetup/vehicle { "name" : "santro", "brand" : "hyundai", "type" : "hatchback", "price" : 500000 } { "_index": "meetup", "_type": "vehicle", "_id": "AU4PxI-36MsC0zl6aXJO", "_version": 1, "created": true } PUT meetup/vehicle { "name" : "verna", "brand" : "maruti", "type" : "sedan", "price" : 850000 } No handler found for uri [/meetup/vehicle] and method [PUT] Using POST { "_index": "meetup", "_type": "vehicle", "_id": "AU4Pxm5Y6MsC0zl6aXKL", "_version": 1, "created": true } GET _cat/indices?v Note: “v” denotes verbose health status index pri rep docs.count docs.deleted store.size pri.store.size yellow open meetup 5 1 3 0 9.5kb 9.5kb yellow open .marvel-2015.06.20 1 1 998 0 4mb 4mb To get all the documents from the Index GET meetup/vehicle/_search { "query": { "match_all": {} } } Get the "verna" vehicle details GET meetup/vehicle/_search { "query": { "match": { "name": "verna" } } } GET meetup/vehicle/_mapping { "meetup": { "mappings": { "vehicle": { "properties": { "brand": { "type": "string" }, "doc": { "type": "string" }, "name": { "type": "string" }, Page 3 of 7
  • 4.
    "price": { "type": "long" }, "type":{ "type": "string" } } } } } } POST meetup/vehicle/1/_update { "doc" : { "year" : 2000 } } { "_index": "meetup", "_type": "vehicle", "_id": "1", "_version": 3 } GET meetup/vehicle/_mapping { "meetup": { "mappings": { "vehicle": { "properties": { "brand": { "type": "string" }, "doc": { "type": "string" }, "name": { "type": "string" }, "price": { "type": "long" }, "type": { "type": "string" }, "year": { "type": "long" } } } } } } DELETE meetup { "acknowledged": true } PUT meetup { "acknowledged": true } PUT meetup/vehicle/_mapping { "vehicle" : { { "acknowledged": true } Page 4 of 7
  • 5.
    "properties" : { "name":{ "type": "string" }, "brand": { "type": "string" }, "type": { "type": "string" }, "price": { "type": "long" } } } } Dynamic Mapping PUT meetup/vehicle/_mapping { "vehicle" : { "dynamic" : "strict", "properties" : { "name": { "type": "string" }, "brand": { "type": "string" }, "type": { "type": "string" }, "price": { "type": "long" } } } } PUT meetup/vehicle/1 { "name" : "figo", "brand" : "ford", "type" : "hatchback", "price" : "600000", "year" : 2000 } { "error": "StrictDynamicMappingException[mapping set to strict, dynamic introduction of [year] within [vehicle] is not allowed]", "status": 400 } Bulk Insertion of Data from Datasets POST /cars/transactions/_bulk { "index": {}} { "price" : 10000, "color" : "red", "make" : "honda", "sold" : "2015-01-28" } { "index": {}} { "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2015-02-05" } { "index": {}} { "price" : 30000, "color" : "green", "make" : "ford", "sold" : "2015-05-18" } { "index": {}} Page 5 of 7
  • 6.
    { "price" :15000, "color" : "blue", "make" : "toyota", "sold" : "2015-06-02" } { "index": {}} { "price" : 12000, "color" : "green", "make" : "toyota", "sold" : "2014-08-19" } { "index": {}} { "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2014-11-05" } { "index": {}} { "price" : 80000, "color" : "red", "make" : "bmw", "sold" : "2014-01-01" } { "index": {}} { "price" : 25000, "color" : "blue", "make" : "ford", "sold" : "2014-02-12" } Retrieve Records GET cars/transactions/_search { "query": { "match_all": {} } } Term GET cars/transactions/_search { "query": { "term": { "color": { "value": "blue" } } } } Get documents from index "cars" with price between 9999 and 19999 GET cars/transactions/_search { "query": { "range": { "price": { "gte": 9999, "lte": 19999 } } } } Match and Range GET cars/transactions/_search { "query": { "filtered": { "query": {"match": { "make": "honda" }}, "filter": {"range": { "price": { "gte": 9999, "lte": 19999 } }} } Page 6 of 7
  • 7.
    } } Aggregation GET /cars/transactions/_search?search_type=count { "aggs": { "test":{ "terms": { "field": "make" } } } } Analyzers GET /_analyze?analyzer=whitespace&text=Text to analyze(1) GET /_analyze?analyzer=standard&text=Text to analyze(1) GET /_analyze?analyzer=simple&text=Text to analyze(1) Page 7 of 7