ElasticSearch添加索引代碼實(shí)例解析
1. 編寫(xiě)索引內(nèi)容
節(jié)點(diǎn)解釋?zhuān)?/p>
settings:配置信息
"number_of_replicas": 0 不需要備份(單節(jié)點(diǎn)的ElasticSearch使用)
"mappings": 映射內(nèi)容
"dynamic":false 是否動(dòng)態(tài)索引,這里使用的是false,表示索引的固定的,不需要修改。
"properties": 屬性結(jié)構(gòu)內(nèi)容
"index":"true" 需要分詞處理的結(jié)構(gòu)
type對(duì)應(yīng)的數(shù)據(jù)類(lèi)型,text文本(長(zhǎng)字符串),integer數(shù)字,date時(shí)間,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)建索引請(qǐng)求

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

問(wèn)題2:Could not convert [title.index] to boolean
原因:也是新版本的問(wèn)題,之前版本的index屬性寫(xiě)法是"analyze",現(xiàn)在只能設(shè)置true, false, "true","false"
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
用Java代碼實(shí)現(xiàn)棧數(shù)據(jù)結(jié)構(gòu)的基本方法歸納
這篇文章主要介紹了用Java代碼實(shí)現(xiàn)棧數(shù)據(jù)結(jié)構(gòu)的基本方法歸納,各種算法的實(shí)現(xiàn)也是ACM上經(jīng)常出現(xiàn)的題目,是計(jì)算機(jī)學(xué)習(xí)的基本功,需要的朋友可以參考下2015-08-08
Java實(shí)現(xiàn)PDF轉(zhuǎn)圖片的三種方法
有些時(shí)候我們需要在項(xiàng)目中展示PDF,所以我們可以將PDF轉(zhuǎn)為圖片,然后已圖片的方式展示,效果很好,Java使用各種技術(shù)將pdf轉(zhuǎn)換成圖片格式,并且內(nèi)容不失幀,本文給大家介紹了三種方法實(shí)現(xiàn)PDF轉(zhuǎn)圖片的案例,需要的朋友可以參考下2023-10-10
詳解Spring 兩種注入的方式(Set和構(gòu)造)實(shí)例
本篇文章主要介紹了Spring 兩種注入的方式(Set和構(gòu)造)實(shí)例,Spring框架主要提供了Set注入和構(gòu)造注入兩種依賴(lài)注入方式。有興趣的可以了解一下。2017-02-02
SpringSecurity報(bào)錯(cuò)authenticationManager must be 
這篇文章主要介紹了SpringSecurity報(bào)錯(cuò)authenticationManager must be spec的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
Java中實(shí)現(xiàn)Comparator接口和用法實(shí)例(簡(jiǎn)明易懂)
這篇文章主要介紹了Java中實(shí)現(xiàn)Comparator接口和用法實(shí)例(簡(jiǎn)明易懂),本文給出實(shí)現(xiàn)Comparator接口的實(shí)例和使用這個(gè)接口的代碼實(shí)例,需要的朋友可以參考下2015-05-05

