elasticsearch学习四:使用springboot整合 rest 进行搭建elasticsearch服务

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: 这篇文章介绍了如何使用Spring Boot整合REST方式来搭建和操作Elasticsearch服务。

前言

  • 前面学习了一下 使用springboot整合 transport 进行搭建elasticsearch服务,但是transport 以后要被废弃了,所以便来学习一下,rest 整合 springboot 来搭建操作 elasticsearch,这两个很相似,而且 rest 也是 springboot整合。其依赖为 spring-boot-starter-data-elasticsearch ,里面包含了spring-data-elasticsearch ,这里面又包含了 elasticsearch-rest-high-level-client

  • 使用了 elasticsearch 6.4.2版本,kibana 6.4.2 版本 elasticsearch-head 三个软件

  • 博客目录

  1. elasticsearch学习一:了解 ES,版本之间的对应。安装elasticsearch,kibana,head插件、elasticsearch-ik分词器。
  2. elasticsearch学习二:使用springboot整合TransportClient 进行搭建elasticsearch服务
  3. elasticsearch学习三:elasticsearch-ik分词器的自定义配置 分词内容
  4. elasticsearch学习四:使用springboot整合 rest 进行搭建elasticsearch服务

一、添加pom依赖

1、 可以在创建项目时添加
在这里插入图片描述

2、 可以手动自己添加

        <!--导入的 elasticsearch -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>

3、 还需要阿里的json依赖包,可以快速转换json

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.62</version>
        </dependency>

4、 修改版本信息

    <properties>
        <java.version>1.8</java.version>
        <!-- 自己定义 es 版本依赖,保证和本地一致-->
        <elasticsearch.version>6.4.2</elasticsearch.version>
    </properties>

在这里插入图片描述

5、 pom.xml 的所有依赖

<?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 https://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.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.feng</groupId>
    <artifactId>springboot-elasticsearch-rest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-elasticsearch-rest</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <!-- 自己定义 es 版本依赖,保证和本地一致-->
        <elasticsearch.version>6.4.2</elasticsearch.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.62</version>
        </dependency>

        <!--导入的 elasticsearch -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

二、ElasticSearchConfig配置类

package com.feng.config;

import lombok.Data;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Data
@Configuration
@ConfigurationProperties(prefix = "elasticsearch")
public class ElasticSearchConfig {

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

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

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

    @Bean
    public RestHighLevelClient restHighLevelClient(){
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost(hostname, Integer.valueOf(port), scheme)));
        return client;
    }
}

三、pojo类User(测试)

package com.feng.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Component
public class User {
    private String name;
    private int age;
}

四、application.properties

elasticsearch.hostname = 127.0.0.1
elasticsearch.port = 9200
elasticsearch.scheme = http

五、目录结构

在这里插入图片描述

六、测试代码

  • 都已测试,并测试通过
  • 主要测试了,索引、文档的 CRUD,索引 index 是否存在,搜索(重要),高亮
package com.feng;

import com.alibaba.fastjson.JSON;
import com.feng.bean.User;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermsQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

@SpringBootTest
class SpringbootElasticsearchRestApplicationTests {

    @Autowired
    @Qualifier("restHighLevelClient")
    private RestHighLevelClient client;

    @Test
    void contextLoads() {
        System.out.println("111");
    }

    // 测试 索引的创建 request
    @Test
    void  testCreateIndex() throws IOException {
        // 1、创建索引请求
        CreateIndexRequest request = new CreateIndexRequest("feng_index");
        // 2、 客户端执行请求 IndicesClient ,请求后获取响应。
        CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
        System.out.println(createIndexResponse);
    }

    // 测试 索引是否存在, 这里有问题
    @Test
    void testGetIndex() throws IOException {
        //1.创建索引请求
        GetIndexRequest getIndexRequest = new GetIndexRequest().indices("feng_index");
        boolean exists = client.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
        System.out.println(exists);
    }

    // 测试 删除索引
    @Test
    void testDeleteIndex() throws IOException {
        //1.创建索引请求
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("feng_index");
        DeleteIndexResponse delete = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
        System.out.println(delete.isAcknowledged());
    }

    // 测试 添加文档
    @Test
    void testCreateDocument() throws IOException {
        // 创建对象
        User user = new User("冯凡利", 3);
        // 创建请求
        IndexRequest indexRequest = new IndexRequest("feng_index");
        // 规则 put /feng_index/_doc/1
        indexRequest.id("1");
        indexRequest.type("_doc");
        indexRequest.timeout(TimeValue.timeValueSeconds(3000));
        indexRequest.timeout("1s");

        // 数据放入请求  json
        indexRequest.source(JSON.toJSONString(user), XContentType.JSON);

        // 客户端发送请求,获取响应的结果
        IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
        System.out.println(indexResponse.toString()); //  IndexResponse[index=feng_index,type=_doc,id=1,version=2,result=updated,seqNo=1,primaryTerm=2,shards={"total":2,"successful":1,"failed":0}]
        System.out.println(indexResponse.status()); // 对应命令返回的状态: OK
    }

