鍍金池/ 教程/ Java/ Elasticsearch索引API
Elasticsearch搜索API
Elasticsearch教程
Elasticsearch填充
Elasticsearch測(cè)試
Elasticsearch分析
Elasticsearch映射
Elasticsearch入門教程
Elasticsearch版本之間遷移
Elasticsearch集群API
Elasticsearch模塊
Elasticsearch環(huán)境安裝配置
Elasticsearch聚合
Elasticsearch查詢DSL
Elasticsearch索引API
Elasticsearch API約定
Elasticsearch文檔API

Elasticsearch索引API

這些API負(fù)責(zé)管理索引的所有方面,如設(shè)置,別名,映射,索引模板。

創(chuàng)建索引

此API可用于創(chuàng)建索引。 當(dāng)用戶將JSON對(duì)象傳遞到任何索引時(shí),可以自動(dòng)創(chuàng)建索引,也可以在此之前創(chuàng)建索引。 要?jiǎng)?chuàng)建索引,只需要發(fā)送包含設(shè)置,映射和別名的發(fā)布請(qǐng)求,或者只發(fā)送一個(gè)沒有正文的簡(jiǎn)單請(qǐng)求。 例如,

POST http://localhost:9200/colleges

響應(yīng)

{"acknowledged":true}

或者,加上一些設(shè)置 -

POST http://localhost:9200/colleges

請(qǐng)求正文

{
   "settings" : {
      "index" : {
         "number_of_shards" : 5, "number_of_replicas" : 3
      }
   }
}

響應(yīng)

{"acknowledged":true}

或使用映射 -

POST http://localhost:9200/colleges

請(qǐng)求正文

{
   "settings" : {
      "number_of_shards" : 3
   },

   "mappings" : {
      "type1" : {
         "_source" : { "enabled" : false }, "properties" : {
            "college_name" : { "type" : "string" }, "college type" : {"type":"string"}
         }
      }
   }
}

響應(yīng)

{"acknowledged":true}

或者,用別名 -

POST http://localhost:9200/colleges

請(qǐng)求正文

{
   "aliases" : {
      "alias_1" : {}, "alias_2" : {
         "filter" : {
            "term" : {"user" : "manu" }
         },
         "routing" : "manu"
      }
   }
}

響應(yīng)

{"acknowledged":true}

刪除索引

此API可用來刪除任何索引。只需要傳遞一個(gè)刪除請(qǐng)求以及指定索引的URL。 例如,

DELETE http://localhost:9200/colleges

可以通過使用_all,*刪除所有索引。

獲取索引

這個(gè)API可以通過發(fā)送get請(qǐng)求到一個(gè)或多個(gè)索引來調(diào)用。這將返回有關(guān)索引的信息。

GET http://localhost:9200/schools

響應(yīng)

{
   "schools":{
      "aliases":{}, "mappings":{
         "school":{
            "properties":{
               "city":{"type":"string"}, "description":{"type":"string"},
               "fees":{"type":"long"}, "location":{"type":"double"},
               "name":{"type":"string"}, "rating":{"type":"string"},
               "state":{"type":"string"}, "street":{"type":"string"},
               "tags":{"type":"string"}, "zip":{"type":"string"}
            }
         }
      },

      "settings":{
         "index":{
            "creation_date":"1454409831535", "number_of_shards":"5",
            "number_of_replicas":"1", "uuid":"iKdjTtXQSMCW4xZMhpsOVA",
            "version":{"created":"2010199"}
         }
      },
      "warmers":{}
   }
}

可以使用_all*來獲取所有索引的信息。

測(cè)試索引存在

可以通過向該索引發(fā)送獲取請(qǐng)求來確定索引的存在。如果HTTP響應(yīng)為200,則存在; 如果是404,它不存在。

打開/關(guān)閉索引API

通過在post中添加_close_open來請(qǐng)求索引,可以很容易地關(guān)閉或打開一個(gè)或多個(gè)索引。 例如,
關(guān)閉索引-

POST http://localhost:9200/schools/_close

或打開索引-

POST http://localhost:9200/schools/_open

