【Elasticsearch学习笔记五】es常用的JAVA API、es整合SpringBoot项目中使用、利用JAVA代码操作es、RestHighLevelClient客户端对象

目录

一、Maven项目集成Easticsearch

1)客户端对象

2)索引操作

3)文档操作

4)高级查询

二、springboot项目集成Spring Data操作Elasticsearch

1)pom文件

2)yaml

3)数据实体类

4)配置类

5)Dao数据访问对象

6)索引操作

7)文档操作

8)文档搜索

三、springboot项目集成bboss操作elasticsearch

1)pom文件和配置

2)getConfigRestClientUtil和getRestClientUtil区别

3)创建索引

4)删除索引

5)判断索引是否存在

6)判断索引类型是否存在

7)添加单个文档

8)批量添加文档

9)查询单个文档


一、Maven项目集成Easticsearch

        Elasticsearch软件是由 Java 语言开发的,所以也可以通过 Java API 的方式对 Elasticsearch服务进行访问。  修改pom 文件,增加 Maven 依赖关系。

<properties>
    <java.version>1.8</java.version>
    <elasticsearch.version>7.4.2</elasticsearch.version>
</properties>

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.4.2</version>
</dependency>

1)客户端对象

        创建类,代码中创建 Elasticsearch 客户端对象因为早期版本的客户端对象已经不再推荐使用,且在未来版本中会被删除,所以这里我们采用高级 REST 客户端对象;

    public static void main(String[] args) throws IOException {
        // 创建客户端对象
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("127.0.0.1", 9200, "http"))
        );
 
        // 关闭客户端连接
        client.close();
    }

2)索引操作

        ES服务器正常启动后,可以通过 Java API 客户端对象对 ES 索引进行操作 ;

public class EsIndex {
 
    public static void main(String[] args) throws IOException {
 
        // 创建客户端对象
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("127.0.0.1", 9200, "http"))
        );
 
        // 关闭客户端连接
        client.close();
    }
 
    // 创建索引
    public static void createIndex(RestHighLevelClient client) throws IOException {
        // 创建索引 - 请求对象
        CreateIndexRequest request = new CreateIndexRequest("user");
        // 发送请求,获取响应
        CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
        boolean acknowledged = response.isAcknowledged();
        // 响应状态
        System.out.println("操作状态 = " + acknowledged);
    }
 
    // 查看索引
    public static void getIndex(RestHighLevelClient client) throws IOException {
        // 查询索引 - 请求对象
        GetIndexRequest request = new GetIndexRequest("user");
        // 发送请求,获取响应
        GetIndexResponse response = client.indices().get(request, RequestOptions.DEFAULT);
        System.out.println("aliases: " + response.getAliases());
        System.out.println("mappings: " + response.getMappings());
        System.out.println("settings: " + response.getSettings());
    }
 
    // 删除索引
    public static void deleteIndex(RestHighLevelClient client) throws IOException {
        // 删除索引 - 请求对象
        DeleteIndexRequest request = new DeleteIndexRequest("user");
        // 发送请求,获取响应
        AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);
        // 操作结果
        System.out.println("操作结果: " + response.isAcknowledged());
    }
}

3)文档操作

public class EsDoc {
 
