SpringBoot集成Elasticsearch

简介: SpringBoot集成Elasticsearch

1. 准备工作

  需要提前安装好Elasticsearch,访问地址:http://127.0.0.1:9200/ 得到以下结果,得到cluster_name,下面配置使用。

{
  "name" : "O8GslS3",
  "cluster_name" : "docker-cluster",
  "cluster_uuid" : "pviTqfXtR3GtnxF-Po-_aA",
  "version" : {
    "number" : "6.5.0",
    ......
  },
  "tagline" : "You Know, for Search"
}

 

 

2. 使用Maven创建SpringBoot工程

  配置Maven的pom.xml文件

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-parent</artifactId>
        <version>2.1.6.RELEASE</version>
    </parent>
 
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
    </dependencies>

  注意:spring-boot-starter-data-elasticsearch包,引用的是spring-data-elasticsearch包,而spring-data-elasticsearch包的版本与elasticsearch服务版本是有兼容性问题的。

  目前并不支持elasticsearch7.x,参考:https://github.com/spring-projects/spring-data-elasticsearch

  配置application.yml文件

spring:
  data:
    elasticsearch:
      cluster-name: docker-cluster
      cluster-nodes: 127.0.0.1:9300
      repositories:
        enabled: true

 

3. 代码

  实体类。使用@Document注解,参数indexName是索引名称,type是type名称。

// indexName索引名称,type类型
@Document(indexName = "houseindex", type = "house")
public class HouseIndexTemplate {
 
    @Id
    private Long houseId;
 
    // 使用分词器
    @Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_max_word")
    private String title;
 
    @Field(type = FieldType.Keyword)
    private String name;
 
    @Field(type = FieldType.Integer)
    private int price;
 
 
}    

  访问接口。使用@Repository注解,并继承ElasticsearchRepository接口,就可以直接访问的。

  有两个参数:1.返回的对象,2.ID参数数据类型

@Repository
public interface HouseRepository extends ElasticsearchRepository<HouseIndexTemplate, Long> {
 
}

  测试用例

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class UserServiceTest {
 
    @Autowired
    private HouseRepository houseRepository;
 
    @Test
    public void selectUser(){
        HouseIndexTemplate template = new HouseIndexTemplate();
        template.setId(1);
        template.setName("Tom");
        houseRepository.save(template);
    }
 
}

 

 

4. 异常解释

  问题1: NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{IVH9QII0QrOU9GkXdsJPiA}{127.0.0.1}{127.0.0.1:9300}]]

  原因:这是说配置的节点不可用,原因答题有3种可能:(1)IP地址或端口填写有误;(2)cluster_name填写有误;(3)Elasticsearch服务已关闭

相关实践学习
以电商场景为例搭建AI语义搜索应用
本实验旨在通过阿里云Elasticsearch结合阿里云搜索开发工作台AI模型服务,构建一个高效、精准的语义搜索系统,模拟电商场景,深入理解AI搜索技术原理并掌握其实现过程。
ElasticSearch 最新快速入门教程
本课程由千锋教育提供。全文搜索的需求非常大。而开源的解决办法Elasricsearch(Elastic)就是一个非常好的工具。目前是全文搜索引擎的首选。本系列教程由浅入深讲解了在CentOS7系统下如何搭建ElasticSearch,如何使用Kibana实现各种方式的搜索并详细分析了搜索的原理,最后讲解了在Java应用中如何集成ElasticSearch并实现搜索。 &nbsp;
相关文章
存储 JSON Java
658 0
|
11月前
|
人工智能 运维 自然语言处理
Elasticsearch AI Assistant 集成 DeepSeek,1分钟搭建智能运维助手
Elasticsearch 新支持 DeepSeek 系列模型,使用 AI 助手,通过自然语言交互,为可观测性分析、安全运维管理及数据智能处理提供一站式解决方案。
1281 3
Elasticsearch AI Assistant 集成 DeepSeek,1分钟搭建智能运维助手
|
消息中间件 监控 Java
您是否已集成 Spring Boot 与 ActiveMQ?
您是否已集成 Spring Boot 与 ActiveMQ?
447 0
|
JSON Java API
springboot集成ElasticSearch使用completion实现补全功能
springboot集成ElasticSearch使用completion实现补全功能
256 1
|
开发框架 监控 搜索推荐
GoFly快速开发框架集成ZincSearch全文搜索引擎 - Elasticsearch轻量级替代为ZincSearch全文搜索引擎
本文介绍了在项目开发中使用ZincSearch作为全文搜索引擎的优势,包括其轻量级、易于安装和使用、资源占用低等特点,以及如何在GoFly快速开发框架中集成和使用ZincSearch,提供了详细的开发文档和实例代码,帮助开发者高效地实现搜索功能。
836 0
|
Web App开发 JavaScript Java
elasticsearch学习五:springboot整合 rest 操作elasticsearch的 实际案例操作,编写搜索的前后端,爬取京东数据到elasticsearch中。
这篇文章是关于如何使用Spring Boot整合Elasticsearch,并通过REST客户端操作Elasticsearch,实现一个简单的搜索前后端,以及如何爬取京东数据到Elasticsearch的案例教程。
900 0
elasticsearch学习五:springboot整合 rest 操作elasticsearch的 实际案例操作,编写搜索的前后端,爬取京东数据到elasticsearch中。
|
Java Spring
解决Springboot集成ElasticSearch 报错:A bean with that name has already been defined in null and overriding
解决Springboot集成ElasticSearch 报错:A bean with that name has already been defined in null and overriding
510 2
|
搜索推荐 Java 数据库
springboot集成ElasticSearch的具体操作(系统全文检索)
springboot集成ElasticSearch的具体操作(系统全文检索)
|
Java Windows
【极光系列】springBoot集成elasticsearch
【极光系列】springBoot集成elasticsearch
377 2
|
Java
【极问系列】springBoot集成elasticsearch出现Unable to parse response body for Response
【极问系列】springBoot集成elasticsearch出现Unable to parse response body for Response
1610 2

热门文章

最新文章