Basic Examples of ElasticSearch Uses

Following examples of ElasticSearch will help to add, get and search data in ElasticSearch.

Examples of ElasticSearch

  • Creating Bucket:
     curl -XPUT http://localhost:9200/mybucket
    

    Output:

    {"acknowledged":true}
    
  • Adding Data to ElasticSearch:
    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}
    
  • Fetching Data from ElasticSearch:
    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'
    
  • Searching in ElasticSearch:
    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

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

    The reCAPTCHA verification period has expired. Please reload the page.