Basic Examples of ElasticSearch Uses

Share It!

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

elastic-search-logo_opt

  • 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"
    }
        } ]
      }
    }
    
  • No Responses

    Leave a Reply

    Your email address will not be published.