    public static void main(String[] args) throws IOException {
        // 创建客户端对象
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("127.0.0.1", 9200, "http"))
        );
 
        // 关闭客户端连接
        client.close();
    }
 
    // 创建文档
    public static void createDoc(RestHighLevelClient client) throws IOException {
        // 新增文档 - 请求对象
        IndexRequest request = new IndexRequest();
        // 设置索引及唯一性标识
        request.index("user").id("1001");
        // 创建数据对象
        User user = new User();
        user.setAge(26);
        user.setSex("男");
        user.setName("jak");
        ObjectMapper objectMapper = new ObjectMapper();
        String productJson = objectMapper.writeValueAsString(user);
        // 添加文档数据, 数据格式为Json格式
        request.source(productJson, XContentType.JSON);
        // 客户端发送请求,获取响应对象
        IndexResponse response = client.index(request, RequestOptions.DEFAULT);
        // 打印结果信息
        System.out.println("_index: " + response.getIndex());
        System.out.println("id: " + response.getId());
        System.out.println("_result: " + response.getResult());
    }
 
    // 修改文档
    public static void updateDoc(RestHighLevelClient client) throws IOException {
        // 修改文档 - 请求对象
        UpdateRequest request = new UpdateRequest();
        // 配置修改参数
        request.index("user").id("1001");
        // 设置请求体,对数据进行修改
        request.doc(XContentType.JSON, "sex", "女");
        // 客户端发送请求,获取响应对象
        UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
        System.out.println("_index: " + response.getIndex());
        System.out.println("_id: " + response.getId());
        System.out.println("_result: " + response.getResult());
    }
 
    // 查询文档
    public static void getDoc(RestHighLevelClient client) throws IOException {
        // 创建请求对象
        GetRequest request = new GetRequest().index("user").id("1001");
        // 客户端发送请求,获取响应对象
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        // 打印结果信息
        System.out.println("_index: " + response.getIndex());
        System.out.println("_type: " + response.getType());
        System.out.println("_id: " + response.getId());
        System.out.println("source: " + response.getSourceAsString());
    }
 
    // 删除文档
    public static void deleteDoc(RestHighLevelClient client) throws IOException {
        // 创建请求对象
        DeleteRequest request = new DeleteRequest().index("user").id("1");
        // 客户端发送请求,获取响应对象
        DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
        // 打印信息
        System.out.println(response.toString());
    }
 
    // 批量新增
    public static void bulkCreateDoc(RestHighLevelClient client) throws IOException {
        // 创建批量新增请求对象
        BulkRequest request = new BulkRequest();
        request.add(new IndexRequest().index("user").id("1001").source(XContentType.JSON, "name", "zhangsan"));
        request.add(new IndexRequest().index("user").id("1002").source(XContentType.JSON, "name", "lisi"));
        request.add(new IndexRequest().index("user").id("1003").source(XContentType.JSON, "name", "wangwu"));
        // 客户端发送请求,获取响应对象
        BulkResponse responses = client.bulk(request, RequestOptions.DEFAULT);
        // 打印结果信息
        System.out.println("took: " + responses.getTook());
        System.out.println("items: " + Arrays.toString(responses.getItems()));
    }
 
    // 批量删除
    public static void bulkDeleteDoc(RestHighLevelClient client) throws IOException {
        // 创建批量删除请求对象
        BulkRequest request = new BulkRequest();
        request.add(new DeleteRequest().index("user").id("1001"));
        request.add(new DeleteRequest().index("user").id("1002"));
        request.add(new DeleteRequest().index("user").id("1003"));
        // 客户端发送请求,获取响应对象
        BulkResponse responses = client.bulk(request, RequestOptions.DEFAULT);
        // 打印结果信息
        System.out.println("took: " + responses.getTook());
        System.out.println("items: " + Arrays.toString(responses.getItems()));
 
    }
}

4)高级查询

public class EsSearch {
 
