ElasticSearch添加索引代碼實例解析
1. 編寫索引內(nèi)容
節(jié)點解釋:
settings:配置信息
"number_of_replicas": 0 不需要備份(單節(jié)點的ElasticSearch使用)
"mappings": 映射內(nèi)容
"dynamic":false 是否動態(tài)索引,這里使用的是false,表示索引的固定的,不需要修改。
"properties": 屬性結構內(nèi)容
"index":"true" 需要分詞處理的結構
type對應的數(shù)據(jù)類型,text文本(長字符串),integer數(shù)字,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", "format":"strict_date_optional_time||epoch_millis"},
"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", "index":"true"},
"layoutDesc":{"type":"text", "index":"true"},
"traffic":{"type":"text", "index":"true"},
"roundService": {"type": "text", "index": "true"},
"rentWay":{"type":"integer"}
}
}
}
}
elasticsearch 7.X版本的索引文件
{
"settings":{
"number_of_replicas": 0
},
"mappings":{
"dynamic":false,
"properties":{
"title":{"type":"text", "index":"true"},
"price":{"type":"integer"},
"area":{"type":"integer"},
"createTime":{"type":"date","format":"strict_date_optional_time||epoch_millis"},
"lastUpdateTime":{"type":"date", "format":"strict_date_optional_time||epoch_millis"},
"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", "index":"true"},
"layoutDesc":{"type":"text", "index":"true"},
"traffic":{"type":"text", "index":"true"},
"roundService": {"type": "text", "index": "true"},
"rentWay":{"type":"integer"}
}
}
}
2. 創(chuàng)建索引
使用Postmen發(fā)送創(chuàng)建索引請求

?。?)地址欄后半段是索引名稱
?。?)請求使用的PUT方式,選擇Body,raw形式,采用JSON格式發(fā)送
創(chuàng)建成功的顯示結果:
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "house"
}
在ElasticSearch-Head里查看結果:
3. 創(chuàng)建索引時的報錯:
錯誤1:Root mapping definition has unsupported parameters
原因:ElasticSearch7.X之后的版本默認不在支持指定索引類型,默認索引類型是_doc(隱含:include_type_name=false),所以在mappings節(jié)點后面,直接跟properties就可以了。

問題2:Could not convert [title.index] to boolean
原因:也是新版本的問題,之前版本的index屬性寫法是"analyze",現(xiàn)在只能設置true, false, "true","false"
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
用Java代碼實現(xiàn)棧數(shù)據(jù)結構的基本方法歸納
這篇文章主要介紹了用Java代碼實現(xiàn)棧數(shù)據(jù)結構的基本方法歸納,各種算法的實現(xiàn)也是ACM上經(jīng)常出現(xiàn)的題目,是計算機學習的基本功,需要的朋友可以參考下2015-08-08
SpringSecurity報錯authenticationManager must be 
這篇文章主要介紹了SpringSecurity報錯authenticationManager must be spec的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11
Java中實現(xiàn)Comparator接口和用法實例(簡明易懂)
這篇文章主要介紹了Java中實現(xiàn)Comparator接口和用法實例(簡明易懂),本文給出實現(xiàn)Comparator接口的實例和使用這個接口的代碼實例,需要的朋友可以參考下2015-05-05

