Skip to content

Instantly share code, notes, and snippets.

@okram999
Last active October 31, 2018 15:44
Show Gist options
  • Save okram999/89328dfcae634dee40cc1fe143d452cf to your computer and use it in GitHub Desktop.
Save okram999/89328dfcae634dee40cc1fe143d452cf to your computer and use it in GitHub Desktop.
ElasticSearch Notes
```
Index is like a database
Index can be splited into shared shared accross multiple nodes (it can have replicas)
A node can have more than 1 shards
Each shards is an instance of lucene - and they hence scales horizontally
Schema of the documents are defined by -- "the Type"
Inverted indices - quickly maps the search
Calcualations of shards in a cluster:
Index configured with 5 primary & 3 replicas ==> 20 shards
```
Mapping -- the schema definition (sometimes you need to customize)
##Using Curl to add a single document to the ES
curl -H "Content-Type: application/json" -XPUT 127.0.0.1:9200/movies/movie/109487 -d '
{ "genre" : ["IMAX","Sci-Fi"],
"title" : "Interstellar",
"year" : 2014
}'
## get data from an index: movies of "Type" movie
curl -H "Content-Type: application/json" -XGET 127.0.0.1:9200/movies/movie/_search?pretty
#bulk upload
157 curl -H "Content-Type: application/json" -XPUT 127.0.0.1:9200/_bulk?pretty --data-binary @movies.json
# retrieve all movies
158 curl -H "Content-Type: application/json" -XGET 127.0.0.1:9200/movies/_search?pretty
##################### Some commands
First the document was created by using the http 'PUT':
curl -H "Content-Type: application/json" -XPUT 127.0.0.1:9200/movies/movie/109487 -d '{ "genre" : ["IMAX","Sci-Fi"],"title" : "Interstellar","year" : 2014}'
Then the bulk import failed as the document already exist with this :
curl -H "Content-Type: application/json" -XPUT 127.0.0.1:9200/_bulk?pretty --data-binary @movies.json
Then used the http 'POST' to update the document using this:
curl -H "Content-Type: application/json" -XPOST 127.0.0.1:9200/movies/movie/109487/_update -d '{
"doc" : {
"title": "Interstellar Woo"
}
}'
Inserting again:
curl -H "Content-Type: application/json" -XPUT 127.0.0.1:9200/movies/movie/109487?pretty -d '
{ "genre" : ["IMAX","Sci-Fi"],"title" : "Interstellar","year" : 2014}'
########################################################
curl -H "Content-Type: application/json" -XDELETE 127.0.0.1:9200/movies/_search?q=Dark << This a search
#Delete a document
curl -H "Content-Type: application/json" -XDELETE 127.0.0.1:9200/movies/movie/123?pretty
###Handling Concurrency:
Uses version for documents
retry_on_conflicts=N "to automatically retry"
### Using Analyzers
Keywords -- will do exact matching when searching (so no analyzing)
text -- will allow analyzing or partial matching
##Search
curl -H "Content-Type: application/json" -XGET 127.0.0.1:9200/movies/movie/_search?pretty -d '
{ "query" : { "match" : { "title" : "Star Trek" }}}'
The above search return multiple movies as title is not set as a keyword
{
"took" : 36,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 0.77904123,
"hits" : [
{
"_index" : "movies",
"_type" : "movie",
"_id" : "122886",
"_score" : 0.77904123,
"_source" : {
"id" : "122886",
"title" : "Star Wars: Episode VII - The Force Awakens",
"year" : 2015,
"genre" : [
"Action",
"Adventure",
"Fantasy",
"Sci-Fi",
"IMAX"
]
}
},
{
"_index" : "movies",
"_type" : "movie",
"_id" : "135569",
"_score" : 0.5753642,
"_source" : {
"id" : "135569",
"title" : "Star Trek Beyond",
"year" : 2016,
"genre" : [
"Action",
"Adventure",
"Sci-Fi"
]
}
}
]
}
}
#### Cannot remap an index. For this the index will need to be deleted and will recreated
#delete an index
curl -H "Content-Type: application/json" -XDELETE 127.0.0.1:9200/movies
#now searching in the deleted index gives 404 error
curl -H "Content-Type: application/json" -XGET 127.0.0.1:9200/movies/movie/_search?pretty -d '
{ "query" : { "match" : { "title" : "Star Trek" }}}'
{
"error" : {
"root_cause" : [
{
"type" : "index_not_found_exception",
"reason" : "no such index",
"resource.type" : "index_or_alias",
"resource.id" : "movies",
"index_uuid" : "_na_",
"index" : "movies"
}
],
"type" : "index_not_found_exception",
"reason" : "no such index",
"resource.type" : "index_or_alias",
"resource.id" : "movies",
"index_uuid" : "_na_",
"index" : "movies"
},
"status" : 404
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment