ES教程-springboot整合ES

1、springboot如何整合ES

1、导入坐标 (下面是老版本的es坐标官方已经不推荐使用)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

高版本的es坐标
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>

2、在配置文件设置es服务器的连接

3、使用ES 注入依赖 创建索引 添加数据 批量添加数据
@Autowired
private RestHighLevelClient restHighLevelClient;

/**
* es创建索引库流程
* @throws IOException
*/
@Test
public void testCreatedIndex() throws IOException {

//获取索引库对象
IndicesClient indicesClient = restHighLevelClient.indices();

//创建索引库请求
CreateIndexRequest createIndexRequest = new CreateIndexRequest("java2022");

//设置索引库的输入格式
createIndexRequest.source("{\n" +
" \"settings\":{\n" +
" \"number_of_shards\" : 2,\n" +
" \"number_of_replicas\" : 0\n" +
" }\n" +
"}", XContentType.JSON);

//建表使用mapping创建映射,数据格式依然为Json
createIndexRequest.mapping("{\n" +
" \"_source\": {\n" +
" \"excludes\":[\"description\"]\n" +
" }, \n" +
" \t\"properties\": {\n" +
" \"name\": {\n" +
" \"type\": \"text\",\n" +
" \"analyzer\":\"ik_max_word\",\n" +
" \"search_analyzer\":\"ik_smart\"\n" +
" },\n" +
" \"description\": {\n" +
" \"type\": \"text\",\n" +
" \"analyzer\":\"ik_max_word\",\n" +
" \"search_analyzer\":\"ik_smart\"\n" +
" },\n" +
" \"studymodel\": {\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"price\": {\n" +
" \"type\": \"float\"\n" +
" },\n" +
" \"pic\":{\n" +
"\t\t \"type\":\"text\",\n" +
"\t\t \"index\":false\n" +
"\t }\n" +
" }\n" +
"}", XContentType.JSON);
indicesClient.create(createIndexRequest,RequestOptions.DEFAULT);
}

/**
* es添加数据
* @throws IOException
*/
@Test
public void testAddDoc() throws IOException {

IndexRequest indexRequest = new IndexRequest("java2022","course","1");

indexRequest.source("{\n" +
" \"name\":\"spring cloud实战\",\n" +
" \"description\":\"本课程主要从四个章节进行讲解: 1.微服务架构入门 2.spring cloud 基础入门 3.实战Spring Boot 4.注册中心eureka。\",\n" +
" \"studymodel\":\"201001\",\n" +
" \"price\":5.6\n" +
"}",XContentType.JSON);
restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
}

/**
* 批量添加数据
* @throws IOException
*/
@Test
public void testAddBulkRequest() throws IOException {
//设置批量添加请求
BulkRequest bulkRequest = new BulkRequest();

//假设定义一个文章的数组
String[] stringList = new String[2];
stringList[0] = "\"{\\n\" +\n" +
" \" \\\"name\\\":\\\"spring cloud实战1\\\",\\n\" +\n" +
" \" \\\"description\\\":\\\"本课程主要从四个章节进行讲解: 1.微服务架构入门 2.spring cloud 基础入门 3.实战Spring Boot 4.注册中心eureka。\\\",\\n\" +\n" +
" \" \\\"studymodel\\\":\\\"201001\\\",\\n\" +\n" +
" \" \\\"price\\\":5.6\\n\" +\n" +
" \"}\"";
stringList[1] = "\"{\\n\" +\n" +
" \" \\\"name\\\":\\\"spring cloud实战2\\\",\\n\" +\n" +
" \" \\\"description\\\":\\\"本课程主要从四个章节进行讲解: 1.微服务架构入门 2.spring cloud 基础入门 3.实战Spring Boot 4.注册中心eureka。\\\",\\n\" +\n" +
" \" \\\"studymodel\\\":\\\"201001\\\",\\n\" +\n" +
" \" \\\"price\\\":5.6\\n\" +\n" +
" \"}\"";
for (String stringItem : stringList){
System.out.println(stringItem);
//创建单条请求
IndexRequest indexRequest = new IndexRequest("java2022");
//设置数据信息 数据格式为json
//此处的数据格式需要转化为json
indexRequest.source(stringItem,XContentType.JSON);
bulkRequest.add(indexRequest);
}
restHighLevelClient.bulk(bulkRequest,RequestOptions.DEFAULT);
}

 

posted @ 2022-11-20 17:40  Paul15963  阅读(1319)  评论(0编辑  收藏  举报