ElasticSearch ( 六 ) 与SpringBoot整合

这篇具有很好参考价值的文章主要介绍了ElasticSearch ( 六 ) 与SpringBoot整合。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

6.与springboot整合

https://www.elastic.co/guide/en/elasticsearch/client/java-rest/

6.1.pom.xml 引入依赖

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

如果当前springboot所默认依赖的版本与es版本不相同

        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.15.0</version>
            <exclusions>
                <exclusion>
                    <groupId>org.elasticsearch</groupId>
                    <artifactId>elasticsearch</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

6.2.配置文件

指明服务器的IP文章来源地址https://www.toymoban.com/news/detail-510642.html

spring.elasticsearch.rest.uris=192.168.46.123:9200

6.3.es操作对象

    @Autowired
    private RestHighLevelClient client;

6.4.操作

6.4.1.实体类

package com.yuan.estest.entity;

import lombok.Data;

@Data
public class StoryEntity {

     private String storyTitle;
     private String storyAuthor;
     private String storyContentEn;
     private String storyContentCn;
     private int storyConut;
}
package com.yuan.estest.entity;

import lombok.Data;

@Data
public class AccountEntity {

    private Integer account_number;
    private Integer balance;
    private String firstname;
    private String lastname;
    private Integer age;
    private String gender;
    private String address;
    private String employer;
    private String email;
    private String city;
    private String state;

}

6.4.2.Controller



import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yuan.estest.entity.AccountEntity;
import com.yuan.estest.entity.StoryEntity;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.DocWriteRequest;
import org.elasticsearch.action.bulk.BulkItemResponse;
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.Node;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.Avg;
import org.elasticsearch.search.aggregations.metrics.AvgAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
public class Test {

    @Autowired
    private RestHighLevelClient client;

    private String INDEX_NAME = "testdata";

    /***************DATA**********************/

    public List<StoryEntity> getDatasForList(){
        List<StoryEntity> list = new ArrayList<>();
        StoryEntity storyEntity1 = new StoryEntity();
        storyEntity1.setStoryTitle("笑看人生");
        storyEntity1.setStoryAuthor("开心");
        storyEntity1.setStoryContentEn("happy lift");
        storyEntity1.setStoryContentCn("一生无苦");
        storyEntity1.setStoryConut(10);
        list.add(storyEntity1);

        StoryEntity storyEntity2 = new StoryEntity();
        storyEntity2.setStoryTitle("华英雄");
        storyEntity2.setStoryAuthor("黄玉郞");
        storyEntity2.setStoryContentEn("hua hero");
        storyEntity2.setStoryContentCn("一个叫华英雄的英雄");
        storyEntity2.setStoryConut(1079867);
        list.add(storyEntity2);

        StoryEntity storyEntity3 = new StoryEntity();
        storyEntity3.setStoryTitle("英雄一生都欢乐");
        storyEntity3.setStoryAuthor("笑英雄");
        storyEntity3.setStoryContentEn("hero is happy");
        storyEntity3.setStoryContentCn("英雄一生都开心");
        storyEntity3.setStoryConut(1267340);
        list.add(storyEntity3);


        return list;
    }



    /**
     * 新增一条
     */
    @RequestMapping("/indexData")
    public void indexData() throws IOException {



        //信息
        StoryEntity storyEntity = new StoryEntity();
        storyEntity.setStoryTitle("英雄之歌");
        storyEntity.setStoryAuthor("流浪英雄");
        storyEntity.setStoryContentEn("hero's song");
        storyEntity.setStoryContentCn("一首英雄的赞歌");
        storyEntity.setStoryConut(1267340);
        // 将对象转为json
        String data = JSONObject.toJSONString(storyEntity);

        IndexRequest indexRequest = new IndexRequest(INDEX_NAME);
        indexRequest.id("7");
        // 保存, 指明类型
        indexRequest.source(data, XContentType.JSON);
        IndexRequest timeout = indexRequest.timeout("50s");
        System.out.println("timeout = " + timeout);
        // 执行
        IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
        // 获取响应数据
        log.info("创建状态:{}", response.status());
    }



    /**
     * 获取文档信息
     */
    public void getDocument() throws IOException {
        // 创建获取请求对象
        GetRequest getRequest = new GetRequest(INDEX_NAME, "7");
        GetResponse response = client.get(getRequest, RequestOptions.DEFAULT);
        System.out.println(response.getSourceAsString());
    }



