irpas技术客

ElasticsearchClient - java - 基础篇 - 索引 - 创建基本索引_这是谁?_elasticsearch java创建索引

irpas 7540

世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。 -侯氏工坊

文章目录 参考导言创建索引索引数据索引单个文档索引多个文档

参考 elasticsearch - java - elasticsearch和kibana的安装和配置elasticsearch - java - elasticsearch对接spring bootElasticsearchClient客户端深入学习访问基础篇和高级篇所有栏目内容参考栏目预告 导言 elasticsearch可以自动识别数据类型本章的目的是将数据索引到elasticsearch中,没有使用mapping及analyzer等操作后续会优先搜索部分的实现 创建索引

原语句

PUT /test

java实现

创建索引/** * @describe 创建索引 * @param index 索引名称 * @return 创建索引的结果 * @throws Exception 抛出异常 */ private Boolean createIndex(String index) throws Exception { // 判断索引是否存在 BooleanResponse b = elasticsearchClient.indices().exists(_0 -> _0 .index(index)); if (!b.value()) { // 创建索引 CreateIndexResponse re = elasticsearchClient.indices() .create(_0 -> _0 .index(index)); return re.acknowledged(); } return b.value(); } 创建数据流索引/** * @describe 创建数据流索引 * @param index 索引名称 * @return 创建索引的结果 * @throws Exception 抛出异常 */ private Boolean createDataStreamIndex(String index) throws Exception { // 判断索引是否存在 BooleanResponse b = elasticsearchClient.indices().exists(_0 -> _0 .index(index)); if (!b.value()) { // 创建数据流索引 CreateDataStreamResponse re = elasticsearchClient.indices() .createDataStream(_0 -> _0 .name(index)); return re.acknowledged(); } return b.value(); } 索引数据 索引单个文档 原语句POST /test/_doc { "name": "hello" } java实现/** * @describe 索引单个文档 * @param index 索引名称 * @param o 文档对象 * @return 索引文档结果 * @throws Exception 抛出异常 */ private IndexResponse index(String index, Object o) throws Exception { return elasticsearchClient .index(_0 -> _0 .index(index) .document(o) ); } 索引多个文档

原语句

POST /test/_bulk {"create": {}} {"name": "good"} {"create": {}} {"name": "me"}

java实现

/** * @describe 索引多个文档 * @param index 索引名称 * @param ls 文档列表 * @return 索引文档结果 * @throws Exception 抛出异常 */ private BulkResponse bulk(String index, List<?> ls) throws Exception { return elasticsearchClient.bulk(_0 -> { _0.index(index); ls.forEach(_1 -> _0 .operations(_2 -> _2 .create(_3 -> _3 .document(_1)))); return _0; }); }


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #ElasticSearch #java创建索引 #JAVA