spring boot es | spring boot 整合elasticsearch | spring boot整合多数据源es

这篇具有很好参考价值的文章主要介绍了spring boot es | spring boot 整合elasticsearch | spring boot整合多数据源es。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

目录

Spring Boot与ES版本对应

Maven依赖

配置类

使用方式

@Test中注入方式

@Component中注入方式

查询文档

实体类

通过ElasticsearchRestTemplate查询

通过JPA查询

保存文档

参考链接


项目组件版本:

Spring Boot:2.2.13.RELEASE

Elasticsearch:6.8.0

JDK:1.8.0_66

Spring Boot与ES版本对应

spring boot es | spring boot 整合elasticsearch | spring boot整合多数据源es

Tips: 主要看第3列和第5列,根据ES版本选择对应的Spring Boot版本,如果ES和Spring Boot版本不一致后续会报错。

Maven依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.13.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<!-- 其他无关内容省略 -->

 <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        <version>2.2.13.RELEASE</version>
    </dependency>
</dependencies>

配置类

通过配置类定义两个ES链接的elasticsearchClient,如果是一个连接删除其中一个即可。

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.reactor.IOReactorConfig;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
import org.springframework.data.elasticsearch.core.ElasticsearchEntityMapper;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.EntityMapper;
import org.springframework.http.HttpHeaders;

/**
 * @author He Changjie on 2022/6/6 14:02
 */
@Configuration
public class ElasticSearchConfig extends AbstractElasticsearchConfiguration{
    /** ES链接一 [host:port] */
    @Value("${spring.data.elasticsearch.client.reactive.endpoints}")
    private String endpoints;
    /** ES链接二 [host:port] */
    @Value("${spring.data.elasticsearch.client.reactive.endpoints.zeek}")
    private String endpointsZeek;
    /** 连接elasticsearch超时时间 */
    @Value("${spring.data.elasticsearch.client.reactive.connection-timeout}")
    private Integer connectTimeout;
    /** 套接字超时时间 */
    @Value("${spring.data.elasticsearch.client.reactive.socket-timeout}")
    private Integer socketTimeout;

    /** 用户名 */
    @Value("${spring.data.elasticsearch.client.reactive.username}")
    private String username;
    /** 密码 */
    @Value("${spring.data.elasticsearch.client.reactive.password}")
    private String password;

    @Bean("elasticsearchRestTemplate")
    @Primary
    public ElasticsearchRestTemplate elasticsearchTemplate() {
        return new ElasticsearchRestTemplate(elasticsearchClient());
    }

    /**
     * 构建方式一
     */
    @Bean("restHighLevelClient")
    @Primary
    @Override
    public RestHighLevelClient elasticsearchClient() {
        // 初始化 RestClient, hostName 和 port 填写集群的内网 IP 地址与端口
        final String host = StringUtils.substringBefore(endpoints, ":");
        final int port = Integer.parseInt(StringUtils.substringAfter(endpoints, ":"));
        RestClientBuilder builder = RestClient.builder(new HttpHost(host, port))
                .setRequestConfigCallback(config -> {
                    config.setConnectTimeout(connectTimeout);
                    config.setSocketTimeout(socketTimeout);
                    return config;
                });
        //保活策略
        builder.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder
                .setDefaultIOReactorConfig(IOReactorConfig.custom()
                        .setSoKeepAlive(true)
                        .build()));
        // 设置认证信息
        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
        builder.setHttpClientConfigCallback(httpAsyncClientBuilder -> {
            httpAsyncClientBuilder.disableAuthCaching();
            return httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
        });

