025 elasticsearch索引管理-Java原生客户端

news/2024/10/22 3:15:36/

文章目录

    • pom.xml
    • 1创建索引
    • 2.创建索引并设置settings信息
    • 3.创建索引并设置mapping信息
    • 4.删除索引库
    • 5.给未设置mapping的索引设置mapping

elasticsearch版本7.10.2,要求java客户端与之相匹配,推荐Springboot版本是2.3以上版本

依赖配置使用的是JUnit 5(由<artifactId>spring-boot-starter-test</artifactId>提供支持),而@Before注解是JUnit 4中的注解。在JUnit 5中,应该使用@BeforeEach来代替@Before。
替换注解:
将所有的@Before注解替换为@BeforeEach。
确保你的测试类使用了JUnit 5的相关注解,如@Test(JUnit 5中的@Test注解位于org.junit.jupiter.api.Test)。
确保测试类正确配置:
确保你的测试类上没有使用JUnit 4的@RunWith注解。
如果使用了Spring的测试支持,确保类上有@SpringBootTest或其他相关的Spring测试注解。
更新测试方法:
确认所有的测试方法都使用了JUnit 5的@Test注解。

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><artifactId>chubemall-search</artifactId><packaging>jar</packaging><name>chubemall-search</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><java.version>1.8</java.version><spring-cloud.version>Greenwich.SR3</spring-cloud.version><elasticsearch.version>7.10.2</elasticsearch.version></properties><dependencies>
<!--        <dependency>-->
<!--            <groupId>junit</groupId>-->
<!--            <artifactId>junit</artifactId>-->
<!--            <version>3.8.1</version>-->
<!--            <scope>test</scope>-->
<!--        </dependency>--><!--引入common公共模块--><dependency><groupId>com.xd.cubemall</groupId><artifactId>cubemall-common</artifactId><version>0.0.1-SNAPSHOT</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!--        <dependency>-->
<!--            <groupId>org.springframework.boot</groupId>-->
<!--            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>-->
<!--        </dependency>--><dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><version>7.10.2</version></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>
</project>

1创建索引

步骤:
1.创建一个RestHightLevelClient对象,相当于和服务端建立连接
2.使用client的索引管理的对象,indices()返回索引管理对象

package com.xd.cubemall.es;import org.apache.http.HttpHost;
import org.elasticsearch.client.IndicesClient;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.junit.jupiter.api.Test;/*** 索引管理*/
public class IndexManager {@Testpublic void createIndex() throws Exception {//创建一个client对象RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("1.1.1.1",9200),new HttpHost("2.2.2.2",9200),new HttpHost("3.3.3.3",9200)));//获得索引管理对象IndicesClient indicesClient = client.indices();//两个参数//1.创建索引请求对象CreateIndexRequest request = new CreateIndexRequest("hello");//2.请求选项,使用默认值。配置请求头,主要用于认证。CreateIndexResponse response = indicesClient.create(request, RequestOptions.DEFAULT);System.out.println(response);}
}

2.创建索引并设置settings信息

CreateIndexRequest对象中设置settings即可

package com.xd.cubemall.es;import org.apache.http.HttpHost;
import org.elasticsearch.client.IndicesClient;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.settings.Settings;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;/*** 索引管理*/
public class IndexManager {private RestHighLevelClient client;@BeforeEachpublic void init() {//创建一个client对象client = new RestHighLevelClient(RestClient.builder(new HttpHost("1.1.1.1",9200),new HttpHost("2.2.2.2",9200),new HttpHost("3.3.3.3",9200)));}@Testpublic void createIndex() throws Exception {//获得索引管理对象IndicesClient indicesClient = client.indices();//两个参数//1.创建索引请求对象CreateIndexRequest request = new CreateIndexRequest("hello");//2.请求选项,使用默认值。配置请求头,主要用于认证。CreateIndexResponse response = indicesClient.create(request, RequestOptions.DEFAULT);//显示结果System.out.println(response);}@Testpublic void createIndex2() throws Exception {CreateIndexRequest request = new CreateIndexRequest("hello1").settings(Settings.builder().put("number_of_shards", 5).put("number_of_replicas",1).build());System.out.println("Client is null: " + (client == null));System.out.println("Request is null: " + (request == null));client.indices().create(request,RequestOptions.DEFAULT);}}

3.创建索引并设置mapping信息

{"properties":{"id":{"type":"long"},"title":{"type":"text","analyzer":"ik_smart","store":true},"content":{"type":"text","analyzer":"ik_smart","store":true}		}
}
    @Testpublic void createIndex3() throws Exception {XContentBuilder mappings = XContentFactory.jsonBuilder().startObject().startObject("properties").startObject("id").field("type","long").endObject().startObject("title").field("type","text").field("analyzer","ik_smart").field("store",true).endObject().startObject("content").field("type","text").field("analyzer","ik_smart").field("store",true).endObject().endObject().endObject();CreateIndexRequest request = new CreateIndexRequest("hello2").settings(Settings.builder().put("number_of_shards", 5).put("number_of_replicas",1).build()).mapping(mappings);client.indices().create(request, RequestOptions.DEFAULT);}

4.删除索引库

DeleteIndexRequest对象,其中包含索引库的名称即可
indices().delete(request)