    // 获取文档, 判断是否存在 get /index/doc/1
    @Test
    public void testIsExists() throws IOException {
        GetRequest getRequest = new GetRequest("feng_index","_doc", "1");
        // 不获取返回的 _source 的上下文
//        getRequest.fetchSourceContext(new FetchSourceContext(false));
//        getRequest.storedFields("_none_");
        boolean exists = client.exists(getRequest, RequestOptions.DEFAULT);
        System.out.println(exists);
    }

    // 获取文档信息
    @Test
    public void testGetDocument() throws IOException {
        GetRequest getRequest = new GetRequest("feng_index","_doc", "1");
        GetResponse documentFields = client.get(getRequest, RequestOptions.DEFAULT);
        System.out.println(documentFields.getIndex()); // feng_index
        System.out.println(documentFields.getId());    // 1
        System.out.println(documentFields.getSourceAsMap());  // {name=冯凡利, age=3}
        System.out.println(documentFields.getSourceAsString()); //  打印文档的内容 {"age":3,"name":"冯凡利"}
        System.out.println(documentFields);// 返回的全部内容和命令式一样的  {"_index":"feng_index","_type":"_doc","_id":"1","_version":2,"found":true,"_source":{"age":3,"name":"冯凡利"}}
    }

    // 更新 文档信息
    @Test
    public void testUpdateDocument() throws IOException {
        UpdateRequest updateRequest = new UpdateRequest("feng_index", "_doc", "1");
        updateRequest.timeout("3000S");

        User user = new User("冯凡利java", 18);
        updateRequest.doc(JSON.toJSONString(user), XContentType.JSON);

        UpdateResponse update = client.update(updateRequest, RequestOptions.DEFAULT);
        System.out.println(update.getGetResult());
        System.out.println(update.status());
    }

    // 删除 文档信息
    @Test
    public void testDeleteDocument() throws IOException {

        DeleteRequest deleteRequest = new DeleteRequest("feng_index", "_doc", "1");
        deleteRequest.timeout("3000s");

        DeleteResponse delete = client.delete(deleteRequest, RequestOptions.DEFAULT);
        System.out.println(delete.status()); // ok , 去head中查看,没有文档记录了
    }

    // 特殊, 真的项目一般都会批量插入数据!!
    @Test
    public void testBulkDocument() throws IOException {
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.timeout("10s");

        ArrayList<User> userArrayList = new ArrayList<>();
        userArrayList.add(new User("fengfanli1", 12));
        userArrayList.add(new User("fengfanli2", 12));
        userArrayList.add(new User("fengfanli3", 12));
        userArrayList.add(new User("fengfanli4", 12));

//        批处理请求
        for (int i = 0; i< userArrayList.size(); i++){
//            批量更新和批量删除,就在这里修改对应的请求就可以了
            bulkRequest.add(new IndexRequest("feng_index")
                    .type("_doc")
                    .id(""+(i+1))
                    .source(JSON.toJSONString(userArrayList.get(i)), XContentType.JSON));
        }
        BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        System.out.println(bulk.hasFailures());// 是否失败, 返回false,代表成功
    }