    public static void main(String[] args) throws IOException {
 
        // 创建客户端对象
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("127.0.0.1", 9200, "http"))
        );
 
        // 关闭客户端连接
        client.close();
    }
 
    // 查询所有索引数据
    public static void matchAllQuery(RestHighLevelClient client) throws IOException {
        // 创建搜索请求对象
        SearchRequest request = new SearchRequest();
        request.indices("student");
 
        // 构建查询的请求体
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        // 查询所有数据
        sourceBuilder.query(QueryBuilders.matchAllQuery());
        request.source(sourceBuilder);
 
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
 
        // 查询匹配
        SearchHits hits = response.getHits();
        System.out.println("took: " + response.getTook());
        System.out.println("timeout: " + response.isTimedOut());
        System.out.println("total: " + hits.getTotalHits());
        System.out.println("MaxScore: " + hits.getMaxScore());
        System.out.println("hits------------->");
        for (SearchHit hit : hits) {
            // 输出每条查询的结果信息
            System.out.println(hit.getSourceAsString());
        }
        System.out.println("<-----------------");
 
    }
 
    // term查询,查询条件为关键字
    public static void termQuery(RestHighLevelClient client) throws IOException {
        // 创建搜索请求对象
        SearchRequest request = new SearchRequest();
        request.indices("student");
 
        // 构建查询的请求体
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        // 查询所有数据
        sourceBuilder.query(QueryBuilders.termQuery("age", "30"));
        request.source(sourceBuilder);
 
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
 
        // 查询匹配
        SearchHits hits = response.getHits();
        System.out.println("took: " + response.getTook());
        System.out.println("timeout: " + response.isTimedOut());
        System.out.println("total: " + hits.getTotalHits());
        System.out.println("MaxScore: " + hits.getMaxScore());
        System.out.println("hits------------->");
        for (SearchHit hit : hits) {
            // 输出每条查询的结果信息
            System.out.println(hit.getSourceAsString());
        }
        System.out.println("<-----------------");
    }
 
    // 分页查询
    public static void pageQuery(RestHighLevelClient client) throws IOException {
        // 创建搜索请求对象
        SearchRequest request = new SearchRequest();
        request.indices("student");
 
        // 构建查询的请求体
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        // 查询所有数据
        sourceBuilder.query(QueryBuilders.matchAllQuery());
 
        // 分页查询
        // 当前页起始索引(第一条数据的顺序号),from
        sourceBuilder.from(0);
        // 每页显示多少条size
        sourceBuilder.size(2);
 
        request.source(sourceBuilder);
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
 
        // 查询匹配
        SearchHits hits = response.getHits();
        System.out.println("took: " + response.getTook());
        System.out.println("timeout: " + response.isTimedOut());
        System.out.println("total: " + hits.getTotalHits());
        System.out.println("MaxScore: " + hits.getMaxScore());
        System.out.println("hits------------->");
        for (SearchHit hit : hits) {
            // 输出每条查询的结果信息
            System.out.println(hit.getSourceAsString());
        }
        System.out.println("<-----------------");
    }
 
    // 数据排序
    public static void sortOrderQuery(RestHighLevelClient client) throws IOException {
        // 创建搜索请求对象
        SearchRequest request = new SearchRequest();
        request.indices("student");
 
        // 构建查询的请求体
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        // 查询所有数据
        sourceBuilder.query(QueryBuilders.matchAllQuery());
 
        // 排序
        sourceBuilder.sort("age", SortOrder.ASC);
 
        request.source(sourceBuilder);
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
 
        // 查询匹配
        SearchHits hits = response.getHits();
        System.out.println("took: " + response.getTook());
        System.out.println("timeout: " + response.isTimedOut());
        System.out.println("total: " + hits.getTotalHits());
        System.out.println("MaxScore: " + hits.getMaxScore());
        System.out.println("hits------------->");
        for (SearchHit hit : hits) {
            // 输出每条查询的结果信息
            System.out.println(hit.getSourceAsString());
        }
        System.out.println("<-----------------");
    }
 
    // 过滤字段
    public static void filterFieldsQuery(RestHighLevelClient client) throws IOException {
        // 创建搜索请求对象
        SearchRequest request = new SearchRequest();
        request.indices("student");
 
        // 构建查询的请求体
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        // 查询所有数据
        sourceBuilder.query(QueryBuilders.matchAllQuery());
 
        // 查询字段过滤
        String[] excludes = {};
        String[] includes = {"name", "age"};
        sourceBuilder.fetchSource(includes, excludes);
 
        request.source(sourceBuilder);
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
 
        // 查询匹配
        SearchHits hits = response.getHits();
        System.out.println("took: " + response.getTook());
        System.out.println("timeout: " + response.isTimedOut());
        System.out.println("total: " + hits.getTotalHits());
        System.out.println("MaxScore: " + hits.getMaxScore());
        System.out.println("hits------------->");
        for (SearchHit hit : hits) {
            // 输出每条查询的结果信息
            System.out.println(hit.getSourceAsString());
        }
        System.out.println("<-----------------");
    }
 
    // Bool查询
    public static void boolQuery(RestHighLevelClient client) throws IOException {
        // 创建搜索请求对象
        SearchRequest request = new SearchRequest();
        request.indices("student");
 
        // 构建查询的请求体
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
 
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        // 必须包含
        boolQueryBuilder.must(QueryBuilders.matchQuery("age", "30"));
        // 一定不含
        boolQueryBuilder.mustNot(QueryBuilders.matchQuery("name", "zhangsan"));
        // 可能包含
        boolQueryBuilder.should(QueryBuilders.matchQuery("sex", "男"));
 
        // 查询所有数据
        sourceBuilder.query(boolQueryBuilder);
        request.source(sourceBuilder);
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
 
        // 查询匹配
        SearchHits hits = response.getHits();
        System.out.println("took: " + response.getTook());
        System.out.println("timeout: " + response.isTimedOut());
        System.out.println("total: " + hits.getTotalHits());
        System.out.println("MaxScore: " + hits.getMaxScore());
        System.out.println("hits------------->");
        for (SearchHit hit : hits) {
            // 输出每条查询的结果信息
            System.out.println(hit.getSourceAsString());
        }
        System.out.println("<-----------------");
    }
 
    // 范围查询
    public static void rangeQuery(RestHighLevelClient client) throws IOException {
        // 创建搜索请求对象
        SearchRequest request = new SearchRequest();
        request.indices("student");
 
        // 构建查询的请求体
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
 
        RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("age");
        // 大于等于
        rangeQuery.gte("30");
        // 小于等于
        rangeQuery.lte("40");
 
 
        // 查询所有数据
        sourceBuilder.query(rangeQuery);
        request.source(sourceBuilder);
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
 
        // 查询匹配
        SearchHits hits = response.getHits();
        System.out.println("took: " + response.getTook());
        System.out.println("timeout: " + response.isTimedOut());
        System.out.println("total: " + hits.getTotalHits());
        System.out.println("MaxScore: " + hits.getMaxScore());
        System.out.println("hits------------->");
        for (SearchHit hit : hits) {
            // 输出每条查询的结果信息
            System.out.println(hit.getSourceAsString());
        }
        System.out.println("<-----------------");
    }
 
    // 范围查询
    public static void fuzzyQuery(RestHighLevelClient client) throws IOException {
        // 创建搜索请求对象
        SearchRequest request = new SearchRequest();
        request.indices("student");
 
        // 构建查询的请求体
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
 
        // 查询所有数据
        sourceBuilder.query(QueryBuilders.fuzzyQuery("name", "zhangsan").fuzziness(Fuzziness.ONE));
        request.source(sourceBuilder);
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
 
        // 查询匹配
        SearchHits hits = response.getHits();
        System.out.println("took: " + response.getTook());
        System.out.println("timeout: " + response.isTimedOut());
        System.out.println("total: " + hits.getTotalHits());
        System.out.println("MaxScore: " + hits.getMaxScore());
        System.out.println("hits------------->");
        for (SearchHit hit : hits) {
            // 输出每条查询的结果信息
            System.out.println(hit.getSourceAsString());
        }
        System.out.println("<-----------------");
    }
 
    // 高亮查询
    public static void highLightQuery(RestHighLevelClient client) throws IOException {
        // 创建搜索请求对象
        SearchRequest request = new SearchRequest();
        request.indices("student");
 
        // 构建查询的请求体
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
 
        // 构建查询方式: 高亮查询
        TermsQueryBuilder termsQueryBuilder = QueryBuilders.termsQuery("name", "zhangsan");
        // 设置查询方式
        sourceBuilder.query(termsQueryBuilder);
 
        // 构建高亮字段
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        // 设置标签前缀
        highlightBuilder.preTags("<font color='red'>");
        // 设置标签后缀
        highlightBuilder.postTags("</font>");
        // 设置高亮字段
        highlightBuilder.field("name");
        // 设置高亮构建对象
        sourceBuilder.highlighter(highlightBuilder);
        // 设置请求体
        request.source(sourceBuilder);
 
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
 
        // 查询匹配
        SearchHits hits = response.getHits();
        System.out.println("took: " + response.getTook());
        System.out.println("timeout: " + response.isTimedOut());
        System.out.println("total: " + hits.getTotalHits());
        System.out.println("MaxScore: " + hits.getMaxScore());
        System.out.println("hits------------->");
        for (SearchHit hit : hits) {
            // 输出每条查询的结果信息
            System.out.println(hit.getSourceAsString());
        }
        System.out.println("<-----------------");
    }
 
    // 聚合查询
    public static void AggrQuery(RestHighLevelClient client) throws IOException {
        // 创建搜索请求对象
        SearchRequest request = new SearchRequest();
        request.indices("student");
 
        // 构建查询的请求体
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.aggregation(AggregationBuilders.max("maxAge").field("age"));
        // 设置请求体
        request.source(sourceBuilder);
        // 客户端发送请求,获取响应对象
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
 
        // 查询匹配
        SearchHits hits = response.getHits();
        System.out.println("took: " + response.getTook());
        System.out.println("timeout: " + response.isTimedOut());
        System.out.println("total: " + hits.getTotalHits());
        System.out.println("MaxScore: " + hits.getMaxScore());
        System.out.println("hits------------->");
        for (SearchHit hit : hits) {
            // 输出每条查询的结果信息
            System.out.println(hit.getSourceAsString());
        }
        System.out.println("<-----------------");
    }
 
    // 分组统计
    public static void groupQuery(RestHighLevelClient client) throws IOException {
        // 创建搜索请求对象
        SearchRequest request = new SearchRequest();
        request.indices("student");
 
        // 构建查询的请求体
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.aggregation(AggregationBuilders.terms("age_group_by").field("age"));
        // 设置请求体
        request.source(sourceBuilder);
        // 客户端发送请求,获取响应对象
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
 
        // 查询匹配
        SearchHits hits = response.getHits();
        System.out.println("took: " + response.getTook());
        System.out.println("timeout: " + response.isTimedOut());
        System.out.println("total: " + hits.getTotalHits());
        System.out.println("MaxScore: " + hits.getMaxScore());
        System.out.println("hits------------->");
        for (SearchHit hit : hits) {
            // 输出每条查询的结果信息
            System.out.println(hit.getSourceAsString());
        }
        System.out.println("<-----------------");
    }
}

