elasticsearch整合java利用创建索引、指定索引映射、操作添加文档、删除文 ...

张裕  高级会员 | 2024-8-30 19:44:57 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 212|帖子 212|积分 636

前言:

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

1.创建索引

  1. @Test
  2.     public void contextLoads() throws IOException {
  3.         ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
  4.         
  5.         boolean exists = elasticsearchClient.indices().exists(query -> query.index("new_ceshi")).value();
  6.         System.out.println(exists);
  7.         if (exists) {
  8.             System.out.println("已存在");
  9.         } else {
  10.             final CreateIndexResponse products = elasticsearchClient.indices().create(builder -> builder.index("new_ceshi"));
  11.             System.out.println(products.acknowledged());
  12.         }
  13.     }
复制代码
2.查询索引

  1. @Test
  2.     public void contextLoads() throws IOException {
  3.         ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
  4.         GetIndexResponse products = elasticsearchClient.indices().get(query -> query.index("new_ceshi"));
  5.         System.out.println(products.toString());
  6.     }
复制代码
3.删除索引

  1. @Test
  2.     public void contextLoads() throws IOException {
  3.         ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
  4.         boolean exists = elasticsearchClient.indices().exists(query -> query.index("new_ceshi")).value();
  5.         System.out.println(exists);
  6.         if (exists) {
  7.             DeleteIndexResponse response = elasticsearchClient.indices().delete(query -> query.index("new_ceshi"));
  8.             System.out.println(response.acknowledged());
  9.         } else {
  10.             System.out.println("索引不存在");
  11.         }
  12.     }
复制代码
索引映射 

4.查询索引的映射

  1. @Test
  2.     public void contextLoads() throws IOException {
  3.         ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
  4.         GetIndexResponse response = elasticsearchClient.indices().get(builder -> builder.index("new_bank"));
  5.         System.out.println(response.result().get("new_bank").mappings().toString());
  6.     }
复制代码
5.创建索引以及初始化索引映射

  1.     @Test
  2.     public void contextLoads() throws IOException {
  3.         ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
  4.         elasticsearchClient.indices()
  5.                 .create(builder -> builder.index("new_product")
  6.                         .mappings(map -> map.properties("name", p -> p.text(textProperty -> textProperty.analyzer("ik_max_word").searchAnalyzer("ik_max_word")))
  7.                                 .properties("intro", p -> p.text(textProperty -> textProperty.analyzer("ik_max_word").searchAnalyzer("ik_max_word")))
  8.                                 .properties("stock", p -> p.integer(integerProperty -> integerProperty)))
  9.                 );
  10.     }
复制代码
文档 

6.创建文档-自界说类数据存储容器

  1. @Test
  2.     public void contextLoads() throws IOException {
  3.         ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
  4.         Produce produce = new Produce("饼干", "上好的饼干", 2000);
  5.         IndexResponse response = elasticsearchClient.index(builder -> builder.index("new_product").id("1").document(produce));
  6.         System.err.println(response.version());
  7.     }
复制代码
效果:

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

  1. @Test
  2.     public void contextLoads() throws IOException {
  3.         ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
  4.         HashMap<String, Object> doc = new HashMap<>();
  5.         doc.put("name","油条");
  6.         doc.put("intro","纯油炸的油条");
  7.         doc.put("stock","999");
  8.         final IndexResponse response = elasticsearchClient.index(builder -> builder.index("new_product").id("2").document(doc));
  9.     }
复制代码
8.查询全部文档

  1. @Test
  2.     public void contextLoads() throws IOException {
  3.         ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
  4.         SearchResponse<Object> response = elasticsearchClient.search(builder -> builder.index("new_product"), Object.class);
  5.         List<Hit<Object>> hits = response.hits().hits();
  6.         hits.forEach(
  7.                 x-> System.out.println(x.toString())
  8.         );
  9.     }
复制代码
9.查询某个id的文档

  1.   @Test
  2.     public void contextLoads() throws IOException {
  3.         ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
  4.         GetRequest new_product = new GetRequest.Builder()
  5.                 .index("new_product")
  6.                 .id("1")
  7.                 .build();
  8.         GetResponse<Object> objectGetResponse = elasticsearchClient.get(new_product, Object.class);
  9.         System.out.printf("objectGetResponse=========="+objectGetResponse.source());
  10.     }
复制代码
10删除文档

  1. @Test
  2.     public void contextLoads() throws IOException {
  3.         ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
  4.         //删除文档
  5.         DeleteRequest new_product = new DeleteRequest.Builder()
  6.                 .index("new_product")
  7.                 .id("1")
  8.                 .build();
  9.         DeleteResponse delete = elasticsearchClient.delete(new_product);
  10.         System.out.printf("delete==========" + delete);
  11.     }
复制代码
 11.更新文档-自界说类

        全更新
  1. @Test
  2.     public void contextLoads() throws IOException {
  3.         ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
  4.         Produce produce = new Produce("铁锤", "全刚的大铁锤", 666);
  5.         UpdateResponse<Produce> new_product = elasticsearchClient.update(builder -> builder.index("new_product").id("2").doc(produce), Produce.class);
  6.         System.err.println(new_product.shards().successful());
  7.     }
复制代码
        指定字段修改.docAsUpsert(true)
  1. @Test
  2.     public void contextLoads() throws IOException {
  3.         ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
  4.         Produce produce = new Produce();
  5.         produce.setName("小铁锤");
  6.         UpdateResponse<Produce> new_product = elasticsearchClient.update(builder -> builder.index("new_product").id("2").docAsUpsert(true).doc(produce), Produce.class);
  7.         System.err.println(new_product.shards().successful());
  8.     }
复制代码
12更新文档-Map更新

  1. @Test
  2.     public void contextLoads() throws IOException {
  3.         ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
  4.         Map<String, Object> updateJson = new HashMap<>();
  5.         updateJson.put("name","巨大铁锤");
  6.         UpdateRequest<Object, Object> updateRequest = new UpdateRequest.Builder<>()
  7.                 .index("new_product")
  8.                 .id("2")
  9.                 .doc(updateJson)
  10.                 .build();
  11.         UpdateResponse<Object> updateResponse = elasticsearchClient.update(updateRequest, Object.class);
  12.         System.out.println("Document updated: " + updateResponse.result());
  13.     }
复制代码
批量操作
13批量添加
  1. @Test
  2.     public void contextLoads() throws IOException {
  3.         ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
  4.         List<SkuEsModel> skuEsModels = new ArrayList<>();
  5.         BulkRequest.Builder br = new BulkRequest.Builder();
  6.         for (SkuEsModel skuEsModel : skuEsModels) {
  7.             br.operations(op->op.index(idx->idx.index("produces").id(String.valueOf(skuEsModel.getSkuId())).document(skuEsModel)));
  8.         }
  9.         BulkResponse response = elasticsearchClient.bulk(br.build());
  10.     }
复制代码
 
复杂检索请查看:elasticsearch复杂检索,match、matchAll、matchPhrase、term、多条件查询、multiMatch多字段查询等

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

张裕

高级会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表