ES入门2-CRUD

2018-05-06  本文已影响0人  加碘盐_ed6c

Index API

Index API提供了几种创建Index方式。
简单方式创建index

curl -XPUT ‘http://localhost:9200/twitter/'

在创建索引的时候指定分片和副本数量参数,参数格式采用JSON格式。

curl -XPUT 'http://localhost:9200/twitter/' -d '{
  "settings":{
      "index":{
         "number_of_shards":3,
         "number_of_replicas":2
      }
  }
}’

或者简化为

curl -XPUT 'http://localhost:9200/twitter' -d '{
  "settings":{
      "number_of_shards":3,
      "number_of_replicas":2
  }
}'

另外的一种create index方式

curl -XPUT 'http://localhost:9200twitter/tweet/1/_create' -d '{
  "user":"kimchy",
  "post_date":"2009-11-11T14:12:12",
  "message":"hello,world"
}'

以上代码如果不指定id(即1),系统会自动生成id。


Get API

查询索引为twitter,索引类型为tweet,id为1的数据。

curl -XGET 'http://localhost:9200/twitter/tweet/1'

查询结果返回为:

{
    "_index" : "twitter",
    "_type" : "tweet",
    "_id" : "1",
    "_version" : 1,
    "found": true,
    "_source" : {
        "user" : "kimchy",
        "postDate" : "2009-11-15T14:12:12",
        "message" : "trying out Elasticsearch"
    }
}

URI Search

查询为twitter,类型为tweet,user为kimch的文档信息。

curl -XGET 'http://localhost:9200/twitter/tweet/_search?q=user:kimchy'

返回结果为:

{
    "_shards":{
        "total" : 5,
        "successful" : 5,
        "failed" : 0
    },
    "hits":{
        "total" : 1,
        "hits" : [
            {
                "_index" : "twitter",
                "_type" : "tweet",
                "_id" : "1",
                "_source" : {
                    "user" : "kimchy",
                    "postDate" : "2015-05-05T14:12:12",
                    "message" : "trying out Elasticsearch"
                }
            }
        ]
    }
}

_search 的URI参数还包括其它一些,如from、size、analyzer、sort、fields、df等


Request Body Search

查询为twitter,类型为tweet,userkimchiy满足条件的10条文档信息。

  curl -XGET 'http://localhost:9200/twitter/tweet/_search'  -d '{
 "from" : 0, "size" : 10,
 "query" : {
  "term" : { "user" : "kimchy" }
  }
  }
 '

_searchRequest Body 参数还包括其它一些,如sort、fields等

详细文档大家请看: http://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html


Update API

比如插入”twitter”的索引,并且索引类型为tweet,id为2的记录。如下代码:

curl -XPUT  'http://localhost:9200/twitter/tweet/2' -d '{
       "user":"kimchy",
      "post_date":"2012-12-12",
       “message”:”trying out ElasticSearch!”
}’

添加成功后,其会返回操作状态,索引、类型、id等信息如下代码返回信息。

{
     "ok" : true,
     "_index" : "twitter",
     "_type" : "tweet",
     "_id" : "2"
}

当然也可以不指定id对数据进行增加,系统会自动增加一个id,我们要用POST命令来实现。

curl -XPOST  'http://localhost:9200/twitter/tweet' -d '{
       "user":"jim",
      "post_date":"2015-05-12",
       “message”:”trying out ElasticSearch!”
}’

Delete API

通过id删除文档 下面的例子是删除索引名为twitter,类型为tweet,id为1的文档:

curl -XDELETE   'http://localhost:9200/twitter/tweet/1'

结果返回

{
    "found" : true,
    "_index" : "twitter",
    "_type" : "tweet",
    "_id" : "1",
    "_version" : 2
}

通过查询条件删除文档

curl -XDELETE   'http://localhost:9200/twitter/tweet/_query' -d '{
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}
'
上一篇 下一篇

猜你喜欢

热点阅读