ElasticSearch-API-Index的使用亲测

ElasticSearch-API-Index

索引创建API允许初始化一个索引。ElasticSearch对多重索引提供了支持,包括跨多个索引执行操作。每个索引在创建时可以让一个特定的设置项与其关联。

  • 最简单的方式创建索引

    curl -XPUT ‘http://localhost:9200/twitter/'
  • 在创建索引的时候指定分片和副本数量,参数格式采用YAML格式

    curl -XPUT ‘http://localhost:9200/twitter/‘ -d ‘
    index:
      number_of_shards:3
      number_of_replicas:2
  • 在创建索引的时候指定分片和副本数量参数,参数格式采用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
      }
    }’

    *请注意,你不需要在settings项中显示的指定index。

  • 索引创建API可以接受一个或者一组映射选项

    curl -XPOST localhost:9200/test -d ‘{
      “settings”:{
          “number_of_shards:1
      },
      “mappings”:{
          “type1”:{
              “_source”:{“enabled:false},
              “preperties”:{
                  “field1”:{“type”:”string”,
                          ”index”:”not_analyzed”
                  }
              }
          }
      }
    }’
  • REST风格的插入方式。

    curl -XPOST http://localhost:9200/索引名称/索引类型/id -d JSON格式的参数

    比如插入”twitter”的索引,并且索引类型为tweet

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

    添加成功后,其会返回操作状态,索引、类型、id等信息如上例中返回信息

    {
      "ok" : true,
      "_index" : "twitter",
      "_type" : "tweet",
      "_id" : "1"
    }
  • 每一个被索引的文档都会有一个版本号,被关联的版本号会作为index API的请求的响应信息一部分返回回来。因此,我们可以在对索引操作的时候,指定特定的版本号,操作对应版本的文档。例如

    curl -XPUT ‘localhost:9200/twitter/tweet/1?version=2’ -d ‘{
      “message”:”elasticsearch now has versioning support,double cool!”
    }’

    *注意 1.版本控制完全是实时的,不会影响接近实时方面的查询操作。如果版本已经被提供了,那么操作执行检查所有的版本。 2.默认情况下,版本从1开始,自增因子为1。

  • op_type。索引操作也接受参数op_type,用来强制创建索引的操作。如果索引尚未建立的时候,可以接受这样的行为,但是当文档的缩影已经存在的时候,该操作会将失败。 下面是一个op_type参数的例子

    curl -XPUT ‘http://localhost:9200/twitter/tweet/1?op_type=create’ -d ‘{
      “user”:”kimchy”,
      “post_date”:”2014-12-05T14:12:12”,
      “message”:”trying out Elastic Searche”
    }’

    另外的一种create方式

    curl -XPUT ‘http://localhost:9200/twitter/tweet/1/_create’ -d ‘{
      “user”:”kimchy”,
      “post_date”:”2009-11-11T14:12:12”,
      “message”:”hello,world”
    }’
  • 自动生成Id.在创建一个Index的操作中,如果没有指定id,系统将会自动地为其生成一个id.

    curl -XPOST ‘http://localhost:9200/twitter/tweet/‘ -d ‘{
      “user”:”kimchy”,
      “post_date”:”2013-11-12T12:12:12”,
      “message”:”Hello,world”
    }’

    操作响应的结果如下

    {
      “ok”:true,
      “_index”:”twitter”,
      “_type”:”tweet”,
      “_id”:”6a8ca01c-7896-48e9-81cc-9f70661fcb32”
    }
  • 路由(Routing)。默认情况下,分片或路由是通过计算文档的hash值来控制的,为了更明确的控制值,送入路由器使用hash函数计算hash值的操作可以通过routing参数来控制。

    curl -XPOST ‘http://localhost:9200/twitter/tweet?routing=kimchy’ -d ‘{
      “user”:”kimchy”,
      “post_date”:”2014-12-12T12:12:12”
    }’
  • TTL。一个文档建立索引的时候,能够为其指定ttl。

    curl -XPUT 'http://localhost:9200/twitter/tweet/1?ttl=86400000' -d '{
      "user""kimchy",
      "message""Trying out elasticsearch, so far so good?"
    }'
    curl -XPUT 'http://localhost:9200/twitter/tweet/1?ttl=1d' -d '{
      "user""kimchy",
      "message""Trying out elasticsearch, so far so good?"
    }'
    curl -XPUT 'http://localhost:9200/twitter/tweet/1' -d '{
      "_ttl""1d",
      "user""kimchy",
      "message""Trying out elasticsearch, so far so good?"
    }'
        
        



