ElasticSearch添加索引代码实例解析

网友投稿 327 2023-06-15

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", "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"},

"roundServicehttp://": {"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. 创建索引

使用Postmen发送创建索引请求

(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就可以了。

问题2:Could not convert [title.index] to boolean

原因:也是新版本的问题,之前版本的index属性写法是"analyze",现在只能设置true, false, "true","false"

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:SpringMVC中RequestContextHolder获取请求信息的方法
下一篇:SpringBoot集成Elasticsearch过程实例
相关文章

 发表评论

暂时没有评论,来抢沙发吧~