        return new RestHighLevelClient(builder);
    }

    @Bean("zeekElasticsearchTemplate")
    public ElasticsearchRestTemplate ZeekElasticsearchTemplate() {
        return new ElasticsearchRestTemplate(zeekRestHighLevelClient());
    }

    /**
     * 构建方式二
     */
    @Bean("zeekRestHighLevelClient")
    public RestHighLevelClient zeekRestHighLevelClient() {
        HttpHeaders defaultHeaders = new HttpHeaders();
        defaultHeaders.setBasicAuth(username, password);
        ClientConfiguration clientConfiguration = ClientConfiguration.builder()
                .connectedTo(endpointsZeek)
                .withConnectTimeout(connectTimeout)
                .withSocketTimeout(socketTimeout)
                .withDefaultHeaders(defaultHeaders)
                .withBasicAuth(username, password)
                .build();
        return RestClients.create(clientConfiguration).rest();
    }

    @Bean
    @Override
    public EntityMapper entityMapper() {
        ElasticsearchEntityMapper entityMapper = new ElasticsearchEntityMapper(elasticsearchMappingContext(),
                new DefaultConversionService());
        entityMapper.setConversions(elasticsearchCustomConversions());
        return entityMapper;
    }
}

Tips:

  1. 配置类中定义RestHighLevelClient使用了两个种方式,任选其中一种都可以
  2. 必须选择一个ES链接打上@Primary注解
  3. 示例中两个连接都需要密码,且密码相同

使用方式

elasticsearchRestTemplate的使用在@Test中和其他@Component中注入方式不同(亲测),在@Component中直接使用@Resource注入ElasticsearchRestTemplate会报找不到对应的Bean。

@Test中注入方式

@Resource(name = "elasticsearchRestTemplate")
private ElasticsearchRestTemplate elasticsearchRestTemplate;
@Resource(name = "zeekElasticsearchTemplate")
private ElasticsearchRestTemplate zeekElasticsearchTemplate;

@Component中注入方式

@Service
public class DemoServiceImpl implements DemoService {
    private final ElasticsearchRestTemplate elasticsearchRestTemplate;
    private final ElasticsearchRestTemplate zeekElasticsearchRestTemplate;

    @Autowired
    public DemoServiceImpl(RestHighLevelClient restHighLevelClient,
    					@Qualifier(value = "zeekRestHighLevelClient") RestHighLevelClient zeekRestHighLevelClient) {
        this.elasticsearchRestTemplate = new ElasticsearchRestTemplate(restHighLevelClient);
        this.zeekElasticsearchRestTemplate = new ElasticsearchRestTemplate(zeekRestHighLevelClient);
    }
}

查询文档

实体类

import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import java.io.Serializable;

@Data
@Document(indexName = "demo-index-20220609", type = "log")
public class QdnsLogs implements Serializable {
    @Id
    private String _id;
    @Field(type = FieldType.Keyword)
    private String name;
    @Field(type = FieldType.Keyword)
    private String address;
    // ........
    @Field(type = FieldType.Date, name = "timestamp")
    private Long timestamp;
}

Tips:

  1. 该类具体内容进行了脱敏
  2. 需要特别注意@Document的type一定要和es中的_type一致,否则查询结果为是空
  3. 如果不需要保存文档,可以不要@Field注解

通过ElasticsearchRestTemplate查询

import com.xxx.entity.es.Eth0Logs;
import com.xxx.entity.es.QdnsLogs;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.metrics.tophits.TopHits;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.query.*;
import javax.annotation.Resource;
import java.util.List;

/**
 * 实现描述:
 *
 * @author Hecj
 * @version v 1.0.0
 * @since 2022/06/17
 */
@SpringBootTest(classes = Application.class)
public class EsTest {
    @Resource(name = "elasticsearchRestTemplate")
    private ElasticsearchRestTemplate elasticsearchRestTemplate;
    @Resource(name = "zeekElasticsearchTemplate")
    private ElasticsearchRestTemplate zeekElasticsearchTemplate;

    /**
     * 通过时间范围和是否存在某一字段查询
     */
    @Test
    void test1(){
        SearchQuery searchQuery  = new NativeSearchQueryBuilder()//查询数据,构造出一个查询
                .withQuery(QueryBuilders.boolQuery().must(QueryBuilders.rangeQuery("timestamp").from(1654506000000L).to(1654507340785L)).must(QueryBuilders.existsQuery("name")))
                .build();//构造一个SearchQuery
        List<QdnsLogs> list = elasticsearchRestTemplate.queryForList(searchQuery, QdnsLogs.class);
        System.out.println(list.size());
    }