    @Testpublic void deleteIndex() throws Exception {client.indices().delete(new DeleteIndexRequest("hello"),RequestOptions.DEFAULT);}

5.给未设置mapping的索引设置mapping

    @Testpublic void putMappings() throws Exception {String mappings = "{\n" +"\t\"properties\":{\n" +"\t\t\"id\":{\n" +"\t\t\t\"type\":\"long\"\n" +"\t\t},\n" +"\t\t\"title\":{\n" +"\t\t\t\"type\":\"text\",\n" +"\t\t\t\"analyzer\":\"ik_smart\",\n" +"\t\t\t\"store\":true\n" +"\t\t},\n" +"\t\t\"content\":{\n" +"\t\t\t\"type\":\"text\",\n" +"\t\t\t\"analyzer\":\"ik_smart\",\n" +"\t\t\t\"store\":true\n" +"\t\t}\t\t\n" +"\t}\n" +"}";PutMappingRequest request = new PutMappingRequest("hello1").source(mappings, XContentType.JSON);client.indices().putMapping(request,RequestOptions.DEFAULT);}

http://www.ppmy.cn/news/1540959.html

相关文章

如何使用Rancher管理K8S集群

目录 1 Rancher介绍 1.1 Rancher简介 1.2 Rancher和k8s的区别 1.3 Rancher使用案例 2安装rancher 2.1 初始化实验环境 2.2 安装Rancher 2.3 登录Rancher平台 3 通过Rancher管理已存在的k8s集群 4 通过Rancher仪表盘管理k8s集群&#xff1a;部署tomcat服务 文档中…

C++面试速通宝典——29

543. 简述#ifdef、#else、#endif和#ifndef的作用 利用#ifdef、#endif将程序功能模块包括进去&#xff0c;以向特定用户提供该功能。 在不需要时用户可轻易将其屏蔽。 #ifdef MATH #include "math.c" #endif 在子程序前加上标记&#xff0c;以便于追踪和调试。 …

Java JDK的面试题

关于Java JDK的面试题&#xff0c;这里有一些常见的问题和答案&#xff0c;可以帮助你准备面试&#xff1a; JDK、JRE和JVM的区别&#xff1a; JDK&#xff08;Java Development Kit&#xff09;是Java开发工具包&#xff0c;包含了JRE和开发工具&#xff08;如javac编译器和ja…

在win系统上做生信数据分析如何快速检查和填写正确的文件路径

在win系统上做生信数据分析如何快速检查和填写正确的文件路径&#xff0c;避免分析报错 文件路径没有填写正确导致分析报错或闪退的问题 视频教程 https://www.bilibili.com/video/BV1mUmHYiESC/ (在win系统上做生信数据分析如何快速检查和填写正确的文件路径&#xff0c;避…

JDK17下,使用SHA1算法报Certificates do not conform to algorithm constraints错误

JDK17从17.0.5开始&#xff0c;默认不再允许使用SHA1算法&#xff0c;如果引用的jar包或代码里使用了SHA1算法&#xff0c;会报以下错误。 Caused by: javax.net.ssl.SSLHandshakeException: Certificates do not conform to algorithm constraintsat java.base/sun.security.…

Python3 接口自动化测试,HTTPS下载文件(GET方法和POST方法)

Python3 接口自动化测试,HTTPS下载文件(GET方法和POST方法) requests-pkcs12 PyPI python中如何使用requests模块下载文件并获取进度提示 1、GET方法 1.1、调用 # 下载客户端(GET)def download_client_get(self, header_all):try:url = self.host + "/xxx/v1/xxx-mod…

前端_005_Nodejs

文章目录 npm包管理器cjs和mjsYarn包管理器 1.Node.js 是js的一个运行环境&#xff0c;从nodejs诞生后js代码不局限于只在浏览器中执行&#xff0c;此外还能再nodejs里写服务端&#xff0c;用js可以前后端全栈开发 2.Node.js不跟浏览器一样默认含有document,window对象&#xf…

进程线程知识总结

1. 程序什么时候应该使用线程&#xff0c;什么时候单线程效率高 使用线程&#xff1a;在I/O密集型或高并发的场景&#xff0c;例如网络服务、文件读写等。通过多线程可以同时处理多个任务&#xff0c;提高利用率。单线程效率高&#xff1a;在CPU密集型任务中&#xff0c;当任务…