Springboot接入ES并实现查询

setp1 POM文件

版本约定:springboot2.1.6.RELEASE + es7.6.2

<?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 http://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.1.6.RELEASE</version>
    </parent>
    <groupId>org.example</groupId>
    <artifactId>esLearn</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <elasticsearch.version>7.6.2</elasticsearch.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-elasticsearch -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-elasticsearch</artifactId>
            <version>3.2.5.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>2.0.2</version>
        </dependency>

    </dependencies>

</project>

step2 yaml配置

es:
  host: {your es host}
  port: {your es port}

step3 注入工具类


@Configuration
public class EsConfig {
    @Value("${es.host}")
    private String esHost;
    @Value("${es.port}")
    private int esPort;

    @Bean
    public RestHighLevelClient restHighLevelClient() {
        return new RestHighLevelClient(RestClient.builder(new HttpHost(esHost, esPort, "http")));
    }
}

step4 使用


@Service
public class EsService {

    @Resource
    private RestHighLevelClient restHighLevelClient;

    public void es() throws IOException {
        // 创建索引(不恰当的类似于mysql的table)
        CreateIndexRequest create = new CreateIndexRequest("x_index");
        CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(create, RequestOptions.DEFAULT);
        System.out.println(createIndexResponse.toString());

        // 获取索引(只能判读是否存在)
        GetIndexRequest getIndexRequest = new GetIndexRequest("x_index");
        boolean exists = restHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
        System.out.println(exists);

        // 删除索引
        DeleteIndexRequest xu_index = new DeleteIndexRequest("x_index");
        AcknowledgedResponse delete = restHighLevelClient.indices().delete(xu_index, RequestOptions.DEFAULT);
        System.out.println(delete.isAcknowledged());

        // 文档添加数据
        User user = new User(1, "xiaohong", 12);
        IndexRequest docAdd = new IndexRequest("x_index");
        docAdd.id("1");
        docAdd.timeout(TimeValue.timeValueSeconds(1));
        docAdd.source(JSON.toJSONString(user), XContentType.JSON);
        IndexResponse response = restHighLevelClient.index(docAdd, RequestOptions.DEFAULT);
        System.out.println(response.toString());
        System.out.println(response.status());
        
        // 文档查询数据
        GetRequest getReq = new GetRequest("x_index", "1");
        boolean exists = restHighLevelClient.exists(getReq, RequestOptions.DEFAULT);
        if(exists) {
        	GetResponse documentFields = restHighLevelClient.get(getReq, RequestOptions.DEFAULT);
		}
        System.out.println(documentFields.getSourceAsString());
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值