    /**
     * 通过name值等于特定值
     */
    @Test
    void test2(){
        SearchQuery searchQuery  = new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.termQuery("name", "zhangsan"))
                .build();//构造一个SearchQuery
        List<Eth0Logs> eth0Logs = zeekElasticsearchTemplate.queryForList(searchQuery, Eth0Logs.class);
        System.out.println(eth0Logs.size());
        for (Eth0Logs log : eth0Logs) {
            System.out.println(log.getTs());
        }
    }

    /**
     * aggs查询
     * 
     * 查询指定时间范围内存在name值的记录,进行通过name聚合,按照时间倒序排序取最新一条记录
     */
    @Test
    void test3() {
        SearchQuery searchQuery  = new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.boolQuery()
                        .must(
                                QueryBuilders.rangeQuery("timestamp")
                                        .from(1654506000000L)
                                        .to(1654507340785L)
                        )
                        .must(QueryBuilders.existsQuery("name")))
                .addAggregation(AggregationBuilders.terms("name")
                        .field("name")
                        .size(10000)
                        .subAggregation(
                                AggregationBuilders.topHits("top")
                                        .sort("timestamp", SortOrder.DESC)
                                        .size(1)
                        )
                )
                .build();
        AggregatedPage<QdnsLogs> logs = elasticsearchRestTemplate.queryForPage(searchQuery, QdnsLogs.class);
        ParsedStringTerms fqdn = (ParsedStringTerms)logs.getAggregation("name");
        List<? extends Terms.Bucket> buckets = fqdn.getBuckets();
        for (Terms.Bucket entry : buckets) {
            String key = entry.getKeyAsString();
            TopHits topHits= entry.getAggregations().get("top");
            SearchHits hits = topHits.getHits();
            SearchHit at = hits.getAt(0);
            System.out.println(key + "-" + at);
        }
    }
}

Tips: 部分包完整名称进行了脱敏

通过JPA查询

这里的接口不需要添加@Service,通过JPA方式需要特别注意书写规范,字段名称的正确性。

interface BookRepository extends Repository<Book, String> {
  List<Book> findByNameAndPrice(String name, Integer price);
}

相当于:

{
    "query": {
        "bool" : {
            "must" : [
                { "query_string" : { "query" : "?", "fields" : [ "name" ] } },
                { "query_string" : { "query" : "?", "fields" : [ "price" ] } }
            ]
        }
    }
}

Table 2. Supported keywords inside method names

Keyword Sample Elasticsearch Query String

And

findByNameAndPrice

{ "query" : { "bool" : { "must" : [ { "query_string" : { "query" : "?", "fields" : [ "name" ] } }, { "query_string" : { "query" : "?", "fields" : [ "price" ] } } ] } }}

Or

findByNameOrPrice

{ "query" : { "bool" : { "should" : [ { "query_string" : { "query" : "?", "fields" : [ "name" ] } }, { "query_string" : { "query" : "?", "fields" : [ "price" ] } } ] } }}

Is

findByName

{ "query" : { "bool" : { "must" : [ { "query_string" : { "query" : "?", "fields" : [ "name" ] } } ] } }}

Not

findByNameNot

{ "query" : { "bool" : { "must_not" : [ { "query_string" : { "query" : "?", "fields" : [ "name" ] } } ] } }}

Between

findByPriceBetween

{ "query" : { "bool" : { "must" : [ {"range" : {"price" : {"from" : ?, "to" : ?, "include_lower" : true, "include_upper" : true } } } ] } }}

LessThan

findByPriceLessThan

{ "query" : { "bool" : { "must" : [ {"range" : {"price" : {"from" : null, "to" : ?, "include_lower" : true, "include_upper" : false } } } ] } }}

LessThanEqual

findByPriceLessThanEqual

{ "query" : { "bool" : { "must" : [ {"range" : {"price" : {"from" : null, "to" : ?, "include_lower" : true, "include_upper" : true } } } ] } }}

