Springboot 2.3.2集成elasticsearch简单的样例

集成背景

elk或者快速搜索都要用到elasticsearch,源于他的速度非常快。如何采用最新的接口集成,下面就介绍一个样例。

代码示例

1. 加依赖

 <properties>
        <java.version>1.8</java.version>
        <elastic.version>7.8.0</elastic.version>
    </properties>

    <dependencies>
        <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>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
<!--        <dependency>-->
<!--            <groupId>org.elasticsearch.plugin</groupId>-->
<!--            <artifactId>transport-netty4-client</artifactId>-->
<!--            <version>7.8.0</version>-->
<!--        </dependency>-->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>${elastic.version}</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>${elastic.version}</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.71</version>
        </dependency>
    </dependencies>

2. 写代码之controller

package vip.mate.elasticsearch.controller;

import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import vip.mate.elasticsearch.entity.User;
import vip.mate.elasticsearch.service.IEsService;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@AllArgsConstructor
public class TestEsController {

    private final IEsService esService;

    @RequestMapping(value = "helloEs")
    public Map<String, Object> hello() {

        Map<String, Object> query = new HashMap<>();
        try{
            User user = User.builder()
                    .id(1L)
                    .name("张三")
                    .desc("张三是名JAVA开发工程师")
                    .build();
            String indexId = "test001";
            esService.save(user, indexId);

            User user2 = User.builder()
                    .id(2L)
                    .name("李四")
                    .desc("李四是一名数据产品经理")
                    .build();
            indexId = "test002";
            esService.save(user2, indexId);

            query = esService.query(indexId);
        }catch (IOException e){
            e.printStackTrace();
        }
//        return Result.data(query);
        return query;
    }
}

3. 写代码之entity

package vip.mate.elasticsearch.entity;

import lombok.Builder;
import lombok.Data;

import java.io.Serializable;

@Data
@Builder
public class User implements Serializable {

    private static final long serialVersionUID = 1809041336715990704L;

    private Long id;
    private String name;
    private String desc;
}

4. 写代码之service

package vip.mate.elasticsearch.service;

import vip.mate.elasticsearch.entity.User;

import java.io.IOException;
import java.util.Map;

public interface IEsService {

    boolean save(User user, String indexId) throws IOException;

    Map<String, Object> query(String indexId) throws IOException;
}

这个是业务接口类

package vip.mate.elasticsearch.service.impl;

import com.alibaba.fastjson.JSON;
import lombok.AllArgsConstructor;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.stereotype.Service;
import vip.mate.elasticsearch.entity.User;
import vip.mate.elasticsearch.service.IEsService;

import java.io.IOException;
import java.util.Map;

@Service
@AllArgsConstructor
public class EsServiceImpl implements IEsService {

    private final RestHighLevelClient restHighLevelClient;
    private final String indexName = "indexname";

    @Override
    public boolean save(User user, String indexId) throws IOException {
        String json = JSON.toJSONString(user);
        IndexRequest request = new IndexRequest(indexName).id(indexId).source(json, XContentType.JSON);
        restHighLevelClient.index(request, RequestOptions.DEFAULT);
        return true;
    }

    @Override
    public Map<String, Object> query(String indexId) throws IOException  {
        GetRequest request = new GetRequest(indexName, indexId);
        GetResponse response = restHighLevelClient.get(request, RequestOptions.DEFAULT);
        return response.getSource();
    }
}

以上是业务实现类

5.启动工程进行测试

打开URL:http://localhost:8888/helloEs
即可进行添加数据和查询数据的DEMO

代码样例

https://github.com/735728811/mate-elasticsearch-demo

以上只是最简单的一个demo,方便初学者学习用,后续会集成操作工具,敬请期待。


版权声明:本文为bufegar0原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。