二、springboot项目集成Spring Data操作Elasticsearch

        Spring Data是一个用于简化数据库、非关系型数据库、索引库访问,并支持云服务的开源框架。  其主要目标是使得对数据的访问变得方便快捷,并支持 map reduce 框架和云计算数据服务。 Spring Data 可以极大的简化 JPA Elasticsearch …)的写法,可以在几乎不用写实现的情况下,实现对数据的访问和操作。  除了 CRUD 外,还包括如分页、排序等一些常用的功能。官网地址:https://spring.io/projects/spring-data

        Spring Data Elasticsearch基于 spring data API 简化 Elasticsearch 操作,将原始操作Elasticsearch 的客户端 API 进行封装 。 Spring Data 为 Elasticsearch 项目提供集成搜索引擎。  Spring Data Elasticsearch POJO 的关键功能区域为中心的模型与 Elastichsearch 交互文档和轻松地编写一个存储索引库数据访问层。

1)pom文件

<?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.3.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.best</groupId>
    <artifactId>best-es</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>best-es</name>
    <description>Demo project for Spring Boot</description>
 
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
 
    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.12</version>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</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-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
 
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>7.8.0</version>
        </dependency>
        <!-- elasticsearch 的客户端 -->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.8.0</version>
        </dependency>
        <!-- elasticsearch 依赖 2.x 的 log4j -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.8.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.8.2</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.9</version>
        </dependency>
        <!-- junit 单元测试 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
        </dependency>
 
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
        </dependency>
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
 