GreaterThan

findByPriceGreaterThan

{ "query" : { "bool" : { "must" : [ {"range" : {"price" : {"from" : ?, "to" : null, "include_lower" : false, "include_upper" : true } } } ] } }}

GreaterThanEqual

findByPriceGreaterThan

{ "query" : { "bool" : { "must" : [ {"range" : {"price" : {"from" : ?, "to" : null, "include_lower" : true, "include_upper" : true } } } ] } }}

Before

findByPriceBefore

{ "query" : { "bool" : { "must" : [ {"range" : {"price" : {"from" : null, "to" : ?, "include_lower" : true, "include_upper" : true } } } ] } }}

After

findByPriceAfter

{ "query" : { "bool" : { "must" : [ {"range" : {"price" : {"from" : ?, "to" : null, "include_lower" : true, "include_upper" : true } } } ] } }}

Like

findByNameLike

{ "query" : { "bool" : { "must" : [ { "query_string" : { "query" : "?*", "fields" : [ "name" ] }, "analyze_wildcard": true } ] } }}

StartingWith

findByNameStartingWith

{ "query" : { "bool" : { "must" : [ { "query_string" : { "query" : "?*", "fields" : [ "name" ] }, "analyze_wildcard": true } ] } }}

EndingWith

findByNameEndingWith

{ "query" : { "bool" : { "must" : [ { "query_string" : { "query" : "*?", "fields" : [ "name" ] }, "analyze_wildcard": true } ] } }}

Contains/Containing

findByNameContaining

{ "query" : { "bool" : { "must" : [ { "query_string" : { "query" : "*?*", "fields" : [ "name" ] }, "analyze_wildcard": true } ] } }}

In

findByNameIn(Collection<String>names)

{ "query" : { "bool" : { "must" : [ {"bool" : {"must" : [ {"terms" : {"name" : ["?","?"]}} ] } } ] } }}

NotIn

findByNameNotIn(Collection<String>names)

{ "query" : { "bool" : { "must" : [ {"bool" : {"must_not" : [ {"terms" : {"name" : ["?","?"]}} ] } } ] } }}

Near

findByStoreNear

Not Supported Yet !

True

findByAvailableTrue

{ "query" : { "bool" : { "must" : [ { "query_string" : { "query" : "true", "fields" : [ "available" ] } } ] } }}

False

findByAvailableFalse

{ "query" : { "bool" : { "must" : [ { "query_string" : { "query" : "false", "fields" : [ "available" ] } } ] } }}

OrderBy

findByAvailableTrueOrderByNameDesc

{ "query" : { "bool" : { "must" : [ { "query_string" : { "query" : "true", "fields" : [ "available" ] } } ] } }, "sort":[{"name":{"order":"desc"}}] }

保存文档

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import java.util.List;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;

// 这里的dataList是需要保存到ES的bean集合,各位自行替换
List<IndexQuery> queries = dataList.stream().map(e -> {
        IndexQuery query = new IndexQuery();
        // 这个自行替换,也可以省略
        query.setId(IdUtil.simpleUUID());
        // 具体的数据
        query.setObject(e);
        // 索引名称
        query.setIndexName("demo-index-20220609");
        // 索引类型
        query.setType("log");
        return query;
    }).collect(Collectors.toList());
    
    if(CollectionUtil.isNotEmpty(queries)){
        zeekElasticsearchTemplate.bulkIndex(queries);
        log.info("#~ 写入日志成功,写入条数:{}", queries.size());
    }

参考链接

Spring Data版本依赖矩阵

elasticsearch官方手册文章来源地址https://www.toymoban.com/news/detail-416923.html

