spring boot学习第八篇:操作elastic search的索引和索引中的数据

2024-01-25 16:12

本文主要是介绍spring boot学习第八篇:操作elastic search的索引和索引中的数据,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前提参考:elastic search入门-CSDN博客

前提说明:已经安装好了elastic search 7.x版本,我的es版本是7.11.1

1、 pom.xml文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<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 https://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.6.4</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.hmblogs</groupId><artifactId>hmblogs</artifactId><version>0.0.1-SNAPSHOT</version><name>hmblogs</name><description>hmblogs</description><properties><java.version>8</java.version><druid.version>1.2.8</druid.version><log4jdbc.version>1.16</log4jdbc.version><es.version>7.9.2</es.version></properties><dependencies><!-- druid数据源驱动 --><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>${druid.version}</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- mybatis --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.3.1</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><!--Mysql依赖包--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><!--lombok插件--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><!--监控sql日志--><dependency><groupId>org.bgee.log4jdbc-log4j2</groupId><artifactId>log4jdbc-log4j2-jdbc4.1</artifactId><version>${log4jdbc.version}</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.9</version></dependency><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId></dependency><dependency><groupId>org.apache.kafka</groupId><artifactId>kafka-clients</artifactId></dependency><dependency><groupId>org.springframework.kafka</groupId><artifactId>spring-kafka</artifactId></dependency><!-- high client--><dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><version>${es.version}</version><exclusions><exclusion><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId></exclusion><exclusion><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-client</artifactId></exclusion></exclusions></dependency><!-- rest-high-level-client 依赖如下2个jar --><dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId><version>${es.version}</version></dependency><dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-client</artifactId><version>${es.version}</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

2、application.yml文件内容如下:

server:port: 8081servlet.context-path: /#配置数据源
spring:datasource:druid:db-type: com.alibaba.druid.pool.DruidDataSourcedriverClassName: net.sf.log4jdbc.sql.jdbcapi.DriverSpyurl: jdbc:log4jdbc:mysql://${DB_HOST:localhost}:${DB_PORT:3306}/${DB_NAME:eladmin}?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=falseusername: ${DB_USER:root}password: ${DB_PWD:123456}redis:host: localhostport: 6379password: hemingdatabase: 10es:host: 43.138.0.199port: 9200scheme: http

3、BackendApplication.java文件内容如下:

package com.hmblogs.backend;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class BackendApplication {public static void main(String[] args) {SpringApplication.run(BackendApplication.class, args);}}

4、测试验证,ElasticsearchClientTest.java文件内容如下:


import com.alibaba.fastjson.JSONObject;
import com.hmblogs.backend.entity.Users;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.stereotype.Component;
import org.springframework.test.context.junit4.SpringRunner;import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;@Slf4j
//@Component
@RunWith(SpringRunner.class)
@SpringBootTest
public class ElasticsearchClientTest {@Autowiredprivate RestHighLevelClient client;String index = "users";/*** 创建索引** @throws IOException*/@Testpublic void createIndex() throws IOException {CreateIndexRequest indexRequest = new CreateIndexRequest(index);CreateIndexResponse response = client.indices().create(indexRequest, RequestOptions.DEFAULT);log.info("创建索引:"+response.isAcknowledged());}/*** 判断索引是否存在** @throws IOException*/@Testpublic void indexExists() throws IOException {GetIndexRequest request = new GetIndexRequest(index);boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);log.info("索引是否存在:"+exists);}/*** 添加文档** @throws IOException*/@Testpublic void addDoc() throws IOException {IndexRequest request = new IndexRequest(index);String source = JSONObject.toJSONString(new Users(10000, "逍遥", 30));// 手动设置id
//        request.id("10000");request.source(source, XContentType.JSON);IndexResponse response = client.index(request, RequestOptions.DEFAULT);log.info("添加文档:"+response.getResult());}/*** 批量添加文档*/@Testpublic void batchAddDoc() throws IOException {BulkRequest bulkRequest = new BulkRequest();List<IndexRequest> requests = generateRequests();for (IndexRequest indexRequest : requests) {bulkRequest.add(indexRequest);}BulkResponse responses = client.bulk(bulkRequest, RequestOptions.DEFAULT);log.info("批量添加结果:"+!responses.hasFailures());}public List<IndexRequest> generateRequests() {List<IndexRequest> requests = new ArrayList<>();requests.add(generateNewsRequest(1, "小明", 22));requests.add(generateNewsRequest(2, "隔壁老王", 30));requests.add(generateNewsRequest(3, "lily", 25));return requests;}public IndexRequest generateNewsRequest(Integer id, String name, Integer age) {IndexRequest indexRequest = new IndexRequest(index);String source = JSONObject.toJSONString(new Users(id, name, age));indexRequest.source(source, XContentType.JSON);return indexRequest;}/*** 更新文档** @throws IOException*/@Testpublic void updateDoc() throws IOException {UpdateRequest updateRequest = new UpdateRequest(index, "AmxCP40BM8-M0To1vOET");Map<String, Object> params = new HashMap<>();params.put("id", "1");params.put("name", "逍遥");params.put("age", 33);params.put("hobby", "唱歌,跳舞,网上冲浪,看电影,旅行");updateRequest.doc(params);UpdateResponse response = client.update(updateRequest, RequestOptions.DEFAULT);log.info("修改文档结果:"+response.getResult());}/*** 搜索** @throws IOException*/@Testpublic void search() throws IOException {SearchRequest request = new SearchRequest(index);SearchSourceBuilder builder = new SearchSourceBuilder();BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();boolQueryBuilder.must(new RangeQueryBuilder("age").from(20).to(30)).must(new TermQueryBuilder("id", 3));builder.query(boolQueryBuilder);request.source(builder);log.info("搜索语句为: " + request.source().toString());SearchResponse search = client.search(request, RequestOptions.DEFAULT);log.info("搜索结果为:"+search);SearchHits hits = search.getHits();SearchHit[] hitsArr = hits.getHits();for (SearchHit documentFields : hitsArr) {log.info(documentFields.getSourceAsString());}}@Testpublic void search2() {SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();sourceBuilder.from(0);sourceBuilder.size(10);sourceBuilder.fetchSource(new String[]{"name", "age"}, new String[]{});MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("hobby", "唱歌,跳舞,网上冲浪,看电影,旅行");TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "逍遥");RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age");rangeQueryBuilder.gte(20);rangeQueryBuilder.lte(40);BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();boolBuilder.must(matchQueryBuilder);boolBuilder.must(termQueryBuilder);boolBuilder.must(rangeQueryBuilder);sourceBuilder.query(boolBuilder);SearchRequest searchRequest = new SearchRequest(index);searchRequest.source(sourceBuilder);try {log.info("搜索语句为: " + searchRequest.source().toString());SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);log.info("搜索结果为:"+search);SearchHits hits = search.getHits();SearchHit[] hitsArr = hits.getHits();for (SearchHit documentFields : hitsArr) {log.info(documentFields.getSourceAsString());}} catch (IOException e) {log.error("搜索报错:{}",e);}}/*** 删除文档* @throws IOException*/@Testpublic void deleteDoc() throws IOException {DeleteRequest deleteRequest = new DeleteRequest(index, "3g8vP40Bi8WQ8ue06wWd");DeleteResponse response = client.delete(deleteRequest, RequestOptions.DEFAULT);log.info("删除结果为:"+response.getResult());}/*** 删除索引* @throws IOException*/@Testpublic void deleteIndex() throws IOException {DeleteIndexRequest request = new DeleteIndexRequest(index);AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);log.info("删除索引结果为:"+response.isAcknowledged());}}

每个方法的作用见注释说明,