</project>

2)yaml

# 端口号
server:
  port: 8080
 
# es服务地址与端口
elasticsearch:
  host: 127.0.0.1
  port: 9200
 
# 配置日志级别,开启debug日志
logging:
  level:
    com:
      best: debug

3)数据实体类

@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Document(indexName = "shopping", shards = 3, replicas = 1)
public class Product {
 
    // 商品唯一标识, 必须有id, 这里的id 是全局唯一的标识,等同于es中的"_id"
    @Id
    private Long id;
 
    /**
     * type: 字段数据类型
     * analyzer: 分词器类型
     * index: 是否索引(默认:true)
     * Keyword: 短语, 不进行分词
     */
    // 商品名称
    @Field(type = FieldType.Text, analyzer = "ik_max_word")
    private String title;
 
    // 分类名称
    @Field(type = FieldType.Keyword)
    private String category;
 
    // 商品价格
    @Field(type = FieldType.Double)
    private Double price;
 
    // 图片地址
    @Field(type = FieldType.Keyword, index = false)
    private String images;
}

4)配置类

        ElasticsearchRestTemplate是spring-data-elasticsearch项目中的一个类,和其他spring项目中的 template类似。在新版的spring-data-elasticsearch 中,ElasticsearchRestTemplate 代替了原来的ElasticsearchTemplate。原因是ElasticsearchTemplate基于TransportClient,TransportClient即将在8.x 以后的版本中移除。

        我们推荐使用ElasticsearchRestTemplate。ElasticsearchRestTemplate基于RestHighLevelClient客户端的。需要自定义配置类,继承AbstractElasticsearchConfiguration,并实现elasticsearchClient()抽象方法,创建RestHighLevelClient对象。

AbstractElasticsearchConfiguration源码:

public abstract class AbstractElasticsearchConfiguration extends ElasticsearchConfigurationSupport {
 
	//需重写本方法
	public abstract RestHighLevelClient elasticsearchClient();
 
