Springboot接入ES并实现查询

梦想不抛弃苦心追求的人,只要不停止追求,你们会沐浴在梦想的光辉之中。再美好的梦想与目标,再完美的计划和方案,如果不能尽快在行动中落实,最终只能是纸上谈兵,空想一番。只要瞄准了大方向,坚持不懈地做下去,才能够扫除挡在梦想前面的障碍,实现美好的人生蓝图。Springboot接入ES并实现查询,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

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());
    }

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/153509.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!