索引別名

此API有助于使用_aliases關(guān)鍵字向任何索引提供別名。 單個(gè)別名可以映射到多個(gè)別名,且別名不能與索引具有相同的名稱。 例如,

POST http://localhost:9200/_aliases

請(qǐng)求正文

{
   "actions" : [
      { "add" : { "index" : "schools", "alias" : "schools_pri" } }
   ]
}

響應(yīng)

{"acknowledged":true}

然后,

GET http://localhost:9200/schools_pri

響應(yīng)

{"schools":{"aliases":{"schools_pri":{}},"}}

索引設(shè)置

可以通過在URL結(jié)尾處附加_settings關(guān)鍵字來獲取索引設(shè)置。 例如,

GET http://localhost:9200/schools/_settings

響應(yīng)

{
   "schools":{
      "settings":{
         "index":{
            "creation_date":"1454409831535", "number_of_shards":"5", 
            "number_of_replicas":"1", "uuid":"iKdjTtXQSMCW4xZMhpsOVA", 
            "version":{"created":"2010199"}
        }
      }
   }
}

分析

此API有助于分析文本并使用偏移值和數(shù)據(jù)類型發(fā)送令牌。 例如,

POST http://localhost:9200/_analyze

請(qǐng)求正文

{
   "analyzer" : "standard",
   "text" : "you are reading this at YIIBAI point"
}

響應(yīng)

{
   "tokens":[
      {"token":"you", "start_offset":0, "end_offset":3, "type":"<ALPHANUM>", "position":0},
      {"token":"are", "start_offset":4, "end_offset":7, "type":"<ALPHANUM>", "position":1},
      {"token":"reading", "start_offset":8, "end_offset":15, "type":"<ALPHANUM>", "position":2},
      {"token":"this", "start_offset":16, "end_offset":20, "type":"<ALPHANUM>", "position":3},
      {"token":"at", "start_offset":21, "end_offset":23, "type":"<ALPHANUM>", "position":4},
      {"token":"tutorials", "start_offset":24, "end_offset":33, "type":"<ALPHANUM>", "position":5},
      {"token":"point", "start_offset":34, "end_offset":39, "type":"<ALPHANUM>", "position":6}
   ]
}

還可以使用任何索引分析文本,然后根據(jù)與該索引關(guān)聯(lián)的分析器來分析文本。

索引模板

還可以創(chuàng)建具有映射的索引模板,這可以應(yīng)用于新的索引。 例如,

POST http://localhost:9200/_template/template_a

響應(yīng)

{
   "template" : "tu*", 
      "settings" : {
         "number_of_shards" : 3
   },

   "mappings" : {
      "chapter" : {
         "_source" : { "enabled" : false }
      }
   }
}

以“tu”開頭的任何索引都將具有與模板相同的設(shè)置。

索引統(tǒng)計(jì)

此API可用于提取有關(guān)特定索引的統(tǒng)計(jì)信息。只需要發(fā)送一個(gè)帶有索引URL_stats關(guān)鍵字的get請(qǐng)求。

GET http://localhost:9200/schools/_stats

響應(yīng)

………………………………………………
{"_shards":{"total":10, "successful":5, "failed":0}, "_all":{"primaries":{"docs":{
   "count":3, "deleted":0}}}, "store":{"size_in_bytes":16653, "throttle_time_in_millis":0},
………………………………………………

刷新清除數(shù)據(jù)

此API用于從索引內(nèi)存中清除數(shù)據(jù),并將其遷移到索引存儲(chǔ),并清除內(nèi)部事務(wù)日志。 例如,

GET http://localhost:9200/schools/_flush

響應(yīng)

{"_shards":{"total":10, "successful":5, "failed":0}}

 刷新索引

默認(rèn)情況下,刷新在Elasticsearch中一般按計(jì)劃來執(zhí)行,但可以使用_refresh顯式刷新一個(gè)或多個(gè)索引。 例如,

GET http://localhost:9200/schools/_refresh

響應(yīng)

{"_shards":{"total":10, "successful":5, "failed":0}}