ElasticSearch添加索引代碼實例解析
1. 編寫索引內(nèi)容
節(jié)點解釋:
settings:配置信息
"number_of_replicas": 0 不需要備份(單節(jié)點的ElasticSearch使用)
"mappings": 映射內(nèi)容
"dynamic":false 是否動態(tài)索引,這里使用的是false,表示索引的固定的,不需要修改。
"properties": 屬性結(jié)構(gòu)內(nèi)容
"index":"true" 需要分詞處理的結(jié)構(gòu)
type對應(yīng)的數(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)建成功的顯示結(jié)果:
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "house"
}
在ElasticSearch-Head里查看結(jié)果:
3. 創(chuàng)建索引時的報錯:
錯誤1:Root mapping definition has unsupported parameters
原因:ElasticSearch7.X之后的版本默認(rèn)不在支持指定索引類型,默認(rèn)索引類型是_doc(隱含:include_type_name=false),所以在mappings節(jié)點后面,直接跟properties就可以了。

問題2:Could not convert [title.index] to boolean
原因:也是新版本的問題,之前版本的index屬性寫法是"analyze",現(xiàn)在只能設(shè)置true, false, "true","false"
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Bean創(chuàng)建和循環(huán)依賴
這篇文章主要介紹了Spring Bean創(chuàng)建和循環(huán)依賴,講述了Spring容器中?Bean?的創(chuàng)建過程已經(jīng)主要的方法,另外也著重分析了循環(huán)依賴的問題,需要的小伙伴可以參考一下2022-05-05
IntelliJ IDEA配置java環(huán)境及解決IDEA不能直接運行單個JAVA文件的問題
這篇文章主要介紹了IntelliJ IDEA配置java環(huán)境及解決IDEA不能直接運行單個JAVA文件的問題,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07
springboot中rabbitmq實現(xiàn)消息可靠性機(jī)制詳解
這篇文章主要介紹了springboot中rabbitmq實現(xiàn)消息可靠性機(jī)制詳解,本文通過實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-09-09
java并發(fā)學(xué)習(xí)-CountDownLatch實現(xiàn)原理全面講解
這篇文章主要介紹了java并發(fā)學(xué)習(xí)-CountDownLatch實現(xiàn)原理全面講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02
Spring聲明式事務(wù)@Transactional注解實現(xiàn)元數(shù)據(jù)驅(qū)動的事務(wù)管理
這篇文章主要為大家介紹了Spring聲明式事務(wù)@Transactional注解實現(xiàn)元數(shù)據(jù)驅(qū)動的事務(wù)管理示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
Object.wait()與Object.notify()的用法詳細(xì)解析
以下是對java中Object.wait()與Object.notify()的用法進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下2013-09-09

