springboot如何整合elasticsearch8.2.0

springboot整合elasticsearch8.2.0

一、application.yml或者其他的配置文件皆可

两种方式:【1】

elasticsearch:
  hosts: 127.0.0.1:9200     # 如果有多个IP就自己加逗号吧

【2】

spring:
	elasticsearch:
    	uris: localhost:9200   #这样子自动配置了

二、config类

不同的版本会有不同的config类要求,这里只提供es8.2.0的,对应着上面两种application文件,此处也提供两种对应的config类,至于之后和springboot整合就不会出现分类了。

【1】自定义属性
/**
 * @Author Caosen
 * @Date 2022/9/22 10:34
 * @Version 1.0
 */
@Configuration
public class EsUtilConfigClint2 {

    @Value("elasticsearch.hosts")
    private String hosts;

    private HttpHost[] getHttpHost(){
        if (hosts.length() > 0){
            System.out.println(hosts);

        }
        else {
            throw new RuntimeException("invalid");
        }
        String[] hosts_array = hosts.split(",");
        //用string类型创建host的集合

        HttpHost[] httpHosts = new HttpHost[hosts_array.length];

        int i = 0;
        for (String s : hosts_array) {
            //这里解析端口
            String[] hosts_array_in = s.split(":");
            //到这里就有了id和端口两个东西
            HttpHost http = new HttpHost(hosts_array_in[0], Integer.parseInt(hosts_array_in[1]), "http");
            httpHosts[i++] = http;

        }
        System.out.println("目前的配置加入了" + i + "个id及其端口");
        return httpHosts;
    }

    /**
     * 客户端
     * @return
     * @throws IOException
     */
    @Bean
    public ElasticsearchClient configClint() throws IOException {
        // Create the low-level client
        HttpHost[] httpHosts = getHttpHost();
        RestClient restClient = RestClient.builder(httpHosts).build();

        // Create the transport with a Jackson mapper
        ElasticsearchTransport transport = new RestClientTransport(
                restClient, new JacksonJsonpMapper());

        // 客户端
        ElasticsearchClient client = new ElasticsearchClient(transport);

        return client;
    }
}

【2】使用自带的属性

/**
 * @Author Caosen
 * @Date 2022/9/18 15:01
 * @Version 1.0
 */
@Configuration
public class EsUtilConfigClint {
    /**
     * 客户端
     * @return
     * @throws IOException
     */
    public ElasticsearchClient configClint() throws IOException {
        // Create the low-level client
        RestClient restClient = RestClient.builder(
                new HttpHost("127.0.0.1", 9200)).build();

        // Create the transport with a Jackson mapper
        ElasticsearchTransport transport = new RestClientTransport(
                restClient, new JacksonJsonpMapper());

        // 客户端
        ElasticsearchClient client = new ElasticsearchClient(transport);

        return client;
    }

}

三、测试

【1】service接口

由于作者是直接在项目里面加内容的,可能会出现一些不相关的东西,我尽量截取相关代码

/**
     * 从数据库中导入所有商品到ES
     */
    int importAll();

    /**
     * 新建指定名称的索引
     * @param name
     * @throws IOException
     */
    void addIndex(String name) throws IOException;

    /**
     * 检查指定名称的索引是否存在
     * @param name
     * @return
     * @throws IOException
     */
    boolean indexExists(String name) throws IOException;

    /**
     * 删除指定索引
     * @param name
     * @throws IOException
     */
    void delIndex(String name) throws IOException;

    /**
     * 创建索引,指定setting和mapping
     * @param name 索引名称
     * @param settingFn 索引参数
     * @param mappingFn 索引结构
     * @throws IOException
     */
    void create(String name,
                Function<IndexSettings.Builder, ObjectBuilder<IndexSettings>> settingFn,
                Function<TypeMapping.Builder, ObjectBuilder<TypeMapping>> mappingFn) throws IOException;




