当前位置: 首页 > news >正文

elasticsearch整合java使用创建索引、指定索引映射、操作添加文档、删除文档、更新文档、批量操作

前言:

elasticsearch的整合流程可以参考:Elasticsearch7.15版本后新版本的接入-CSDN博客

索引

1.创建索引

@Testpublic void contextLoads() throws IOException {ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();boolean exists = elasticsearchClient.indices().exists(query -> query.index("new_ceshi")).value();System.out.println(exists);if (exists) {System.out.println("已存在");} else {final CreateIndexResponse products = elasticsearchClient.indices().create(builder -> builder.index("new_ceshi"));System.out.println(products.acknowledged());}}

2.查询索引

@Testpublic void contextLoads() throws IOException {ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();GetIndexResponse products = elasticsearchClient.indices().get(query -> query.index("new_ceshi"));System.out.println(products.toString());}

3.删除索引

@Testpublic void contextLoads() throws IOException {ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();boolean exists = elasticsearchClient.indices().exists(query -> query.index("new_ceshi")).value();System.out.println(exists);if (exists) {DeleteIndexResponse response = elasticsearchClient.indices().delete(query -> query.index("new_ceshi"));System.out.println(response.acknowledged());} else {System.out.println("索引不存在");}}

索引映射 

4.查询索引的映射

@Testpublic void contextLoads() throws IOException {ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();GetIndexResponse response = elasticsearchClient.indices().get(builder -> builder.index("new_bank"));System.out.println(response.result().get("new_bank").mappings().toString());}

5.创建索引以及初始化索引映射

    @Testpublic void contextLoads() throws IOException {ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();elasticsearchClient.indices().create(builder -> builder.index("new_product").mappings(map -> map.properties("name", p -> p.text(textProperty -> textProperty.analyzer("ik_max_word").searchAnalyzer("ik_max_word"))).properties("intro", p -> p.text(textProperty -> textProperty.analyzer("ik_max_word").searchAnalyzer("ik_max_word"))).properties("stock", p -> p.integer(integerProperty -> integerProperty))));}

文档 

6.创建文档-自定义类数据存储容器

@Testpublic void contextLoads() throws IOException {ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();Produce produce = new Produce("饼干", "上好的饼干", 2000);IndexResponse response = elasticsearchClient.index(builder -> builder.index("new_product").id("1").document(produce));System.err.println(response.version());}

结果:

7.创建文档-HashMap存储容器

@Testpublic void contextLoads() throws IOException {ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();HashMap<String, Object> doc = new HashMap<>();doc.put("name","油条");doc.put("intro","纯油炸的油条");doc.put("stock","999");final IndexResponse response = elasticsearchClient.index(builder -> builder.index("new_product").id("2").document(doc));}

8.查询所有文档

@Testpublic void contextLoads() throws IOException {ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();SearchResponse<Object> response = elasticsearchClient.search(builder -> builder.index("new_product"), Object.class);List<Hit<Object>> hits = response.hits().hits();hits.forEach(x-> System.out.println(x.toString()));}

9.查询某个id的文档

  @Testpublic void contextLoads() throws IOException {ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();GetRequest new_product = new GetRequest.Builder().index("new_product").id("1").build();GetResponse<Object> objectGetResponse = elasticsearchClient.get(new_product, Object.class);System.out.printf("objectGetResponse=========="+objectGetResponse.source());}

10删除文档

@Testpublic void contextLoads() throws IOException {ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();//删除文档DeleteRequest new_product = new DeleteRequest.Builder().index("new_product").id("1").build();DeleteResponse delete = elasticsearchClient.delete(new_product);System.out.printf("delete==========" + delete);}

 11.更新文档-自定义类

        全更新

@Testpublic void contextLoads() throws IOException {ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();Produce produce = new Produce("铁锤", "全刚的大铁锤", 666);UpdateResponse<Produce> new_product = elasticsearchClient.update(builder -> builder.index("new_product").id("2").doc(produce), Produce.class);System.err.println(new_product.shards().successful());}

        指定字段修改.docAsUpsert(true)

@Testpublic void contextLoads() throws IOException {ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();Produce produce = new Produce();produce.setName("小铁锤");UpdateResponse<Produce> new_product = elasticsearchClient.update(builder -> builder.index("new_product").id("2").docAsUpsert(true).doc(produce), Produce.class);System.err.println(new_product.shards().successful());}

12更新文档-Map更新

@Testpublic void contextLoads() throws IOException {ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();Map<String, Object> updateJson = new HashMap<>();updateJson.put("name","巨大铁锤");UpdateRequest<Object, Object> updateRequest = new UpdateRequest.Builder<>().index("new_product").id("2").doc(updateJson).build();UpdateResponse<Object> updateResponse = elasticsearchClient.update(updateRequest, Object.class);System.out.println("Document updated: " + updateResponse.result());}

批量操作

13批量添加

@Testpublic void contextLoads() throws IOException {ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();List<SkuEsModel> skuEsModels = new ArrayList<>();BulkRequest.Builder br = new BulkRequest.Builder();for (SkuEsModel skuEsModel : skuEsModels) {br.operations(op->op.index(idx->idx.index("produces").id(String.valueOf(skuEsModel.getSkuId())).document(skuEsModel)));}BulkResponse response = elasticsearchClient.bulk(br.build());}

 

复杂检索请查看:elasticsearch复杂检索,match、matchAll、matchPhrase、term、多条件查询、multiMatch多字段查询等


http://www.mrgr.cn/news/15516.html

相关文章:

  • HTML静态网页成品作业(HTML+CSS)——非遗徽州木雕网页(6个页面)
  • PHP房屋出售出租多端多平台预约系统小程序源码
  • 深度学习环境安装与验证指南
  • 安全运维--Linux常用命令
  • 黑悟空!一区预定!原创首发!SLWCHOA-Transformer-LSTM混合改进策略的黑猩猩优化算法多变量时间序列预测
  • HS编码的作用是什么
  • 【最新】高效可用的Docker仓库源
  • 概率论与高等数学、线性代数的联系及应用
  • HarmonyOs如何获取rawfile目录下的所有文件列表
  • 控制理论:MindOpt Python API求解模型预测控制问题(Model Predictive Control)
  • 使用maven快速生成打包文件2
  • 【Rust光年纪】保护用户数据安全隐私:探秘Rust机密计算与安全库
  • 科技赋能 重塑未来医疗丨共谋医疗信息化新方向,2024东北医院信息网络大会圆满落幕!
  • Oracle中数据类型 NVARCHAR2 与 VARCHAR2 的对比
  • 最好的超声波清洗机是哪一款?好用的超声波清洗机排名
  • 嘴上说“摆烂”的90后,考了最多的PMP证书......
  • SOMEIP_ETS_073: Union_with_wrong_type_field_for_union_member_Padding
  • git---gitignore--忽略文件
  • 第T10周:使用TensorFlow实现数据增强
  • MySQL基础学习:如何排查慢SQL