elasticsearch创建索引操做的API

ElasticSearch-API-Index

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

  • 最简单的方式建立索引web

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

    curl -XPUT ‘http://localhost:9200/twitter/‘ -d ‘
    index:
      number_of_shards:3
      number_of_replicas:2
    ‘
  • 在建立索引的时候指定分片和副本数量参数,参数格式采用JSON格式app

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

    或者简化为curl

    curl -XPUT ‘http://localhost:9200/twitter’ -d ‘{
      “settings”:{
          “number_of_shards”:3,
          “number_of_replicas”:2
      }
    }’

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

  • 索引建立API能够接受一个或者一组映射选项ide

    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”的索引,而且索引类型为tweetpost

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

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

    {
      "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?"
    }'

    更过信息请浏览_ttl mapping page

  • 想要关注更多不一样的,能够在建立索引时设置索引级选项,请看index modules

  • 本文参考信息来自elasticSearch中文社区

相关文章
相关标签/搜索