测试过的语句:
1. 创建索引:相当于创建一个数据库
curl -XPUT 'http://192.168.8.98:9200/twitter/'

2.  在创建索引的时候指定分片和副本数量,参数格式采用YAML格式
报错
3. 在创建索引的时候指定分片和副本数量参数,参数格式采用JSON格式
curl -XPUT 'http://192.168.8.98:9200/twitter1/' -d '{
  "settings":{
      "index" :{
          "number_of_shards":3,
          "number_of_replicas":2
      }
  }
}'
比如插入”twitter”的索引,并且索引类型为tweet
  curl -XPUT 'http://192.168.8.98:9200/twitter/tweet/1' -d '{
  "user":"kimchy",
  "post_date":"2012-12-12",
  "message":"trying out ElasticSearch!"
}'

D3BB9AE5-37C4-4EAE-9A31-E5AFCDCEECF8.png

9F0133CC-804C-46A2-BCA4-8B48971560EE.png
每一个被索引的文档都会有一个版本号,被关联的版本号会作为index API的请求的响应信息一部分返回回来。因此,我们可以在对索引操作的时候,指定特定的版本号,操作对应版本的文档。例如:
curl -XPUT '192.168.8.98:9200/twitter/tweet/1?version=1' -d '{
  "message":"elasticsearch now has versioning support,double cool!"
}'

EAF99F3F-4DCA-458F-8DB8-35B6BDC03249.png
E9412E96-14A8-4973-AF58-C4DA6C0D3A74.png
*注意  1.版本控制完全是实时的,不会影响接近实时方面的查询操作。如果版本已经被提供了,那么操作执行检查所有的版本。 2.默认情况下,版本从1开始,自增因子为1。

op_type。索引操作也接受参数op_type,用来强制创建索引的操作。如果索引尚未建立的时候,可以接受这样的行为,但是当文档的缩影已经存在的时候,该操作会将失败。 下面是一个op_type参数的例子
curl -XPUT 'http://192.168.8.98:9200/twitter/tweet/1?op_type=create' -d '{
  "user":"kimchy",
  "post_date":"2014-12-05T14:12:12",
  "message":"trying out Elastic Searche”
}’
89E94B27-452B-4271-A2C1-1FFFD577A3E9.png

另外的一种create方式:
curl -XPUT 'http://192.168.8.98:9200/twitter/wwn/1/_create' -d '{
  "user":"kimchy",
  "post_date":"2009-11-11T14:12:12",
  "message":"hello,world"
}'
97A93B06-35B3-4E3D-BAC6-1ED57234F58F.png

curl -XPUT 'http://192.168.8.98:9200/twitter/wwn/1/_create' -d '{
  "user":"kimchy",
  "post_date":"2009-11-11T14:12:12",
  "message":"hello,world"
}’
3E89140D-D679-4CAB-8502-9039B3F7E21D.png
没有就可以成功创建。

路由(Routing)。默认情况下,分片或路由是通过计算文档的hash值来控制的,为了更明确的控制值,送入路由器使用hash函数计算hash值的操作可以通过routing参数来控制。
curl -XPOST 'http://192.168.8.98:9200/twitter/tweet?routing=kimchy' -d '{
  "user":"wwn",
  "post_date":"2014-12-12T12:12:12"
}'
75D51913-2215-430E-ABC8-43A8C66FE41A.png
7508A7D0-C508-4EC3-ABED-7F55FE50687D.png























评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wulantian

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值