springboot整合ES索引引擎

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

在Springboot整合ES提供了启动依赖jar。 该jar包封了一个类: RestHighLevelClient 该类可以对象ES中各个接口进行相应的操作。

1.创建springboot工程并导入相关的依赖 2.3.12.RELEASE

springboot整合es搜索,spring boot

2.创建一个配置类,返回 RestHighLevelClient

修改一下版本
springboot整合es搜索,spring boot
创建路径config包下的ESconfig类

@Configuration
public class ESConfig {
    // springboot连接ES提供的一个客户端类.RestHighLevelClient
    @Bean
    public RestHighLevelClient restHighLevelClient(){
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("127.0.0.1",9200,"http"))
        );
        return client;
    }
}

3.测试

在Test中测试

@SpringBootTest
class SpringbootESday02ApplicationTests{
    // 注入
    @Autowired
    private RestHighLevelClient client;
    // 创建ES的索引
    @Test
    void createIndex() throws Exception{
        // 创建createIndexRequest请求类---对应到索引的创建需要的参数 都封装到该类中
        CreateIndexRequest indexRequest = new CreateIndexRequest("celebrity");
        // 关于对索引操作的功能都IndiceClient类中
        CreateIndexResponse indexResponse = client.indices().create(indexRequest, RequestOptions.DEFAULT);
        System.out.println("是否创建索引成功:"+indexResponse.isAcknowledged());
    }

}

结果:springboot整合es搜索,spring boot

判断索引是否存在

 // 判断索引是在否存在
    @Test
    public void isExist() throws Exception {
        GetIndexRequest getIndexRequest = new GetIndexRequest("celebrity");
        boolean exists = client.indices().exists(getIndexRequest,RequestOptions.DEFAULT);
        System.out.println("索引是否存在:"+exists);
    }

结果:
springboot整合es搜索,spring boot

删除索引

 // 删除索引
    @Test
    public void delete() throws IOException {
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("celebrity");
        AcknowledgedResponse delete = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
        System.out.println("是否删除成功:"+delete.isAcknowledged());
    }

结果:springboot整合es搜索,spring boot

在索引中添加文档

添加pom.xml依赖(转JSON数据)
<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.62</version>
        </dependency>

在entity包创建celebrity实体类

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Celebrity {
    private String name;
    private String address;
    private String sex;
    private Integer age;
}
  // 索引中添加文档
    @Test
    public void insertDoc()throws Exception{
        IndexRequest indexRequest = new IndexRequest("celebrity");
        // 指定文档id的值
        indexRequest.id("1");
        // 指定文档内容
        Celebrity celebrity = new Celebrity("李四","中国","男",18);
        indexRequest.source(JSON.toJSONString(celebrity), XContentType.JSON);
        IndexResponse index = client.index(indexRequest, RequestOptions.DEFAULT);
        System.out.println("结果:"+index.getResult());

    }

结果:springboot整合es搜索,spring boot

根据id查询文档内容

  //根据id查询文档内容
    @Test
    public void findByid() throws IOException {
        GetRequest getRequest=new GetRequest("celebrity");
        getRequest.id("1");
        GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);

        Map<String, Object> sourceAsMap = getResponse.getSourceAsMap();
        System.out.println(sourceAsMap.get("name")+"===="+sourceAsMap.get("sex"));


        String sourceAsString = getResponse.getSourceAsString();
        System.out.println(sourceAsString);
    }

结果:springboot整合es搜索,spring boot

根据id删除文档

 // 根据id删除文档
    @Test
    public void deleteByid() throws Exception{
        DeleteRequest deleteRequest = new DeleteRequest("celebrity");
        deleteRequest.id("1");
        DeleteResponse delete = client.delete(deleteRequest, RequestOptions.DEFAULT);
        System.out.println(delete.getResult());
    }

结果:springboot整合es搜索,spring boot

