Tuesday, May 1, 2018

Elastic Search Example: Part 1

Donghuas-MacBook-Air:elasticsearch-6.2.4 donghua$ curl -XGET -u elastic:elastic 'localhost:9200/?pretty'
{
  "name" : "0rArjNg",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "dZYuMdDkQICV_oS2-nvmpQ",
  "version" : {
    "number" : "6.2.4",
    "build_hash" : "ccec39f",
    "build_date" : "2018-04-12T20:37:28.497551Z",
    "build_snapshot" : false,
    "lucene_version" : "7.2.1",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

Donghuas-MacBook-Air:elasticsearch-6.2.4 donghua$ curl -XPUT -H 'Content-Type: application/json' -u elastic:elastic 'localhost:9200/blogs/post/1' -d '{
> "Title" : "title 1",
> "Body" : "blog, blog",
> "user" : "donghua",
> "date" : "1/5/2018"
> }'
{"_index":"blogs","_type":"post","_id":"1","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}Donghuas-MacBook-Air:elasticsearch-6.2.4 donghua$ 
Donghuas-MacBook-Air:elasticsearch-6.2.4 donghua$ 

Donghuas-MacBook-Air:elasticsearch-6.2.4 donghua$ curl -XDELETE -H 'Content-Type: application/json' -u elastic:elastic 'localhost:9200/blogs/post/1'
{"_index":"blogs","_type":"post","_id":"1","_version":3,"result":"deleted","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":2,"_primary_term":1}Donghuas-MacBook-Air:elasticsearch-6.2.4 donghua$ 


Donghuas-MacBook-Air:elasticsearch-6.2.4 donghua$ curl -XPUT -H 'Content-Type: application/json' -u elastic:elastic 'localhost:9200/blogs/post/1?pretty' -d '{
> "Title" : "title 2",
> "Body" : "blog, blog blogs",
> "user" : "donghua",
> "date" : "1/5/2018"
> }'
{
  "_index" : "blogs",
  "_type" : "post",
  "_id" : "1",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}


Donghuas-MacBook-Air:elasticsearch-6.2.4 donghua$ curl -XGET -u elastic:elastic 'localhost:9200/blogs/post/1?pretty'
{
  "_index" : "blogs",
  "_type" : "post",
  "_id" : "1",
  "_version" : 4,
  "found" : true,
  "_source" : {
    "Title" : "title 1",
    "Body" : "blog, blog",
    "user" : "donghua",
    "date" : "1/5/2018"
  }
}


Donghuas-MacBook-Air:elasticsearch-6.2.4 donghua$ curl -XGET -u elastic:elastic 'localhost:9200/blogs/_search?pretty'
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "blogs",
        "_type" : "post",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "Title" : "title 2",
          "Body" : "blog, blog blogs",
          "user" : "donghua",
          "date" : "1/5/2018"
        }
      },
      {
        "_index" : "blogs",
        "_type" : "post",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "Title" : "title 1",
          "Body" : "blog, blog",
          "user" : "donghua",
          "date" : "1/5/2018"
        }
      }
    ]
  }
}


Donghuas-MacBook-Air:elasticsearch-6.2.4 donghua$ curl -XPUT -H 'Content-Type: application/json' -u elastic:elastic 'localhost:9200/blogs/post/?pretty' -d '{
> "Title" : "title 3",
> "Body" : "blog without docid",
> "user" : "donghua2",
> "date" : "1/5/2018"
> }'
{
  "error" : "Incorrect HTTP method for uri [/blogs/post/?pretty] and method [PUT], allowed: [POST]",
  "status" : 405
}

Donghuas-MacBook-Air:elasticsearch-6.2.4 donghua$ 
Donghuas-MacBook-Air:elasticsearch-6.2.4 donghua$ curl -XPOST -H 'Content-Type: application/json' -u elastic:elastic 'localhost:9200/blogs/post/?pretty' -d '{
> "Title" : "title 3",
> "Body" : "blog without docid",
> "user" : "donghua2",
> "date" : "1/5/2018"
> }'
{
  "_index" : "blogs",
  "_type" : "post",
  "_id" : "M1i9GGMBNSS82bObMFNL",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}
Donghuas-MacBook-Air:elasticsearch-6.2.4 donghua$