	@Bean(name = { "elasticsearchOperations", "elasticsearchTemplate" })
	public ElasticsearchOperations elasticsearchOperations(ElasticsearchConverter elasticsearchConverter) {
		return new ElasticsearchRestTemplate(elasticsearchClient(), elasticsearchConverter);
	}
}

        需要自定义配置类,继承AbstractElasticsearchConfiguration,并实现elasticsearchClient()抽象方法,创建RestHighLevelClient对象。 

@Data
@Configuration
@ConfigurationProperties(prefix = "elasticsearch")
public class ElasticsearchConfig extends AbstractElasticsearchConfiguration {
 
    private String host;
 
    private Integer port;
 
    @Override
    public RestHighLevelClient elasticsearchClient() {
        RestClientBuilder builder = RestClient.builder(new HttpHost(host, port));
        return new RestHighLevelClient(builder);
    }
}

5)Dao数据访问对象

@Repository
public interface ProductDao extends ElasticsearchRepository<Product, Long> {
}

6)索引操作

@SpringBootTest
class BestEsApplicationTests {
 
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;
 
    @Test
    public void creatIndex() {
        // 创建索引并增加映射配置
        // 创建索引,系统初始化会自动创建索引
        System.out.println("创建索引");
    }
 
    @Test
    public void deleteIndex() {
        boolean flag = elasticsearchRestTemplate.deleteIndex(Product.class);
        System.out.println("删除索引: " + flag);
    }
 
}

7)文档操作

@SpringBootTest
public class SpringDataEsProductDaoTest {
 
    @Autowired
    private ProductDao productDao;
    
    /**
     * 新增
     */
    @Test
    public void save(){
        Product product = new Product();
        product.setId(2L);
        product.setTitle("华为手机");
        product.setCategory("手机");
        product.setPrice(2999.0);
        product.setImages("http://www.test/hw.jpg");
        productDao.save(product);
    }
    //POSTMAN, GET http://localhost:9200/shopping/_doc/2
 
    //修改
    @Test
    public void update(){
        Product product = new Product();
        product.setId(2L);
        product.setTitle("小米 2 手机");
        product.setCategory("手机");
        product.setPrice(9999.0);
        product.setImages("http://www.test/xm.jpg");
        productDao.save(product);
    }
    //POSTMAN, GET http://localhost:9200/shopping/_doc/2
 
 
    //根据 id 查询
    @Test
    public void findById(){
        Product product = productDao.findById(2L).get();
        System.out.println(product);
    }
 
    @Test
    public void findAll(){
        Iterable<Product> products = productDao.findAll();
        for (Product product : products) {
            System.out.println(product);
        }
    }
 
    //删除
    @Test
    public void delete(){
        Product product = new Product();
        product.setId(2L);
        productDao.delete(product);
    }
    //POSTMAN, GET http://localhost:9200/shopping/_doc/2
 
    //批量新增
    @Test
    public void saveAll(){
        List<Product> productList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Product product = new Product();
            product.setId(Long.valueOf(i));
            product.setTitle("["+i+"]小米手机");
            product.setCategory("手机");
            product.setPrice(1999.0 + i);
            product.setImages("http://www.test/xm.jpg");
            productList.add(product);
        }
        productDao.saveAll(productList);
    }
 
    //分页查询
    @Test
    public void findByPageable(){
        //设置排序(排序方式,正序还是倒序,排序的 id)
        Sort sort = Sort.by(Sort.Direction.DESC,"id");
        int currentPage=0;//当前页,第一页从 0 开始, 1 表示第二页
        int pageSize = 5;//每页显示多少条
        //设置查询分页
        PageRequest pageRequest = PageRequest.of(currentPage, pageSize,sort);
        //分页查询
        Page<Product> productPage = productDao.findAll(pageRequest);
        for (Product Product : productPage.getContent()) {
            System.out.println(Product);
        }
    }
}

8)文档搜索

@SpringBootTest
public class SpringDataEsSearchTest {
 
    @Autowired
    private ProductDao productDao;
 
    /**
     * term 查询
     * search(termQueryBuilder) 调用搜索方法,参数查询构建器对象
     */
    @Test
    public void termQuery(){
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("title", "小米");
        Iterable<Product> products = productDao.search(termQueryBuilder);
        for (Product product : products) {
            System.out.println(product);
        }
    }
    /**
     * term 查询加分页
     */
    @Test
    public void termQueryByPage(){
        int currentPage= 0 ;
        int pageSize = 5;
        //设置查询分页
        PageRequest pageRequest = PageRequest.of(currentPage, pageSize);
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("category", "手机");
        Iterable<Product> products =
                productDao.search(termQueryBuilder,pageRequest);
        for (Product product : products) {
            System.out.println(product);
        }
    }
 
}

