Elasticsearch
elasticsearch
elasticsearch.org Open Source Distributed Real Time Search & Analytics
elasticsearch - http://www.elasticsearch.org/
Install
Elastic Search:
rpm --import http://packages.elasticsearch.org/GPG-KEY-elasticsearch cat > /etc/yum.repos.d/elastic.repo << "EOF" [elasticsearch-1.1] name=Elasticsearch repository for 1.1.x packages baseurl=http://packages.elasticsearch.org/elasticsearch/1.1/centos gpgcheck=1 gpgkey=http://packages.elasticsearch.org/GPG-KEY-elasticsearch enabled=1 EOF yum install elasticsearch # start on boot sudo /sbin/chkconfig --add elasticsearch # start manually sudo service elasticsearch start # see if it runs curl -XGET 'http://localhost:9200' # Create Stage Index: curl -XPUT 'http://localhost:9200/somestage'
Paths:
Data location: /var/lib/elasticsearch Config: /etc/sysconfig/elasticearch Exe: /usr/share/elasticsearch Startup: /etc/init.d/elasticsearch Config: /etc/elasticsearch/*.yml
Search:
curl -XGET 'http://localhost:9200/_search'
Configuration
- ES_HEAP_SIZE environment variable allows to set the heap memory that will be allocated to elasticsearch java process. It will allocate the same value to both min and max values.
curl localhost:9200/_nodes/process?pretty
config/elasticsearch.yml:
bootstrap.mlockall: true
References:
- Configuration - http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/setup-configuration.html
- ElasticSearch and Logstash Tuning – jablonskis - http://jablonskis.org/2013/elasticsearch-and-logstash-tuning/index.html
CRUD
Create
curl -XPUT "http://localhost:9200/movies/movie/1" -d' { "title": "title1", "director": "director1", "year": 100 }'
{"_index":"movies","_type":"movie","_id":"1","_version":1,"created":true}
Read
curl -XGET "http://localhost:9200/movies/movie/1"
Update
Same as create, but return json will have:
{"_index":"movies","_type":"movie","_id":"1","_version":2,"created":false}
Delete
curl -XDELETE "http://localhost:9200/movies/movie/1"
Search
http://localhost:9200/_search - Search across all indexes and all types. http://localhost:9200/movies/_search - Search across all types in the movies index. http://localhost:9200/movies/movie/_search - Search explicitly for documents of type movie within the movies index.
Basic free text search
search for the word "kill"
curl -XPOST "http://localhost:9200/_search" -d' { "query": { "query_string": { "query": "kill" } } }'
Search only in titles:
curl -XPOST "http://localhost:9200/_search" -d' { "query": { "query_string": { "query": "ford", "fields": ["title"] } } }'
Filter
Query with filter:
curl -XPOST "http://localhost:9200/_search" -d' { "query": { "filtered": { "query": { "query_string": { "query": "drama" } }, "filter": { "term": { "year": 1962 } } } } }'
Just filter:
curl -XPOST "http://localhost:9200/_search" -d' { "query": { "filtered": { "query": { "match_all": { } }, "filter": { "term": { "year": 1962 } } } } }'
curl -XPOST "http://localhost:9200/_search" -d' { "query": { "constant_score": { "filter": { "term": { "year": 1962 } } } } }'
Index
In order to index a first JSON object we make a PUT request to the REST API to a URL made up of the index name, type name and ID. That is:
http://localhost:9200/<index>/<type>/[<id>]
Tutorials
ElasticSearch 101 - A getting started tutorial - http://joelabrahamsson.com/elasticsearch-101/