【2】serviceImpl,主要看create ,add,exsits, delete
@Autowired
    private EsProductDao esProductDao;

    @Autowired
    private EsProductRepository esProductRepository;

    @Autowired
    private ElasticsearchClient elasticsearchClient;

    @Override
    public int importAll() {
        List<EsProduct> allEsProductList = esProductDao.getAllEsProductList(null);
        Iterable<EsProduct> esProducts = esProductRepository.saveAll(allEsProductList);
        Iterator<EsProduct> iterator = esProducts.iterator();
        int result = 0;
        while (iterator.hasNext()) {
            result++;
            iterator.next();
        }
        return result;
    }

    @Override
    public void addIndex(String name) throws IOException {
        elasticsearchClient.indices().create(b -> b.index(name));

    }

    @Override
    public boolean indexExists(String name) throws IOException {
        return elasticsearchClient.indices().exists(b -> b.index(name)).value();
    }

    @Override
    public void delIndex(String name) throws IOException {
        elasticsearchClient.indices().delete(b -> b.index(name));
    }

    @Override
    public void create(String name, Function<IndexSettings.Builder, ObjectBuilder<IndexSettings>> setting, Function<TypeMapping.Builder, ObjectBuilder<TypeMapping>> mapping) throws IOException {
        elasticsearchClient.indices()
                .create(b -> b
                        .index(name)
                        .settings(setting)
                        .mappings(mapping));
    }
【3】测试

测试可以用controller 或者 用test伪装controller,自己测试的时候还是用postman接口比较合适。这里测试两种创建index,一种简单,一种稍微复杂,结果都是通过,见图。其他的删除啊,导入数据库的数据(repository)都是可以查到的。

@Test
    void addIndexSimple() throws IOException {
        String s = "simple";

        esProductService.addIndex(s);
        System.out.println("创建success");
    }
    @Test
    void addIndexComplicated() throws IOException {

        String s = "complicated";
        Function<IndexSettings.Builder, ObjectBuilder<IndexSettings>> setting = builder -> builder
                .index(i -> i.numberOfShards("3").numberOfReplicas("1"));
        Property keywordproperty = Property.of(p -> p.keyword(k -> k.ignoreAbove(256)));
        Property testproperty = Property.of(p -> p.text(builder -> builder));
        Property integerproperty = Property.of(builder -> builder.integer(i -> i));

        Function<TypeMapping.Builder, ObjectBuilder<TypeMapping>> mapping = builder -> builder
                .properties("name", keywordproperty)
                .properties("description", testproperty)
                .properties("price", integerproperty);
        esProductService.create(s, setting, mapping);

    }

ilder -> builder
.properties(“name”, keywordproperty)
.properties(“description”, testproperty)
.properties(“price”, integerproperty);
esProductService.create(s, setting, mapping);

}
![在这里插入图片描述](https://img-blog.csdnimg.cn/c442d8d65096466389acb07dd7ab447b.png)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是SpringBoot整合Elasticsearch的步骤: 1. 添加Elasticsearch的依赖 在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> ``` 2. 配置Elasticsearch连接 在application.yml或application.properties中添加以下配置: ``` spring: data: elasticsearch: cluster-name: elasticsearch # Elasticsearch集群名称 cluster-nodes: 127.0.0.1:9300 # Elasticsearch连接地址 ``` 3. 创建Elasticsearch实体类 创建一个Java类,使用@Document注解标记为Elasticsearch文档类型,并使用其他注解指定属性的映射关系,如下所示: ``` @Document(indexName = "my_index", type = "my_type") public class MyDocument { @Id private String id; private String title; private String content; // ... 省略getter和setter方法 } ``` 4. 创建Elasticsearch操作接口 创建一个接口,继承ElasticsearchRepository接口,并指定泛型为步骤3中创建的实体类,如下所示: ``` public interface MyDocumentRepository extends ElasticsearchRepository<MyDocument, String> { } ``` 5. 使用Elasticsearch操作数据 在需要使用Elasticsearch的地方注入MyDocumentRepository,即可使用其提供的方法进行数据的CRUD操作,如下所示: ``` @Autowired private MyDocumentRepository repository; public void save(MyDocument document) { repository.save(document); } public MyDocument findById(String id) { return repository.findById(id).orElse(null); } public void deleteById(String id) { repository.deleteById(id); } ``` 以上就是SpringBoot整合Elasticsearch的基本步骤,希望对你有帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值