参考原文地址: 

三、springboot项目集成bboss操作elasticsearch

1)pom文件和配置

pom文件引入依赖:

        <dependency>
            <groupId>com.bbossgroups.plugins</groupId>
            <artifactId>bboss-elasticsearch-rest-jdbc</artifactId>
            <version>6.1.8</version>
        </dependency>
        <dependency>
            <groupId>com.bbossgroups.plugins</groupId>
            <artifactId>bboss-elasticsearch-spring-boot-starter</artifactId>
            <version>6.1.8</version>
        </dependency>

application.properties配置:

##ES集群配置,支持x-pack和searchguard
#spring.elasticsearch.bboss.elasticUser=elastic
#spring.elasticsearch.bboss.elasticPassword=changeme
 
 
spring.elasticsearch.bboss.elasticsearch.rest.hostNames=192.168.57.128:9200
#spring.elasticsearch.bboss.elasticsearch.rest.hostNames=10.180.211.27:9280,10.180.211.27:9281,10.180.211.27:9282
##https配置,添加https://协议头
#spring.elasticsearch.bboss.default.elasticsearch.rest.hostNames=https://10.180.211.27:9280,https://10.180.211.27:9281,https://10.180.211.27:9282
spring.elasticsearch.bboss.elasticsearch.dateFormat=yyyy.MM.dd
spring.elasticsearch.bboss.elasticsearch.timeZone=Asia/Shanghai
spring.elasticsearch.bboss.elasticsearch.ttl=2d
#在控制台输出脚本调试开关showTemplate,false关闭,true打开,同时log4j至少是info级别
spring.elasticsearch.bboss.elasticsearch.showTemplate=true
spring.elasticsearch.bboss.elasticsearch.discoverHost=false
# dsl配置文件热加载扫描时间间隔,毫秒为单位,默认5秒扫描一次,<= 0时关闭扫描机制
spring.elasticsearch.bboss.dslfile.refreshInterval = -1
 
##es client http连接池配置
spring.elasticsearch.bboss.http.timeoutConnection = 50000
spring.elasticsearch.bboss.http.timeoutSocket = 50000
spring.elasticsearch.bboss.http.connectionRequestTimeout=50000
spring.elasticsearch.bboss.http.retryTime = 1
spring.elasticsearch.bboss.http.maxLineLength = -1
spring.elasticsearch.bboss.http.maxHeaderCount = 200
spring.elasticsearch.bboss.http.maxTotal = 400
spring.elasticsearch.bboss.http.defaultMaxPerRoute = 200
spring.elasticsearch.bboss.http.soReuseAddress = false
spring.elasticsearch.bboss.http.soKeepAlive = false
spring.elasticsearch.bboss.http.timeToLive = 3600000
spring.elasticsearch.bboss.http.keepAlive = 3600000
spring.elasticsearch.bboss.http.keystore =
spring.elasticsearch.bboss.http.keyPassword =
# ssl 主机名称校验,是否采用default配置,
# 如果指定为default,就采用DefaultHostnameVerifier,否则采用 SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER
spring.elasticsearch.bboss.http.hostnameVerifier =

2)getConfigRestClientUtil和getRestClientUtil区别

1. getConfigRestClientUtil获取在配置文件中根据DSL名称定义的DSL并执行它:

<properties>  
    <!--  
        sql query  
    -->  
    <property name="sqlQuery">  
        <!\[CDATA\[  
         {"query": "SELECT * FROM dbclobdemo where channelId=#\[channelId\]"}  
        \]\]>  
    </property>  
</properties> 

写入的params参数在xml文件中通过 #[参数名] 来自动注入到 DSL中

ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("esmapper/sql.xml");//define an instanceof ConfigRestClientUtil,It's single instance, multithreaded secure.  
Map params = new HashMap();  
params.put("channelId",1);  
List<Map> json = clientUtil.sql(Map.class,"sqlQuery",params); 

2. getRestClientUtil直接执行代码中定义的DSL:

ClientInterface clientUtil = ElasticSearchHelper.getRestClientUtil();//define an instanceof RestClientUtil,It's single instance, multithreaded secure.  
List<Map> json = clientUtil.sql(Map.class,"{\\"query\\": \\"SELECT * FROM demo\\"}");  

3)创建索引

1.普通创建