    /**
     * 批量新增
     */
    @RequestMapping("/syncData")
    public String syncData() {

        //批量数据
        List<StoryEntity> list = getDatasForList();

        // 1.创建Request
        BulkRequest request = new BulkRequest();

        //下面尽量控制一下一次bulk的数量,如果数据过大,条数过多可能出现同步不完全的情况
        for (StoryEntity storyEntity : list) {

            String jsonString = JSONObject.toJSONString(storyEntity);
            IndexRequest indexRequest = new IndexRequest(INDEX_NAME);
            indexRequest.source(jsonString, XContentType.JSON);
            // 保存, 指明类型
            request.add(indexRequest);
        }
        try {
            BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);
            System.out.println("response = " + response);

            if (response.hasFailures()) {
                exceptionRetry(request, response);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "over";
    }

    /**
     * 不重要
     * 异常捕获用于重试
     */
    private void exceptionRetry(BulkRequest request, BulkResponse response) {
        List<DocWriteRequest<?>> list = request.requests();
        BulkRequest requestRetry = new BulkRequest();
        //下面尽量控制一下一次bulk的数量,如果数据过大,条数过多可能出现同步不完全的情况
        for (BulkItemResponse bir : response) {
            if (bir.isFailed()) {
                int docIndex = bir.getItemId();
                IndexRequest ir = (IndexRequest) list.get(docIndex);
                requestRetry.add(new IndexRequest(INDEX_NAME).source(ir.sourceAsMap(), XContentType.JSON));
            }
        }
        try {
            //遇到错误,休眠1s后重试
            Thread.sleep(1000);
            BulkResponse responseRetry = client.bulk(requestRetry, RequestOptions.DEFAULT);
            //重试仍然失败时记录该数据
            exceptionLog(requestRetry, responseRetry);
        } catch (Exception e) {
            log.error("ES同步重试出错!", e);
        }
    }

    /**
     * 不重要
     * 重试结果判断
     */
    private void exceptionLog(BulkRequest request, BulkResponse response) {
        List<DocWriteRequest<?>> list = request.requests();
        for (BulkItemResponse bir : response) {
            if (bir.isFailed()) {
                int docIndex = bir.getItemId();
                IndexRequest ir = (IndexRequest) list.get(docIndex);
                //记录失败原因及失败数据
                log.error("同步重试失败reason=[{}],data=[{}]", bir.getFailureMessage(), ir.sourceAsMap().toString());
            }
        }
    }





    /**************************************修改*******************************************/
    /**
     * 根据查询将数据更新,唯一主键
     *         "_id" : "7",
     *         "_score" : 1.0,
     *         "_source" : {
     *           "storyTitle" : "英雄故事",
     *           "storyAuthor" : "不是真龙",
     *           "storyContentEn" : "Hero story",
     *           "storyContentCn" : "英雄故事",
     *           "storyConut" : 1234
     *         }
     */
    @RequestMapping("/updateById")
    public String updateById() {

        StoryEntity storyEntity = new StoryEntity();
        storyEntity.setStoryTitle("英雄故事");
        storyEntity.setStoryAuthor("不是真龙也不是英雄");
        storyEntity.setStoryContentEn("Hero story");
        storyEntity.setStoryContentCn("英雄故事");
        storyEntity.setStoryConut(1234);

        if (log.isDebugEnabled()) {
            log.info("es开始更新数据:{}", JSON.toJSONString(storyEntity));
        }

        // 创建索引请求对象
        UpdateRequest request ;
        try {
             String data = JSONObject.toJSONString(storyEntity);
            request = new UpdateRequest(INDEX_NAME, "7").doc(data, XContentType.JSON);
            UpdateResponse response = client.update(request,  RequestOptions.DEFAULT);
            log.info("更新状态:{}", response.getResult());
        } catch (IOException e) {
            log.error("更新写入异常:{}", e.getMessage(), e);
        }
        if (log.isDebugEnabled()) {
            log.info("es更新数据完成");
        }
        return "over";
    }

    /**
     * 删除文档信息
     */
    public void deleteDocument() throws IOException {

        // 创建删除请求对象
        DeleteRequest deleteRequest = new DeleteRequest(INDEX_NAME, "7");
        // 执行删除文档
        DeleteResponse response = client.delete(deleteRequest, RequestOptions.DEFAULT);
        log.info("删除状态:{}", response.status());
    }


//------------------------------------------------------------------------

    /**
     * 获取文档信息
     */
    @RequestMapping("/query")
    public void query() throws IOException {
        // 1.准备Request
        SearchRequest searchRequest = new SearchRequest(INDEX_NAME);
        // 2.准备DSL
        searchRequest.source().query(QueryBuilders.matchAllQuery());
        // 3.发送请求
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        System.out.println("searchResponse = " + searchResponse);

    }



    @RequestMapping("/queryBySearch")
    public void queryBySearch() throws IOException {

        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("bank");

        // 设置 查询条件
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(QueryBuilders.matchQuery("address", "mill"));

        TermsAggregationBuilder ageAgg = AggregationBuilders.terms("ageAgg").field("age").size(10);
        sourceBuilder.aggregation(ageAgg);

        AvgAggregationBuilder balanceAvg = AggregationBuilders.avg("balanceAvg").field("balance");
        sourceBuilder.aggregation(balanceAvg);

        System.out.println("1:==>sourceBuilder.toString() = " + sourceBuilder);
        searchRequest.source(sourceBuilder);

        // 分析结果
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        System.out.println("2:==>searchResponse = " + searchResponse);


        // 查询结果
        SearchHits searchHits = searchResponse.getHits();
        System.out.println("hits.getTotalHits() = " + searchHits.getTotalHits());

        SearchHit[] hitss = searchHits.getHits();
        for (SearchHit hit : hitss) {
            String sourceAsString = hit.getSourceAsString();
            System.out.println("sourceAsString = " + sourceAsString);
            AccountEntity accountEntity = JSONObject.parseObject(sourceAsString, new TypeReference<AccountEntity>() {});
            System.out.println("accountEntity = " + accountEntity);
        }

        // 聚合信息
        Aggregations aggregations = searchResponse.getAggregations();

        Terms ageAggData = aggregations.get("ageAgg");
        for (Terms.Bucket bucket : ageAggData.getBuckets()) {
            System.out.println("bucket.getKeyAsString() = " + bucket.getKeyAsString());
        }
        Avg balanceAvgData = aggregations.get("balanceAvg");
        System.out.println("balanceAvgData.getValue() = " + balanceAvgData.getValue());
    }


    @RequestMapping("/testES")
    public void testES() throws IOException {
        List<Node> nodes = client.getLowLevelClient().getNodes();
        nodes.forEach(node -> {
            System.out.println(node.getHost());
        });
        System.out.println(client);

    }
}


到了这里,关于ElasticSearch ( 六 ) 与SpringBoot整合的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • ElasticSearch(九)【SpringBoot整合】