根据id修改文档

    @Test
    public void update()throws Exception{
        UpdateRequest updateRequest = new UpdateRequest("celebrity", "2");
        Celebrity celebrity = new Celebrity();
        celebrity.setName("张学友");
        updateRequest.doc(JSON.toJSONString(celebrity),XContentType.JSON);
        UpdateResponse update = client.update(updateRequest, RequestOptions.DEFAULT);
    }

结果:根据上面根据id查询一下
springboot整合es搜索,spring boot

批量添加文档

// 批量添加文档
    @Test
    public void batchInsert() throws Exception{
        BulkRequest bulkRequest = new BulkRequest("celebrity");
        List<Celebrity> list = new ArrayList<>();
        list.add(new Celebrity("张三","北京","男",19));
        list.add(new Celebrity("李四","天津","男",19));
        list.add(new Celebrity("王五","湖北","男",19));
        list.add(new Celebrity("赵六","湖南","男",19));

        for (Celebrity li : list){
            bulkRequest.add(new IndexRequest().source(JSON.toJSONString(li),XContentType.JSON));
        }
        client.bulk(bulkRequest,RequestOptions.DEFAULT);
    }

结果:springboot整合es搜索,spring boot

复杂查询:

@Test
    public void search() throws IOException {
    // 创建一个搜索请求对象
    SearchRequest searchRequest = new SearchRequest("celebrity");
    // 构建一个搜索条件对象
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    // 为条件对象添加query查询字段
    MatchQueryBuilder matchQuery = QueryBuilders.matchQuery("name", "张");
    searchSourceBuilder.query(matchQuery);
    // 搜索对象设置分页参数
    searchSourceBuilder.from(0);
    searchSourceBuilder.size(3);
    // 设置高亮
    HighlightBuilder highlightBuilder = new HighlightBuilder();
    highlightBuilder.field("name");
    highlightBuilder.preTags("<font color='red'>");
    highlightBuilder.postTags("</font>");
    searchSourceBuilder.highlighter(highlightBuilder);

    // 条件对象放入请求对象中
    searchRequest.source(searchSourceBuilder);

    SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
    SearchHits hits = searchResponse.getHits();
    System.out.println("总条数:"+hits.getTotalHits().value);

    System.out.println("===============记录内容============");
    SearchHit[] hits1 = hits.getHits();
    for (SearchHit hit : hits1){
        System.out.println(hit.getId()+"-->"+hit.getSourceAsMap());
    }
    System.out.println("============高亮内容=============");
    for (SearchHit hit : hits1){
        System.out.println(hit.getHighlightFields().get("name").getFragments()[0]);
    }
    }

结果:springboot整合es搜索,spring boot文章来源地址https://www.toymoban.com/news/detail-516603.html

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

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

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