    /*
    * 查询
    *   SearchRequest: 搜索请求
    *   SearchSourceBuilder: 条件构造
    *   HighlightBuilder:   构建高亮
    *   TermsQueryBuilder:  精确查询
    *   MatchAllQueryBuilder:匹配全部
    *   XXX  QueryBuilder:
    * */
    @Test
    public void testSearch() throws IOException{
        SearchRequest searchRequest = new SearchRequest("feng_index");
        // 构建搜索条件
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        searchSourceBuilder.highlighter(highlightBuilder);

        // 查询条件,我们可以使用 QueryBuilders 工具类来实现
        // QueryBuilders.termsQuery() 精确
        // QueryBuilders.matchAllQuery() 匹配所有
        TermsQueryBuilder termsQueryBuilder = QueryBuilders.termsQuery("name", "fengfanli1");
        // MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
        searchSourceBuilder.query(termsQueryBuilder);
        // 构建 分页信息
        // searchSourceBuilder.from();
        // searchSourceBuilder.size();
        searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
        searchRequest.source(searchSourceBuilder);

        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        System.out.println(JSON.toJSONString(searchResponse.getHits()));
        // 打印:{"fragment":true,"hits":[{"fields":{},"fragment":false,"highlightFields":{},"id":"1","matchedQueries":[],"score":1.0,"sortValues":[],"sourceAsMap":{"name":"fengfanli1","age":12},"sourceAsString":"{\"age\":12,\"name\":\"fengfanli1\"}","sourceRef":{"childResources":[],"fragment":true},"type":"_doc","version":-1}],"maxScore":1.0,"totalHits":1}
        System.out.println("==================================");
        for (SearchHit documentFields : searchResponse.getHits().getHits()){
            System.out.println(documentFields.getSourceAsMap()); //{name=fengfanli1, age=12}
        }
    }
}
相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
相关文章
|
1天前
|
自然语言处理 搜索推荐 关系型数据库
elasticsearch学习六:学习 全文搜索引擎 elasticsearch的语法,使用kibana进行模拟测试(持续更新学习)
这篇文章是关于Elasticsearch全文搜索引擎的学习指南,涵盖了基本概念、命令风格、索引操作、分词器使用,以及数据的增加、修改、删除和查询等操作。
elasticsearch学习六:学习 全文搜索引擎 elasticsearch的语法,使用kibana进行模拟测试(持续更新学习)
|
1天前
|
Web App开发 JavaScript Java
elasticsearch学习五:springboot整合 rest 操作elasticsearch的 实际案例操作,编写搜索的前后端,爬取京东数据到elasticsearch中。
这篇文章是关于如何使用Spring Boot整合Elasticsearch,并通过REST客户端操作Elasticsearch,实现一个简单的搜索前后端,以及如何爬取京东数据到Elasticsearch的案例教程。
20 0
elasticsearch学习五:springboot整合 rest 操作elasticsearch的 实际案例操作,编写搜索的前后端,爬取京东数据到elasticsearch中。
|
1天前
|
监控 Dubbo Java
dubbo学习三:springboot整合dubbo+zookeeper,并使用dubbo管理界面监控服务是否注册到zookeeper上。
这篇文章详细介绍了如何将Spring Boot与Dubbo和Zookeeper整合,并通过Dubbo管理界面监控服务注册情况。
dubbo学习三:springboot整合dubbo+zookeeper,并使用dubbo管理界面监控服务是否注册到zookeeper上。
|
1天前
|
自然语言处理 Java 网络架构
elasticsearch学习三:elasticsearch-ik分词器的自定义配置 分词内容
这篇文章是关于如何自定义Elasticsearch的ik分词器配置以满足特定的中文分词需求。
16 0
elasticsearch学习三:elasticsearch-ik分词器的自定义配置 分词内容
|
1天前
|
存储 JSON Java
elasticsearch学习一:了解 ES,版本之间的对应。安装elasticsearch,kibana,head插件、elasticsearch-ik分词器。
这篇文章是关于Elasticsearch的学习指南,包括了解Elasticsearch、版本对应、安装运行Elasticsearch和Kibana、安装head插件和elasticsearch-ik分词器的步骤。
13 0
elasticsearch学习一:了解 ES,版本之间的对应。安装elasticsearch,kibana,head插件、elasticsearch-ik分词器。
|
1月前
|
NoSQL 关系型数据库 Redis
mall在linux环境下的部署(基于Docker容器),Docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongo
mall在linux环境下的部署(基于Docker容器),docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongodb、minio详细教程,拉取镜像、运行容器
mall在linux环境下的部署(基于Docker容器),Docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongo
|
2月前
|
数据可视化 Docker 容器
一文教会你如何通过Docker安装elasticsearch和kibana 【详细过程+图解】
这篇文章提供了通过Docker安装Elasticsearch和Kibana的详细过程和图解,包括下载镜像、创建和启动容器、处理可能遇到的启动失败情况(如权限不足和配置文件错误)、测试Elasticsearch和Kibana的连接,以及解决空间不足的问题。文章还特别指出了配置文件中空格的重要性以及环境变量中字母大小写的问题。
一文教会你如何通过Docker安装elasticsearch和kibana 【详细过程+图解】
|
2月前
|
JSON 自然语言处理 数据库
Elasticsearch从入门到项目部署 安装 分词器 索引库操作
这篇文章详细介绍了Elasticsearch的基本概念、倒排索引原理、安装部署、IK分词器的使用,以及如何在Elasticsearch中进行索引库的CRUD操作,旨在帮助读者从入门到项目部署全面掌握Elasticsearch的使用。
|
2月前
|
Ubuntu Oracle Java
如何在 Ubuntu VPS 上安装 Elasticsearch
如何在 Ubuntu VPS 上安装 Elasticsearch
24 0
|
2月前
|
存储 Ubuntu Oracle
在Ubuntu 14.04上安装和配置Elasticsearch的方法
在Ubuntu 14.04上安装和配置Elasticsearch的方法
34 0