    上一篇文章 《ElasticSearch - 过滤查询》 9.1 基本环境配置 创建一个springboot工程 springboot-elasticsearch 在 pom.xml 导入依赖 【 注意 】使用的springboot需要根当前ES版本兼容 配置 application.yml 文件 配置客户端 创建config包,添加配置类 RestClientConfiguration.class 配置完之后,该配置类不仅

    2024年02月14日
    浏览(26)
  • Springboot整合Elasticsearch(Es)

    首先 在测试类中引入RestHighLevelClient对象 其次 准备一个User对象 3.1.1 创建索引  运行结果:创建成功返回true 3.1.2 删除索引 运行结果:删除成功返回true  3.1.3 判断索引是否存在 运行结果:存在返回true,不存在返回false. 3.2.1 添加文档 运行结果:添加成功返回 CREATED 3.2.2 查询文档--

    2023年04月22日
    浏览(33)
  • ElasticSearch ( 六 ) 与SpringBoot整合

    https://www.elastic.co/guide/en/elasticsearch/client/java-rest/ 如果当前springboot所默认依赖的版本与es版本不相同 指明服务器的IP 6.4.1.实体类 6.4.2.Controller

    2024年02月11日
    浏览(28)
  • springboot整合elasticsearch使用案例

    完成搜索和分页 添加品牌、城市、星级、价格等过滤功能 搜索我附近的酒店    让指定的酒店在搜索结果中排名置顶 添加isAD字段

    2024年02月09日
    浏览(28)
  • springboot整合elasticsearch8

    1.引入maven依赖 2.application.yml添加配置 3.编写config文件 启动demo项目,通过控制台日志查看是否能够正常连接es。 4.在DemoApplicationTests编写简单测试操作es。

    2024年02月12日
    浏览(39)
  • springboot 整合 ElasticSearch 方法 (二)

    在pom.xml文件中需要引入3个依赖, 三个都必须有并且三个依赖的版本要一致 , 不然会报错. 不一定是 7.6.1 这个版本 , 只需要保证这三个依赖的版本一致就可以了. 用配置类的方法来配置 es, 配置文件中就 只需要写一下用户名和密码之类的就可以了, 我们用 @Value 拿到它 . 设置用户

    2024年01月24日
    浏览(70)
  • SpringBoot 3整合Elasticsearch 8

    官网说明 本文使用最新的版本 springboot: 3.2.3 spring-data elasticsearch: 5.2.3 elasticsearch: 8.11.4 elasticsearch下载链接:https://www.elastic.co/cn/downloads/past-releases#elasticsearch 最新版可能不兼容,以spring官网为准 使用 https 必须配置 username 和 password spring data的repository方便操作,类似jpa的操作

    2024年04月11日
    浏览(20)
  • SpringBoot整合ElasticSearch版本问题

    最近在整个这两个框架,发现老是版本对不上,不是缺少类,就是启动不了,美好的一下午就这样浪费了,多说一句废话,es的版本更新速度也太快了,如果spring boot已经固定的,注意一下es的版本。 下面的这个链接是spring官方提供的兼容版本 springboot与elasticsearch兼容版本对应

    2024年02月15日
    浏览(35)
  • 配置https ssl elasticsearch,springboot项目中连接elasticsearch https

    参考之前的文章 创建self-signed证书 下面展示一些 内联代码片 。 启动springboot项目应该可以连接上elasticsearch了。

    2024年02月11日
    浏览(30)
  • Elasticsearch 整合springboot-Elasticsearch文章二

    https://www.elastic.co/cn/ https://docs.spring.io/spring-data/elasticsearch/docs/4.4.10/reference/html/ 我们选用的是elasticsearch 7.17.9版本,对应的,我们需要升级springboot版本,对应的中间件都需要升级 Springboot: 2.7.10 spring-data-elasticsearch: 4.4.10 spring-boot-starter-data-elasticsearch: 2.7.10 https://github.com/OrderDo

    2024年02月15日
    浏览(32)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包