@SpringBootTest
class EsBbossTest {
    @Autowired
    private BBossESStarter bbossESStarter;
 
    /**
     *创建索引
     */
    @Test
    public void createIndex(){
        String result = bbossESStarter.getRestClient().createIndiceMapping("blog","");
        System.out.println(result);
    }
}

2.自定义mapping

先创建一个dsl脚本文件,在里面自定义mapping规则:

<propertys>
    <property name="create51jobIndex">//每个property中的name属性表示当前mapping的命名,一个xml文件里可设置多个dsl脚本
        <![CDATA[{
            "settings": {
                "number_of_shards": 1,
                "number_of_replicas": 1,
                "index.refresh_interval": "5s"
            },
            "mappings": {
                    "properties": {
                        "job":{
                            "type":"text"
                        },
                        "company": {
                            "type": "text"
                        },
                        "place": {
                            "type": "text"
                        },
                        "salar": {
                            "type": "text"
                        },
                        "data": {
                            "type": "date",
                            "format":"yyyy-MM-dd HH:mm:ss.SSS||yyyy-MM-dd'T'HH:mm:ss.SSS||yyyy-MM-dd HH:mm:ss||epoch_millis"
                        }
                    }
            }
        }]]>
    </property>
</propertys>

然后创建客户端,并且调用创建索引api:

@SpringBootTest
class EsBbossTest {
    @Autowired
    private BBossESStarter bbossESStarter;
 
    /**
     *创建索引
     */
    @Test
    public void createIndex(){
        ClientInterface clientUtil = bbossESStarter.getConfigRestClient("esmapper/job.xml");
        String result = clientUtil.createIndiceMapping("51job","create51jobIndex");
        System.out.println(result);
    }
}

4)删除索引

@SpringBootTest
class EsBbossTest {
    @Autowired
    private BBossESStarter bbossESStarter;
 
    /**
     *删除索引
     */
    @Test
    public void dropIndex(){
        String result = bbossESStarter.getRestClient().dropIndice("blog");
        System.out.println(result);
    }
}

5)判断索引是否存在

@SpringBootTest
class EsBbossTest {
    @Autowired
    private BBossESStarter bbossESStarter;
 
    /**
     *判断索引是否存在
     */
    @Test
    public void indexIsExists(){
        boolean exist = bbossESStarter.getRestClient().existIndice("51job");
        System.out.println(exist);
    }
}

6)判断索引类型是否存在

@SpringBootTest
class EsBbossTest {
    @Autowired
    private BBossESStarter bbossESStarter;
 
    /**
     *判断索引类型是否存在
     */
    @Test
    public void typeIsExists(){
        boolean exist = bbossESStarter.getRestClient().existIndiceType("51job","_doc");
        System.out.println(exist);
    }
}

7)添加单个文档

@SpringBootTest
class EsBbossTest {
    @Autowired
    private BBossESStarter bbossESStarter;
 
    /**
     *指定索引添加单个文档
     */
    @Test
    public void addDocument(){
        Qcpage qcpage=new Qcpage();
        qcpage.setCompany("xxxxxx有限公司").setData("2022-11-22 21:18:12").setJob("Java开发工程师").setPlace("深圳南山").setSalar("13000/月");
        String result = bbossESStarter.getRestClient().addDocument("51job",qcpage);
        System.out.println(result);
    }
}

8)批量添加文档

@SpringBootTest
class EsBbossTest {
    @Autowired
    private BBossESStarter bbossESStarter;
    @Autowired
    QcpageService qcpageService;
 
    /**
     *批量添加文档(此处从mysql取出来1300条数据测试)
     */
    @Test
    public void batchAddDocument(){
        List<Qcpage> list=qcpageService.list();
        long startTime = System.currentTimeMillis();
        String result = bbossESStarter.getRestClient().addDocuments("51job",list);
        long endTime = System.currentTimeMillis();
        System.out.println("添加"+list.size()+"条数据耗时:" + (endTime - startTime)/1000 + "s");
        System.out.println(result);
    }
}

9)查询单个文档

@SpringBootTest
class EsBbossTest {
    @Autowired
    private BBossESStarter bbossESStarter;
 
    /**
     *查询一个文档
     */
    @Test
    public void getDocument(){
        String result = bbossESStarter.getRestClient().getDocument("51job","7pJsGXMBBreT5daW4X1w");
        System.out.println(result);
    }
}

参考原文地址:http://t.csdn.cn/hm0y7

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

等到鸡吃完米

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值