A bunch of useful commands for managing the Elasticsearch server. You can find all of them somewhere in the Elasticsearch documentation, but I find quite useful having all of them gathered in a single post.
Get cluster status
Retrieve information about the cluster: state, number of nodes, shards, ETC.
curl 'localhost:9200/_cat/health?v'
or just using your web browser:
http://localhost:9200/_cat/health?v
Create Index
For creating an index we just need to use the PUT verb in the root directory followed by the name of the new Index we want to create, in this case project1. You can also add the param ?pretty
afterwards to get a pretty JSON response (if any).
curl -XPUT 'localhost:9200/project1?pretty'
Get Index Settings
Retrieve Index configuration: creation_date, default filters and analyzers, ETC.
curl 'localhost:9200/project1/_settings'
or just using your web browser:
http://localhost:9200/project1/_settings
Delete index
For deleting an existing Index:
curl -XDELETE 'localhost:9200/project1?pretty'
Get all indexes
Indexes are the equivalent to databases in Elasticsearch. For listing all existing indexes just run the following command in console:
curl 'localhost:9200/_cat/indices?v'
or just using your web browser:
http://localhost:9200/_cat/indices?v
Shutdown Elasticsearch server
Command for turning off the server:
curl -XPOST 'http://localhost:9200/_shutdown'
Indexing documents (Create rows)
In Elasticsearch the “types” are equivalent to a database “tables” and the “documents” are equivalent to database “rows“. Knowing this will make easier to understand the structure. Now we are adding the user John Doe (_id = 1) to the “type” users.
curl -XPUT 'localhost:9200/project1/users/1?pretty' -d '{"name": "John Doe"}'
Searching
There are many ways for querying and filtering data with Elasticsearch, here you will find some of the most important ones. Also is worth having basic notions about analyzers and how they work in full-text search.
Finding exact values
The filter for searching exact valued is called “term“. It should be used as often as possible because it is really fast (cached). It can search for exact values in numbers, Booleans, dates and text fields.
curl -XGET 'localhost:9200/project1/users/_search' -d '{"query":{"filtered":{"filter":{"term":{"_id":1}}}}}'
Making Full-text queries
The Full-text queries use the keyword “match” to find a word within a phrase. The way that the phrases are split depends on the Analyzer defined (Usually the standard). Remember that filters like the one showed in the previous step could also be added to this query.
curl -XGET 'localhost:9200/project1/users/_search' -d '{"query":{"match":{"name":"John"}}}'
Getting documents by ID
For retrieving the user we just created in the section Indexing documents:
curl -XGET 'localhost:9200/project1/users/1?pretty'
We should get something like this:
{ "_index" : "project1", "_type" : "users", "_id" : "1", "_version" : 1, "found" : true, "_source" : { "name": "John Doe" } }
Counting documents
We can count the existing documents within a cluster or Index using the _count param:
curl -XGET 'http://localhost:9200/_count?pretty' -d '{"query":{"term":{"user":"John Doe"}}}'
Response:
{ "count" : 1, "_shards" : { "total" : 5, "successful" : 5, "failed" : 0 } }
Deleting documents
For removing indexed documents:
curl -XDELETE 'http://localhost:9200/project1/users/1'