1. 程式人生 > 程式設計 >ElasticSearch新增索引程式碼例項解析

ElasticSearch新增索引程式碼例項解析

1. 編寫索引內容

  節點解釋:

  settings:配置資訊

  "number_of_replicas": 0 不需要備份(單節點的ElasticSearch使用)

  "mappings": 對映內容

  "dynamic":false 是否動態索引,這裡使用的是false,表示索引的固定的,不需要修改。

  "properties": 屬性結構內容

  "index":"true" 需要分詞處理的結構

  type對應的資料型別,text文字(長字串),integer數字,date時間,keyword單詞

elasticsearch 6.X版本的索引檔案

{
 "settings":{
  "number_of_replicas": 0
 },"mappings":{
  "house":{
   "dynamic":false,"properties":{
    "houseId":{"type":"long"},"title":{"type":"text","index":"true"},"price":{"type":"integer"},"area":{"type":"integer"},"createTime":{"type":"date","format":"strict_date_optional_time||epoch_millis"},"lastUpdateTime":{"type":"date","cityEnName":{"type":"keyword"},"regionEnName":{"type":"keyword"},"direction":{"type":"integer"},"distanceToSubway":{"type":"integer"},"subwayLineName":{"type":"keyword"},"subwayStationName":{"type":"keyword"},"tags":{"type":"text"},"district":{"type":"keyword"},"description":{"type":"text","layoutDesc":{"type":"text","traffic":{"type":"text","roundService": {"type": "text","index": "true"},"rentWay":{"type":"integer"}
   }
  }
 }
}

elasticsearch 7.X版本的索引檔案

{
 "settings":{
  "number_of_replicas": 0
 },"mappings":{
  "dynamic":false,"properties":{
   "title":{"type":"text","rentWay":{"type":"integer"}
  }
 }
}

2. 建立索引

  使用Postmen傳送建立索引請求

ElasticSearch新增索引程式碼例項解析

  (1)位址列後半段是索引名稱

  (2)請求使用的PUT方式,選擇Body,raw形式,採用JSON格式傳送

  建立成功的顯示結果:

{
  "acknowledged": true,"shards_acknowledged": true,"index": "house"
}

  在ElasticSearch-Head裡檢視結果:

3. 建立索引時的報錯:

  錯誤1:Root mapping definition has unsupported parameters

  原因:ElasticSearch7.X之後的版本預設不在支援指定索引型別,預設索引型別是_doc(隱含:include_type_name=false),所以在mappings節點後面,直接跟properties就可以了。

ElasticSearch新增索引程式碼例項解析

  問題2:Could not convert [title.index] to boolean

  原因:也是新版本的問題,之前版本的index屬性寫法是"analyze",現在只能設定true,false,"true","false"

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。