ElasticSearch
Getting Started
http://elastic.openthinklabs.com/
Uji Coba Instalasi
● curl 'http://localhost:9200/?pretty'
● http://localhost:9200/_plugin/marvel/
● http://localhost:9200/_plugin/marvel/sense/
Talking to Elasticsearch
● Java API
● RESTful API with JSON over HTTP
curl -X<VERB> '<PROTOCOL>://<HOST>/<PATH>?<QUERY_STRING>' -d '<BODY>'
curl -XGET 'http://localhost:9200/_count?pretty' -d '
{
"query": {
"match_all": {}
}
} '
Document Oriented
JSON
Finding Your Feet
Let's Build an Employee Directory
● Enable data to contain multi value tags, numbers, and
full text.
● Retrieve the full details of any employee
● Allow structured search, such as finding employees over
the age of 30
● Allow simple full-text search and more-complex phrase
searches
● Return highlighted search snippets from the text in the
matching documents
● Enable management to build analytic dashboards over
the data
Indexing Employee Documents
● Relational DB Databases Tables Rows⇒ ⇒ ⇒
Columns⇒
● Elasticsearch Indices Types Documents⇒ ⇒ ⇒
Fields⇒
Index Versus Index Versus Index
● Index (noun)
● Index (verb)
● Inverted index
Indexing Employee Documents
● Megacorp : The index name
● Employee : The type name
● 1 : The ID of this particular employee
PUT /megacorp/employee/1
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
PUT /megacorp/employee/2
{
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 32,
"about" : "I like to collect rock albums",
"interests": [ "music" ]
}
PUT /megacorp/employee/3
{
"first_name" : "Douglas",
"last_name" : "Fir",
"age" : 35,
"about" : "I like to build cabinets",
"interests": [ "forestry" ]
}
Retrieving a Document
● GET /megacorp/employee/1
{
"_index": "megacorp",
"_type": "employee",
"_id": "1",
"_version": 2,
"found": true,
"_source": {
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
}
Search Lite
● GET /megacorp/employee/_search
● GET /megacorp/employee/_search?q=last_name:Smith
Search with Query DSL
GET /megacorp/employee/_search
{
"query" : {
"match" : {
"last_name" : "Smith"
}
}
}
More-Complicated Searches
GET /megacorp/employee/_search
{
"query" : {
"filtered" : {
"filter" : {
"range" : {
"age" : { "gt" : 30 }
}
},
"query" : {
"match" : {
"last_name" : "smith"
}
}
}
}
}
Full-Text Search
GET /megacorp/employee/_search
{
"query" : {
"match" : {
"about" : "rock climbing"
}
}
}
Phrase Search
GET /megacorp/employee/_search
{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
}
}
Highlighting Our Searches
GET /megacorp/employee/_search
{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
},
"highlight": {
"fields": {
"about":{}
}
}
}
Analytics
GET /megacorp/employee/_search
{
"aggs": {
"all_interests": {
"terms": { "field": "interests" }
}
}
}
GET /megacorp/employee/_search
{
"query": {
"match": {
"last_name": "smith"
}
},
"aggs": {
"all_interests": {
"terms": {
"field": "interests"
}
}
}
}
GET /megacorp/employee/_search
{
"aggs" : {
"all_interests" : {
"terms" : { "field" : "interests" },
"aggs" : {
"avg_age" : {
"avg" : { "field" : "age" }
}
}
}
}
}
Referensi
● ElasticSearch, The Definitive Guide, A
Distributed Real-Time Search and Analytics
Engine, Clinton Gormely & Zachary Tong,
O’Reilly

01 ElasticSearch : Getting Started

  • 1.
  • 2.
    Uji Coba Instalasi ●curl 'http://localhost:9200/?pretty' ● http://localhost:9200/_plugin/marvel/ ● http://localhost:9200/_plugin/marvel/sense/
  • 3.
    Talking to Elasticsearch ●Java API ● RESTful API with JSON over HTTP curl -X<VERB> '<PROTOCOL>://<HOST>/<PATH>?<QUERY_STRING>' -d '<BODY>' curl -XGET 'http://localhost:9200/_count?pretty' -d ' { "query": { "match_all": {} } } '
  • 4.
  • 5.
  • 6.
  • 7.
    Let's Build anEmployee Directory ● Enable data to contain multi value tags, numbers, and full text. ● Retrieve the full details of any employee ● Allow structured search, such as finding employees over the age of 30 ● Allow simple full-text search and more-complex phrase searches ● Return highlighted search snippets from the text in the matching documents ● Enable management to build analytic dashboards over the data
  • 8.
    Indexing Employee Documents ●Relational DB Databases Tables Rows⇒ ⇒ ⇒ Columns⇒ ● Elasticsearch Indices Types Documents⇒ ⇒ ⇒ Fields⇒
  • 9.
    Index Versus IndexVersus Index ● Index (noun) ● Index (verb) ● Inverted index
  • 10.
    Indexing Employee Documents ●Megacorp : The index name ● Employee : The type name ● 1 : The ID of this particular employee PUT /megacorp/employee/1 { "first_name" : "John", "last_name" : "Smith", "age" : 25, "about" : "I love to go rock climbing", "interests": [ "sports", "music" ] } PUT /megacorp/employee/2 { "first_name" : "Jane", "last_name" : "Smith", "age" : 32, "about" : "I like to collect rock albums", "interests": [ "music" ] } PUT /megacorp/employee/3 { "first_name" : "Douglas", "last_name" : "Fir", "age" : 35, "about" : "I like to build cabinets", "interests": [ "forestry" ] }
  • 11.
    Retrieving a Document ●GET /megacorp/employee/1 { "_index": "megacorp", "_type": "employee", "_id": "1", "_version": 2, "found": true, "_source": { "first_name": "John", "last_name": "Smith", "age": 25, "about": "I love to go rock climbing", "interests": [ "sports", "music" ] } }
  • 12.
    Search Lite ● GET/megacorp/employee/_search ● GET /megacorp/employee/_search?q=last_name:Smith
  • 13.
    Search with QueryDSL GET /megacorp/employee/_search { "query" : { "match" : { "last_name" : "Smith" } } }
  • 14.
    More-Complicated Searches GET /megacorp/employee/_search { "query": { "filtered" : { "filter" : { "range" : { "age" : { "gt" : 30 } } }, "query" : { "match" : { "last_name" : "smith" } } } } }
  • 15.
    Full-Text Search GET /megacorp/employee/_search { "query": { "match" : { "about" : "rock climbing" } } }
  • 16.
    Phrase Search GET /megacorp/employee/_search { "query": { "match_phrase" : { "about" : "rock climbing" } } }
  • 17.
    Highlighting Our Searches GET/megacorp/employee/_search { "query" : { "match_phrase" : { "about" : "rock climbing" } }, "highlight": { "fields": { "about":{} } } }
  • 18.
    Analytics GET /megacorp/employee/_search { "aggs": { "all_interests":{ "terms": { "field": "interests" } } } } GET /megacorp/employee/_search { "query": { "match": { "last_name": "smith" } }, "aggs": { "all_interests": { "terms": { "field": "interests" } } } } GET /megacorp/employee/_search { "aggs" : { "all_interests" : { "terms" : { "field" : "interests" }, "aggs" : { "avg_age" : { "avg" : { "field" : "age" } } } } } }
  • 19.
    Referensi ● ElasticSearch, TheDefinitive Guide, A Distributed Real-Time Search and Analytics Engine, Clinton Gormely & Zachary Tong, O’Reilly