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!"
}'




每一個被索引的文檔都會有一個版本號,被關聯的版本號會作爲index API的請求的響應信息一部分返回回來。因此,我們可以在對索引操作的時候,指定特定的版本號,操作對應版本的文檔。例如:
curl -XPUT '192.168.8.98:9200/twitter/tweet/1?version=1' -d '{
  "message":"elasticsearch now has versioning support,double cool!"
}'



*注意 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”
}’


另外的一種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"
}'


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"
}’

沒有就可以成功創建。

路由(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"
}'

























發佈了163 篇原創文章 · 獲贊 259 · 訪問量 245萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章