es配合java使用--代码示范

本文介绍了如何在本地搭建Elasticsearch环境,并通过Java进行配合使用。详细讲述了配置文件的设置,Elasticsearch工具类和服务的创建,以及提供了数据列表和分页查询的方法。

本地搭建es镜像环境及谷歌插件可视化环境

https://blog.csdn.net/qq_60191219/article/details/125867721?spm=1001.2014.3001.5501

一:配置文件 --用于配置链接

elasticsearch:
  host: 127.0.0.1
  port: 9200
  scheme: http
  delayTime: 10
  index:
    event: event
    people: people
    place: place

二:es工具类 --为service提供方法

package com.spacesystech.config;

import lombok.Getter;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHost;
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 javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.io.IOException;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Getter
@Configuration
public class ElasticSearchConfig {

    @Value("${elasticsearch.host}")
    private String esHost;

    @Value("${elasticsearch.port}")
    private int esPort;

    @Value("${elasticsearch.scheme}")
    private String scheme;

    @Value("${elasticsearch.index.event:event}")
    private String eventIndex;

    @Value("${elasticsearch.index.people:people}")
    private String peopleIndex;

    @Value("${elasticsearch.index.place:place}")
    private String placeIndex;

    private RestHighLevelClient client;

    @PostConstruct
    public void init(){
        HttpHost[] httpHosts = Stream.of(StringUtils.split(esHost, ","))
               
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值