到了这里,关于spring boot es | spring boot 整合elasticsearch | spring boot整合多数据源es的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【Spring Boot 3】【数据源】自定义JPA数据源

    软件开发是一门实践性科学,对大多数人来说,学习一种新技术不是一开始就去深究其原理,而是先从做出一个可工作的DEMO入手。但在我个人学习和工作经历中,每次学习新技术总是要花费或多或少的时间、检索不止一篇资料才能得出一个可工作的DEMO,这占用了我大量的时

    2024年01月21日
    浏览(53)
  • 【Spring Boot 3】【数据源】自定义JDBC多数据源

    软件开发是一门实践性科学,对大多数人来说,学习一种新技术不是一开始就去深究其原理,而是先从做出一个可工作的DEMO入手。但在我个人学习和工作经历中,每次学习新技术总是要花费或多或少的时间、检索不止一篇资料才能得出一个可工作的DEMO,这占用了我大量的时

    2024年01月23日
    浏览(37)
  • 【Spring Boot 3】【数据源】自定义JPA多数据源

    软件开发是一门实践性科学,对大多数人来说,学习一种新技术不是一开始就去深究其原理,而是先从做出一个可工作的DEMO入手。但在我个人学习和工作经历中,每次学习新技术总是要花费或多或少的时间、检索不止一篇资料才能得出一个可工作的DEMO,这占用了我大量的时

    2024年01月22日
    浏览(60)
  • Spring Boot 配置双数据源

    Survive by day and develop by night. talk for import biz , show your perfect code,full busy,skip hardness,make a better result,wait for change,challenge Survive. happy for hardess to solve denpendies. 需求: 1.基本步骤 添加依赖 添加 Spring Boot 和数据库驱动的依赖 配置数据源 在 application.properties 或 application.yml 中分别配

    2024年01月22日
    浏览(45)
  • ElasticSearch多数据源配置,连接多个ES集群

    开发时遇到需要连接多个ES的需求,类似于连接多个MySQL数据库一样。 Elasticsearch Java API有四类client连接方式 TransportClient RestClient Jest Spring Data Elasticsearch         其中TransportClient和RestClient是Elasticsearch原生的api。TransportClient可以支持2.x,5.x版本,TransportClient将会在Elasticsea

    2023年04月14日
    浏览(32)
  • Spring Boot配置多个Kafka数据源

    application.properties配置文件如下 1.第一个kakfa 2.第二个kakfa 备注: 生产者消费者代码参考链接,开发同学需要以实际情况按要求自己变更下代码即可: Spring Boot 集成多个 Kafka_springboot集成多个kafka_//承续缘_纪录片的博客-CSDN博客

    2024年02月07日
    浏览(57)
  • 实例讲解Spring boot动态切换数据源

    摘要: 本文模拟一下在主库查询订单信息查询不到的时候,切换数据源去历史库里面查询。 本文分享自华为云社区《springboot动态切换数据源》,作者:小陈没烦恼 。 在公司的系统里,由于数据量较大,所以配置了多个数据源,它会根据用户所在的地区去查询那一个数据库

    2024年02月06日
    浏览(32)
  • 【Java】Spring Boot配置动态数据源

    1.1 创建动态数据源 通过实现Spring提供的AbstractRoutingDataSource类,可以实现自己的数据源选择逻辑,从而可以实现数据源的动态切换。 1.2 创建动态数据源配置类 跟配置静态多数据源一样,需要手动配置下面的三个 Bean,只不过DynamicDataSource类的targetDataSources是空的。 1.3 创建动

    2024年02月09日
    浏览(38)
  • 如何在Spring Boot中配置双数据源?

    在许多应用程序中, 可能会遇到需要连接多个数据库的情况 。这些数据库可以是不同的类型,例如关系型数据库和NoSQL数据库,或者它们可以是相同类型但包含不同的数据。为了处理这种情况,我们可以使用双数据源来管理多个数据库连接。 双数据源是指在一个应用程序中

    2024年02月11日
    浏览(37)
  • Spring Boot 多数据源及事务解决方案

    一个主库和N个应用库的数据源,并且会同时操作主库和应用库的数据,需要解决以下两个问题: 如何动态管理多个数据源以及切换? 如何保证多数据源场景下的数据一致性(事务)? 本文主要探讨这两个问题的解决方案,希望能对读者有一定的启发。 通过扩展Spring提供的抽象

    2024年02月10日
    浏览(26)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包