19
ElasticSearch Getting Started http://elastic.openthinklabs.com/

01 ElasticSearch : Getting Started

Embed Size (px)

Citation preview

Page 1: 01 ElasticSearch : Getting Started

ElasticSearch

Getting Startedhttp://elastic.openthinklabs.com/

Page 2: 01 ElasticSearch : Getting Started

Uji Coba Instalasi

● curl 'http://localhost:9200/?pretty'● http://localhost:9200/_plugin/marvel/● http://localhost:9200/_plugin/marvel/sense/

Page 3: 01 ElasticSearch : Getting Started

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": {} }} '

Page 4: 01 ElasticSearch : Getting Started

Document Oriented

Page 5: 01 ElasticSearch : Getting Started

JSON

Page 6: 01 ElasticSearch : Getting Started

Finding Your Feet

Page 7: 01 ElasticSearch : Getting Started

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

Page 8: 01 ElasticSearch : Getting Started

Indexing Employee Documents

● Relational DB Databases Tables Rows ⇒ ⇒ ⇒ Columns⇒

● Elasticsearch Indices Types Documents ⇒ ⇒ ⇒ Fields⇒

Page 9: 01 ElasticSearch : Getting Started

Index Versus Index Versus Index

● Index (noun)● Index (verb)● Inverted index

Page 10: 01 ElasticSearch : Getting Started

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" ]}

Page 11: 01 ElasticSearch : Getting Started

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" ] }}

Page 12: 01 ElasticSearch : Getting Started

Search Lite

● GET /megacorp/employee/_search

● GET /megacorp/employee/_search?q=last_name:Smith

Page 13: 01 ElasticSearch : Getting Started

Search with Query DSL

GET /megacorp/employee/_search{ "query" : { "match" : { "last_name" : "Smith" } }}

Page 14: 01 ElasticSearch : Getting Started

More-Complicated SearchesGET /megacorp/employee/_search{"query" : { "filtered" : { "filter" : { "range" : { "age" : { "gt" : 30 } } }, "query" : { "match" : { "last_name" : "smith" } } } }}

Page 15: 01 ElasticSearch : Getting Started

Full-Text Search

GET /megacorp/employee/_search{ "query" : { "match" : { "about" : "rock climbing" } }}

Page 16: 01 ElasticSearch : Getting Started

Phrase Search

GET /megacorp/employee/_search{ "query" : { "match_phrase" : { "about" : "rock climbing" } }}

Page 17: 01 ElasticSearch : Getting Started

Highlighting Our Searches

GET /megacorp/employee/_search{ "query" : { "match_phrase" : { "about" : "rock climbing" } }, "highlight": { "fields": { "about":{} } }}

Page 18: 01 ElasticSearch : Getting Started

AnalyticsGET /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" } } } } }}

Page 19: 01 ElasticSearch : Getting Started

Referensi

● ElasticSearch, The Definitive Guide, A Distributed Real-Time Search and Analytics Engine, Clinton Gormely & Zachary Tong, O’Reilly