相关文章

  • elasticsearch(ES)分布式搜索引擎01——(初识ES,索引库操作和文档操作,RestClient操作索引库和文档)

    1.1.1.elasticsearch的作用 elasticsearch是一款非常强大的开源搜索引擎,具备非常多强大功能,可以帮助我们从海量数据中快速找到需要的内容 1.1.2.ELK技术栈 elasticsearch结合kibana、Logstash、Beats,也就是elastic stack(ELK)。被广泛应用在日志数据分析、实时监控等领域: 而elasticsearc

    2024年02月07日
    浏览(52)
  • springboot整合es进行搜索

    Elasticsearch 的官方地址:https://www.elastic.co/cn/ 这里选择下载 7.8.0 版本 下载地址:https://www.elastic.co/cn/downloads/past-releases#elasticsearch Elasticsearch 分为 Linux 和 Windows 版本,这里我们只是做测试,因此下载 windows 版本即可。 Windows 版的 Elasticsearch 的安装很简单,解压即安装完毕,解

    2024年02月08日
    浏览(33)
  • SpringBoot 整合 ES 进行各种高级查询搜索

    上一章:《ElasticSearch集群的搭建》 如果你还未安装es的相关信息,请先移步至:《ElasticSearch安装》进行安装 如果您的SpringBoot项目还未整合es,请移步至:《SpringBoot整合ElasticSearch实现模糊查询,批量CRUD,排序,分页,高亮》 同时本文的操作中涉及到ElasticSearchRepository和Ela

    2023年04月15日
    浏览(40)
  • Spring Boot整合ES的两种方式

    在Spring Boot中整合Elasticsearch的方式之一是使用Elasticsearch的官方Spring Data Elasticsearch Starter。该Starter提供了对Elasticsearch的高级集成,简化了配置和管理Elasticsearch客户端。 下面是使用Spring Data Elasticsearch Starter的详细介绍以及如何管理客户端对象的初始化和关闭操作: 添加依赖坐

    2024年02月13日
    浏览(31)
  • ES 部署,问题及整合spring boot 简单使用

    先前部署项目部署es的流程 https://www.elastic.co/cn/downloads/elasticsearch 解压安装elasticsearch config/elasticsearch.yml discovery,seed_hosts:[“127.0.0.1:9200”] cluster.initial_master_nodes:[“node-1”] 因为部分elasticsearch支持的jdk版本不一样,所以可以自己再给elasticsearch配置一个jdk版本 因为elasticsearch不能

    2023年04月09日
    浏览(32)
  • 【Elasticsearch】SpringBoot整合ES实现搜索功能 | 高亮显示

    先看代码: controller: serviceImpl: 小结 : 1、添加ES场景启动器 2、yaml配置ES 3、准备需要用到的变量 注:还有一个注入的RestHighLevelClient 结构如下: 具体调用的方法以及设置页码等参看代码。 加断点查看对应searchResponse数据结构: HighlightFields的数据结构: 对照kinaba结果: 3、根

    2024年02月11日
    浏览(38)
  • 知识点13--spring boot整合elasticsearch以及ES高亮

    本章知识点沿用知识点12的项目,介绍如何使用spring boot整合ES,没有ES的去我主页 各类型大数据集群搭建文档--大数据原生集群本地测试环境搭建三 中可以看到ES如何搭建 不管你有没有ES,最好是没有,因为一定要知道一点,一定要去官网查一下你当前用的spring boot data es的版

    2024年02月12日
    浏览(37)
  • Spring Boot 整合 分布式搜索引擎 Elastic Search 实现 数据聚合

    本文参考黑马 分布式Elastic search Elasticsearch是一款非常强大的开源搜索引擎,具备非常多强大功能,可以帮助我们从海量数据中快速找到需要的内容 本篇文章将讲解 Elastic Search 如何实现数据聚合,以及 在项目实战中如何通过数据聚合实现业务需求并完成功能。 以下为官方

    2024年02月11日
    浏览(37)
  • Spring Boot 整合 分布式搜索引擎 Elastic Search 实现 搜索、分页与结果过滤

    本文参考黑马 分布式Elastic search Elasticsearch是一款非常强大的开源搜索引擎,具备非常多强大功能,可以帮助我们从海量数据中快速找到需要的内容 实现黑马旅游的酒店搜索功能,完成搜索和分页 在项目首页,有一个很大的搜索框、也有分页按钮 点击搜索按钮,可以

    2024年02月06日
    浏览(42)
  • ElasticSearch系列 - SpringBoot整合ES之全文搜索匹配查询 match

    官方文档地址:https://www.elastic.co/guide/en/elasticsearch/reference/index.html 权威指南:https://www.elastic.co/guide/cn/elasticsearch/guide/current/structured-search.html 1. 数据准备 官方测试数据下载地址:https://download.elastic.co/demos/kibana/gettingstarted/accounts.zip ,数据量很大,我们自己构造数据吧。 2. m

    2023年04月08日
    浏览(45)

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

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

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

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

二维码1

领取红包

二维码2

领红包