On Github eoaksnes / introduction-to-elasticsearch
Eirik Ola Aksnes
How well each document matches the query
By default, Elasticsearch sorts matching results by their relevance score, that is, by how well each document matches the query.wget https://download.elasticsearch.org/elasticsearch/release/... tar -zxvf elasticsearch-2.2.0.tar.gz cd elasticsearch-2.2.0/bin ./elasticsearch.sh
You can access it at http://localhost:9200 on your web browser, which returns this:
{ "status":200, "name":"Cypher", "cluster_name":"elasticsearch", "version":{ "number":"1.5.2", "build_hash":"62ff9868b4c8a0c45860bebb259e21980778ab1c", "build_timestamp":"2015-04-27T09:21:06Z", "build_snapshot":false, "lucene_version":"4.10.4" }, "tagline":"You Know, for Search" }
JSON documents!
{ "title": "Introduction to Elasticsearch", "date": "2016-04-07", "author": "Eirik Ola Aksnes" }
The act of storing data in Elasticsearch is called indexing.
$curl -X POST localhost:9200/big-one/pizza/1 --data '{ "name": "California Sunset Chicken" }' $curl -X POST localhost:9200/big-one/pizza/2 --data '{ "name": "American Bacon" }' $curl -X POST localhost:9200/big-one/pizza/3 --data '{ "name": "Classic American" }'It is much like the INSERT keyword in SQL except that, if the document already exists, the new document would replace the old. The second part indicates on which index (an index could be compared to an SQL database, though I don’t like this comparison) your query will be performed, and what is the type (a type could be compared to an SQL table, though I don’t like this comparison either) of the document. From now, I will write indices and types in orange
$curl -X GET localhost:9200/big-one/pizza/1
Result:
{ "_index":"big-one", "_type":"pizza", "_id":"1", "_version":1, "found":true, "_source":{ "name":"California Sunset Chicken" } }
$curl -X PUT localhost:9200/big-one/pizza/1 --data '{ "name":"California Sunset Chicken Awesome" }'
Result:
{ "_index":"big-one", "_type":"pizza", "_id":"1", "_version":2, "created":false }
$curl -X DELETE localhost:9200/big-one/pizza/1
Find all the pizzas that contains the word "American"
$curl -X GET localhost:9200/big-one/pizza/_search?q=American
{ "took":4, "timed_out":false, "_shards":{ "total":5, "successful":5, "failed":0 }, "hits":{ "total":2, "max_score":0.19178301, "hits":[ { "_index":"big-one", "_type":"pizza", "_id":"2", "_score":0.19178301, "_source":{ "name":"American Bacon" } }, { "_index":"big-one", "_type":"pizza", "_id":"3", "_score":0.19178301, "_source":{ "name":"Classic American" } } ] } }
Sorted by relevance!
Find the pizzas with a name that contains the word "American"
$curl -XGET localhost:9200/big-one/pizza/_search -d '{ "query":{ "match":{ "name":"American" } } }'
{ "took": 8, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 2, "max_score": 0.19178301, "hits": [ { "_index": "big-one", "_type": "pizza", "_id": "2", "_score": 0.19178301, "_source": { "name": "American Bacon" } }, { "_index": "big-one", "_type": "pizza", "_id": "3", "_score": 0.19178301, "_source": { "name": "Classic American" } } ] } }
Commonly used in addition to another database...
Github uses Elasticsearch to search 20TB data, including 1.3 billion files and 130 billion code lines
Relationship databases:With filtering, aggregations, highlightning, pagination...
Count things and summarize your data, lots of data, often on timestamped data!
Logs > Logstash > Elasticsearch > Kibana