Basic Examples of ElasticSearch Uses
By Anuket Jain On 26 April 2015 In Linux
Following examples of ElasticSearch will help to add, get and search data in ElasticSearch.
curl -XPUT http://localhost:9200/mybucket
Output:
{"acknowledged":true}
Use following commands to add some data in ElasticSearch.
Command 1:
curl -XPUT 'http://localhost:9200/mybucket/user/tony' -d '{ "name" : "Dennis R" }'
Output:
{"_index":"mybucket","_type":"user","_id":"tony","_version":1,"created":true}
Command 2:
curl -XPUT 'http://localhost:9200/mybucket/post/1' -d ' { "user": "Dennis", "postDate": "01-15-2015", "body": "This is Demo Post 1 in ElasticSearch" , "title": "Demo Post 1" }'
Output:
{"_index":"mybucket","_type":"post","_id":"1","_version":1,"created":true}
Command 3:
curl -XPUT 'http://localhost:9200/mybucket/post/2' -d ' { "user": "Jonny", "postDate": "01-15-2015", "body": "This is Demo Post 2 in ElasticSearch" , "title": "Demo Post 2" }'
Output:
{"_index":"mybucket","_type":"post","_id":"2","_version":1,"created":true}
Use following command to GET data from ElasticSearch and read the output.
curl -XGET 'http://localhost:9200/mybucket/user/tony?pretty=true' curl -XGET 'http://localhost:9200/mybucket/post/1?pretty=true' curl -XGET 'http://localhost:9200/mybucket/post/2?pretty=true'
Use following command to search data from elastic search. Below command will search all data assiciated with user tony.
curl 'http://localhost:9200/mybucket/post/_search?q=user:Dennis&pretty=true'
Output:
{ "took" : 10, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "failed" : 0 }, "hits" : { "total" : 1, "max_score" : 1.0, "hits" : [ { "_index" : "mybucket", "_type" : "post", "_id" : "1", "_score" : 1.0, "_source": { "user": "Dennis", "postDate": "01-15-2015", "body": "This is Demo Post 1 in ElasticSearch" , "title": "Demo Post 1" } } ] } }
Enjoy it!
No Responses