 这里点击Run search2()

然后查看console的log,验证OK

这样写代码,还是不够灵活,能不能执行自定义的SQL或者DSL语句,这样就可以不用去想办法看下Builder那些类对象怎么设置了。

其实,要想实现这样的效果,就是调用http接口而已

RestTemplateUtil.java文件内容如下:


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;import java.util.HashMap;
import java.util.Map;@Component
public class RestTemplateUtil {@Autowiredprivate RestTemplate restTemplate;public String post(String url,Map map){// 设置请求头属性HttpHeaders httpHeaders = new HttpHeaders();httpHeaders.setContentType(MediaType.APPLICATION_JSON);HttpEntity httpEntity = new HttpEntity(map, httpHeaders);String results = restTemplate.postForObject(url, httpEntity, String.class);return results;}
}

ElasticsearchClientSqlDslTest.java类文件内容如下:


import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;import java.util.HashMap;
import java.util.Map;@Slf4j
//@Component
@RunWith(SpringRunner.class)
@SpringBootTest
public class ElasticsearchClientSqlDslTest {@Autowiredprivate RestTemplateUtil restTemplateUtil;@Testpublic void complexQueryEsData() {// 请求参数Map map = new HashMap<>();map.put("query", "SELECT id,name,age FROM users order by name desc limit 6");String url = "http://43.138.0.199:9200/_sql?format=json";String studentData = restTemplateUtil.post(url, map);log.info("studentData:"+studentData);}
}

执行后,查出了数据,console如下截图:

返回数据如下:

{"columns":[{"name":"id","type":"long"},{"name":"name","type":"text"},{"name":"age","type":"long"}],"rows":[[2,"隔壁老王",30],[1,"逍遥",33],[1,"小明",22],[3,"lily",25]]}

 

这篇关于spring boot学习第八篇:操作elastic search的索引和索引中的数据的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/643819

相关文章

Spring Security常见问题及解决方案

《SpringSecurity常见问题及解决方案》SpringSecurity是Spring生态的安全框架,提供认证、授权及攻击防护,支持JWT、OAuth2集成,适用于保护Spring应用,需配置... 目录Spring Security 简介Spring Security 核心概念1. ​Securit

SpringBoot+EasyPOI轻松实现Excel和Word导出PDF

《SpringBoot+EasyPOI轻松实现Excel和Word导出PDF》在企业级开发中,将Excel和Word文档导出为PDF是常见需求,本文将结合​​EasyPOI和​​Aspose系列工具实... 目录一、环境准备与依赖配置1.1 方案选型1.2 依赖配置(商业库方案)二、Excel 导出 PDF

SpringBoot改造MCP服务器的详细说明(StreamableHTTP 类型)

《SpringBoot改造MCP服务器的详细说明(StreamableHTTP类型)》本文介绍了SpringBoot如何实现MCPStreamableHTTP服务器,并且使用CherryStudio... 目录SpringBoot改造MCP服务器(StreamableHTTP)1 项目说明2 使用说明2.1

spring中的@MapperScan注解属性解析

《spring中的@MapperScan注解属性解析》@MapperScan是Spring集成MyBatis时自动扫描Mapper接口的注解,简化配置并支持多数据源,通过属性控制扫描路径和过滤条件,利... 目录一、核心功能与作用二、注解属性解析三、底层实现原理四、使用场景与最佳实践五、注意事项与常见问题六

Spring的RedisTemplate的json反序列泛型丢失问题解决

《Spring的RedisTemplate的json反序列泛型丢失问题解决》本文主要介绍了SpringRedisTemplate中使用JSON序列化时泛型信息丢失的问题及其提出三种解决方案,可以根据性... 目录背景解决方案方案一方案二方案三总结背景在使用RedisTemplate操作redis时我们针对

Java中Arrays类和Collections类常用方法示例详解

《Java中Arrays类和Collections类常用方法示例详解》本文总结了Java中Arrays和Collections类的常用方法,涵盖数组填充、排序、搜索、复制、列表转换等操作,帮助开发者高... 目录Arrays.fill()相关用法Arrays.toString()Arrays.sort()A

Spring Boot Maven 插件如何构建可执行 JAR 的核心配置

《SpringBootMaven插件如何构建可执行JAR的核心配置》SpringBoot核心Maven插件,用于生成可执行JAR/WAR,内置服务器简化部署,支持热部署、多环境配置及依赖管理... 目录前言一、插件的核心功能与目标1.1 插件的定位1.2 插件的 Goals(目标)1.3 插件定位1.4 核

如何使用Lombok进行spring 注入

《如何使用Lombok进行spring注入》本文介绍如何用Lombok简化Spring注入,推荐优先使用setter注入,通过注解自动生成getter/setter及构造器,减少冗余代码,提升开发效... Lombok为了开发环境简化代码,好处不用多说。spring 注入方式为2种,构造器注入和setter

使用zip4j实现Java中的ZIP文件加密压缩的操作方法

《使用zip4j实现Java中的ZIP文件加密压缩的操作方法》本文介绍如何通过Maven集成zip4j1.3.2库创建带密码保护的ZIP文件,涵盖依赖配置、代码示例及加密原理,确保数据安全性,感兴趣的... 目录1. zip4j库介绍和版本1.1 zip4j库概述1.2 zip4j的版本演变1.3 zip4

Java堆转储文件之1.6G大文件处理完整指南

《Java堆转储文件之1.6G大文件处理完整指南》堆转储文件是优化、分析内存消耗的重要工具,:本文主要介绍Java堆转储文件之1.6G大文件处理的相关资料,文中通过代码介绍的非常详细,需要的朋友可... 目录前言文件为什么这么大?如何处理这个文件?分析文件内容(推荐)删除文件(如果不需要)查看错误来源如何避