索引建立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中文社區