Elasticsearch
- is a distributed, JSON-based search and analytics engine designed for horizontal scalability, maximum reliability, and easy management.
Installation
homebrew
Install
homepage - https://www.elastic.co/
0. display available versions
brew search elasticsearch1. install elastic search via home-brew
brew install <elasticsearch version>2. optional load sample son data
curl -XPOST ‘localhost:9200/bank/account/_bulk?pretty&refresh’ —data-binary “@accounts.json”curl ‘localhost:9200/_cat/indices?v’Start Stop Restart
1. start elastic search (runs on default port 9002)
brew services start <elasticsearch version>Or, if you don’t want/need a background service you can just run:
<elasticsearch version>or navigate to bin directory and run in terminal
./elasticsearch2. stop or restart
brew services stop <elasticsearch version>brew services restart <elasticsearch version>
Commands
Click here to expand...
optional load sample son data
curl -XPOST ‘localhost:9200/bank/account/_bulk?pretty&refresh’ —data-binary “@accounts.json”curl ‘localhost:9200/_cat/indices?v’get health of cluster
curl -XGET ‘localhost:9200/_cat/health?v&pretty’get nodes of cluster
curl -XGET ‘localhost:9200/_cat/nodes?v&pretty’lists all indices
curl -XGET ‘localhost:9200/_cat/indices?v&pretty’create index named ‘customer’
curl -XPUT ‘localhost:9200/customer?pretty&pretty’Let’s index a simple customer document into the customer index,“external” type, with an ID of 1 as follows:
curl -XPUT ‘localhost:9200/customer/external/1?pretty&pretty’ -d’{“name”: “John Doe”}’this index a document without explicit id, ElasticSearch will generate a random ID
curl -XPOST ‘localhost:9200/customer/external?pretty&pretty’ -d’{“name”: “Jane Doe”}’updates a document from index by id
curl -XPOST ‘localhost:9200/customer/external/1/_update?pretty&pretty’ -d’{“doc”: { “name”: “Jane Doe” }}’get the document we just indexed
curl -XGET ‘localhost:9200/customer/external/1?pretty&pretty’delete the index
curl -XDELETE ‘localhost:9200/customer?pretty&pretty’review of commands
PUT /customerPUT /customer/external/1{“name”: “John Doe”}GET /customer/external/1DELETE /customerfollows a pattern
<REST Verb> /<Index>/<Type>/<ID>search
curl -XGET ‘localhost:9200/bank/_search?q=*&sort=account_number:asc&pretty’curl -XGET ‘localhost:9200/bank/_search?pretty’ -d’{“query”: { “match_all”: {} },“sort”: [{ “account_number”: “asc” }]}’‘match_all’ searches all in specified index ‘size’ - default to 10 if not specified
curl -XGET ‘localhost:9200/bank/_search?pretty’ -d’{“query”: { “match_all”: {} },“size”: 1}’search
curl -XGET ‘localhost:9200/bank/_search?pretty’ -d’{“query”: { “match_all”: {} },“sort”: { “balance”: { “order”: “desc” } }}’returns account numbered 20
curl -XGET ‘localhost:9200/bank/_search?pretty’ -d’{“query”: { “match”: { “account_number”: 20 } }}’returns all accounts containing “mill” and “lane” in the address
curl -XGET ‘localhost:9200/bank/_search?pretty’ -d’{“query”: {“bool”: {“must”: [{ “match”: { “address”: “mill” } },{ “match”: { “address”: “lane” } }]}}}’returns all accounts containing “mill” or “lane” in the address
curl -XGET ‘localhost:9200/bank/_search?pretty’ -d’{“query”: {“bool”: {“should”: [{ “match”: { “address”: “mill” } },{ “match”: { “address”: “lane” } }]}}}’returns all accounts that contain neither “mill” nor “lane” in the address
curl -XGET ‘localhost:9200/bank/_search?pretty’ -d’{“query”: {“bool”: {“must_not”: [{ “match”: { “address”: “mill” } },{ “match”: { “address”: “lane” } }]}}}’returns all accounts that have balance between 20000 and 30000 inclusive
curl -XGET ‘localhost:9200/bank/_search?pretty’ -d’{“query”: {“bool”: {“must”: { “match_all”: {} },“filter”: {“range”: {“balance”: {“gte”: 20000,“lte”: 30000}}}}}}’aggregate - groups all the accounts by state, and then returns the, top 10 (default) states sorted by count descending (also default)
curl -XGET ‘localhost:9200/bank/_search?pretty’ -d’{“size”: 0,“aggs”: {“group_by_state”: {“terms”: {“field”: “state.keyword”}}}}‘