Last active
April 25, 2018 09:21
-
-
Save marinsagovac/2fd77ad80e62cdd11220a2db2d2cc0c2 to your computer and use it in GitHub Desktop.
Elasticsearch tutorials
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=== ElasticSearch === | |
Install DEB package from: https://www.elastic.co/downloads/elasticsearch | |
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.1.1.deb | |
dpkg -i https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.1.1.deb | |
sudo add-apt-repository -y ppa:webupd8team/java | |
sudo apt-get update | |
sudo apt-get -y install oracle-java8-installer | |
java -version | |
sudo dpkg -i elasticsearch-6.1.1.deb | |
Video: https://player.vimeo.com/video/131718223 | |
sudo update-rc.d elasticsearch defaults | |
=== Configure Elastic === | |
/etc/elasticsearch/elasticsearch.yml > Server settings | |
vi /etc/elasticsearch/elasticsearch.yml [Update node.name/cluster.name value with server name (hostname)] | |
sudo /etc/init.d/elasticsearch start or sudo service elasticsearch start | |
sudo service elasticsearch status | |
=== Test === | |
http://localhost:9200 returns JSON | |
=== Info === | |
RESTful API: | |
http://localhost:9200/places/restaurant [ server/index/type ] | |
=== RESTful === | |
PUT/GET/POST | |
=== Get Postman === | |
https://dl.pstmn.io/download/latest/linux64 | |
=== Examples === | |
https://www.youtube.com/watch?v=60UsHHsKyN4 | |
==== Insert ==== | |
POST: http://localhost:9200/places/restaurants | |
JSON (application/json) | |
REQUEST: | |
Body: | |
{ | |
"name": "Marin Sagovac", | |
"description": "Desc", | |
"address": { | |
"street": "434343", | |
"city": "Zagreb", | |
"state": "Zagrebacka", | |
"zip": 10437 | |
}, | |
"location": [34.0239,-118.3924], | |
"tags":["croatian","italian","spaghetti","pasta"], | |
"rating":"4.5" | |
} | |
RESULT: | |
Result [201 Created]: | |
{ | |
"_index": "places", | |
"_type": "restaurants", | |
"_id": "SSJvs2ABv7IZImis9Z8K", | |
"_version": 1, | |
"result": "created", | |
"_shards": { | |
"total": 2, | |
"successful": 1, | |
"failed": 0 | |
}, | |
"_seq_no": 0, | |
"_primary_term": 1 | |
} | |
==== Query get result ==== | |
POST: http://localhost:9200/places/restaurants/_search | |
REQUEST: | |
{ | |
"query": { | |
"match_all": {} | |
} | |
} | |
RESULT: | |
{ | |
"took": 72, | |
"timed_out": false, | |
"_shards": { | |
"total": 5, | |
"successful": 5, | |
"skipped": 0, | |
"failed": 0 | |
}, | |
"hits": { | |
"total": 1, | |
"max_score": 1, | |
"hits": [ | |
{ | |
"_index": "places", | |
"_type": "restaurants", | |
"_id": "SSJvs2ABv7IZImis9Z8K", | |
"_score": 1, | |
"_source": { | |
"name": "Marin Sagovac", | |
"description": "Desc", | |
"address": { | |
"street": "434343", | |
"city": "Zagreb", | |
"state": "Zagrebacka", | |
"zip": 10437 | |
}, | |
"location": [ | |
34.0239, | |
-118.3924 | |
], | |
"tags": [ | |
"croatian", | |
"italian", | |
"spaghetti", | |
"pasta" | |
], | |
"rating": "4.5" | |
} | |
} | |
] | |
} | |
} | |
==== Query any search ==== | |
POST: http://localhost:9200/places/restaurants/_search | |
REQUEST: | |
{ | |
"query": { | |
"query_string": { | |
"query": "croatian" | |
} | |
} | |
} | |
RESULT: | |
Searched for any string "croatian": | |
{ | |
"took": 5, | |
"timed_out": false, | |
"_shards": { | |
"total": 5, | |
"successful": 5, | |
"skipped": 0, | |
"failed": 0 | |
}, | |
"hits": { | |
"total": 1, | |
"max_score": 0.2876821, | |
"hits": [ | |
{ | |
"_index": "places", | |
"_type": "restaurants", | |
"_id": "SSJvs2ABv7IZImis9Z8K", | |
"_score": 0.2876821, | |
"_source": { | |
"name": "Marin Sagovac", | |
"description": "Desc", | |
"address": { | |
"street": "434343", | |
"city": "Zagreb", | |
"state": "Zagrebacka", | |
"zip": 10437 | |
}, | |
"location": [ | |
34.0239, | |
-118.3924 | |
], | |
"tags": [ | |
"croatian", | |
"italian", | |
"spaghetti", | |
"pasta" | |
], | |
"rating": "4.5" | |
} | |
} | |
] | |
} | |
} | |
==== Query any search ==== | |
POST: http://localhost:9200/places/restaurants/_search | |
REQUEST: | |
{ | |
"query": { | |
"query_string": { | |
"query": "croatian", | |
"fields": ["tags"] | |
} | |
} | |
} | |
Search in field "tags" for matched string "croatian". | |
Results done. | |
==== Query any matched search ==== | |
POST: http://localhost:9200/places/restaurants/_search | |
REQUEST: | |
{ | |
"query": { | |
"query_string": { | |
"query": "croatian", | |
"fields": ["tags"] | |
} | |
} | |
} | |
Search in field "tags" for matched string "croatian". | |
Results done. | |
No result matched if you search '"query": "croatia",' | |
==== Advanced search ==== | |
REQUEST: | |
{ | |
"query":{ | |
"filtered": { | |
"filter": { | |
"range": { | |
"rating": { | |
"gte": 4.0 | |
} | |
} | |
}, | |
"query": { | |
"query_string": { | |
"query": "croatia", | |
"fields": ["tags"] | |
} | |
} | |
} | |
} | |
} | |
This is deprecated in ES 5.0, use [bool > must, filter]: | |
https://www.elastic.co/guide/en/elasticsearch/guide/current/_ranges.html | |
{ | |
"query": { | |
"bool": { | |
"must": { | |
"multi_match": { | |
"operator": "and", | |
"fields": [ | |
"tags" | |
], | |
"query": "croatia" | |
} | |
}, | |
"filter": { | |
"terms": { | |
"rating": [ | |
"4.5" | |
] | |
} | |
} | |
} | |
} | |
} | |
Results done. | |
==== Remove data ==== | |
Request: DELETE | |
URI: POST: http://localhost:9200/places | |
http://localhost:9200/places/?timeout=5m | |
If it can't delete a data: | |
"type": "cluster_block_exception", | |
"reason": "blocked by: [FORBIDDEN/12/index read-only / allow delete (api)]; | |
Then: | |
curl -XPUT -H "Content-Type: application/json" http://localhost:9200/_all/_settings -d '{"index.blocks.read_only_allow_delete": null}' | |
response: | |
{"acknowledged":true} | |
Try delete now. | |
==== Other queries ==== | |
curl -X POST "http://localhost:9200/articles/_search?pretty=true" -d ' | |
{ | |
"query" : { "query_string" : {"query" : "*"} }, | |
"facets" : { | |
"tags" : { "terms" : {"field" : "author"} } | |
} | |
} | |
' | |
curl -X POST "http://localhost:9200/_search?pretty=true" -d ' | |
{ | |
"facets" : { | |
"tags" : { "terms" : {"field" : "network.platform"} }, | |
"size" : 60 | |
}, | |
"size" : 0 | |
} | |
' | |
==== PUT ==== | |
PUT my_index | |
{ | |
"mappings": { | |
"my_type": { | |
"properties": { | |
"city": { | |
"type": "text", | |
"fields": { | |
"raw": { | |
"type": "keyword" | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
PUT my_index/my_type/1 | |
{ | |
"city": "New York" | |
} | |
PUT my_index/my_type/2 | |
{ | |
"city": "York" | |
} | |
GET my_index/_search | |
{ | |
"query": { | |
"match": { | |
"city": "york" | |
} | |
}, | |
"sort": { | |
"city.raw": "asc" | |
}, | |
"aggs": { | |
"Cities": { | |
"terms": { | |
"field": "city.raw" | |
} | |
} | |
} | |
} | |
==== PUT - multiple ==== | |
PUT my_index | |
{ | |
"mappings": { | |
"my_type": { | |
"properties": { | |
"text": { | |
"type": "text", | |
"fields": { | |
"english": { | |
"type": "text", | |
"analyzer": "english" | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
PUT my_index/my_type/1 | |
{ "text": "quick brown fox" } | |
PUT my_index/my_type/2 | |
{ "text": "quick brown foxes" } | |
GET my_index/_search | |
{ | |
"query": { | |
"multi_match": { | |
"query": "quick brown foxes", | |
"fields": [ | |
"text", | |
"text.english" | |
], | |
"type": "most_fields" | |
} | |
} | |
} | |
==== PUT - multiple ==== | |
==== Cheats ==== | |
http://elasticsearch-cheatsheet.jolicode.com/ | |
curl http://10.8.88.97:9200/_cat/templates |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment