elkspringboot集成elasticsearch7.6.2springdata(代码片段)

DragonWu DragonWu     2022-10-23     451

关键词:

目录

一、依赖引入

二、配置yml

三、添加实体类

四、添加Dao层

五、渲染层

六、启动类

七、效果测试


本次小结为最简单易懂的总结,适用于7版本。


一、依赖引入

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.dragonwu</groupId>
    <artifactId>ES8-spring</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <version>2.3.2.RELEASE</version>
        <artifactId>spring-boot-starter-parent</artifactId>
        <relativePath/>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>


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

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

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>
</project>

这里最主要的依赖就是:

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

二、配置yml

spring:
  elasticsearch:
    rest:
      # 设置ES的地址
      uris: http://localhost:9200
  data:
    elasticsearch:
      repositories:
        # 设置springData自动创建表
        enabled: true

三、添加实体类

package com.dragonwu.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

/**
 * @author DragonWu
 * @date 2022-09-19 15:53
 **/
@Data
@AllArgsConstructor
@NoArgsConstructor
// Document设置ES里的索引名
@Document(indexName = "information")
public class Information 

    //store=true设置存储,index=false表示不进行索引搜索
    @Id
    @Field(type = FieldType.Long,store = true,index = false)
    private Long id;

    //analyzer设置分词器
    @Field(type = FieldType.Text,store = true,analyzer = "ik_max_word")
    private String data;

四、添加Dao层

集成抽象类,抽象类的第一个参数为对应的实体类,第二个参数为对应的id类型

package com.dragonwu.dao;

import com.dragonwu.entity.Information;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

/**
 * @author DragonWu
 * @date 2022-09-19 16:04
 **/
public interface InformationDao extends ElasticsearchRepository<Information,Long> 

五、渲染层

由于方便测试,我们这里直接忽略掉服务层

package com.dragonwu.controller;

import com.dragonwu.dao.InformationDao;
import com.dragonwu.entity.Information;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author DragonWu
 * @date 2022-09-19 16:31
 **/
@RestController
@RequestMapping("/information")
public class InformationTestController 
    @Autowired
    private InformationDao informationDao;

    /**
     * 新增或修改
     */
    @PostMapping("/save")
    public String save(@RequestBody Information information)
        informationDao.save(information);
        return "ok";
    

    /**
     * 删除
     */
    @DeleteMapping("/delete/id")
    public String delete(@PathVariable Long id)
        informationDao.deleteById(id);
        return "ok";
    

    /**
     * 查询
     */
    @GetMapping("/queryAll")
    public Iterable<Information>  queryAll()
        return informationDao.findAll();
    



六、启动类

package com.dragonwu;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author DragonWu
 * @date 2022-09-19 16:26
 **/
@SpringBootApplication
public class APP 
    public static void main(String[] args) 
        SpringApplication.run(APP.class,args);
    

七、效果测试

运行启动类,加载程序后索引将会被自动创建。

这里我们通过ApiPost进行测试。

可以看到索引已经被创建。 

访问增加和修改的接口都是这个:

 查询文档:

 查询成功。

通过springboot的api进行查询:

同样也是没问题的。

访问删除接口

 删除成功,再次查看文档

 可以看到文档已经被删除了。

演示结束。

精通系列springboot集成elasticsearch+项目实战(代码片段)

Java之ElasticSearch7.x.x+SpringBoot+爬虫+项目实战【一篇文章精通系列】【SpringBoot集成ElasticSearch+项目实战】一、ElasticSearch的Java官方文档1、查看官方文档2、找到了原生的依赖3、初始化二、创建SpringBoot项目1、创建项目2、... 查看详情

elasticsearch教程

章节Elasticsearch基本概念Elasticsearch安装Elasticsearch使用集群Elasticsearch健康检查Elasticsearch列出索引Elasticsearch创建索引Elasticsearch创建和查询文档Elasticsearch删除索引Elasticsearch修改数据Elasticsearch更新文档Elasticsearch删除文档Elasticsea 查看详情

springboot--elk快速搭建(代码片段)

文章目录搭建ELKSpringboot使用ELKKibana查看示例代码快速搭建自己的日志收集,方便各个微服务的日志收集。搭建ELK采用docker-elk进行快速搭建ELK的环境进行clone仓库gitclonehttps://github.com/deviantony/docker-elk.git开启docker-composeup-d(需要... 查看详情

elk

https://www.elastic.co/elasticsearch:可以看成一个分布式的NoSql数据库,基于Lucene,开放了restfulapi和多种语言的编程接口。             支持复杂的查询,而且查询高效,sql能查的东西elasti... 查看详情

elasticsearch后台运行步骤

Elasticsearch后台运行步骤1.cmd到elasticsearch中bin目录下2.elasticsearch-service出现 3.安装服务elasticsearch-serviceinstall 4.elasticsearch-servicemanager启动服务  过程中出现了错误 提示JAVA版本是32位的与elasticsea 查看详情

es基础用法

1、简介: Elasticsearch是一个兼有搜索引擎和NoSQL数据库功能的开源系统,基于Java/Lucene构建,可以用于全文搜索,结构化搜索以及近实时分析。可以说Lucene是当今最先进,最高效的全功能开源搜索引擎框架。说明:Lucene:只是... 查看详情

elasticsearch之插件介绍及安装

...介:监控es状态的插件,推荐!【目前不支持2.x】  2、ElasticsearchHeadPlugin(作者BenBirch)(主要)    简介:很方便对es进行各种操作的客户端。  3、kopfPlugin(作者lmenezes)(主要)    Kopf是一个ElasticSea 查看详情

elasticsearch拼音搜索

...流程,如下:1.下载安装分词插件 https://github.com/medcl/elasticsea 查看详情

集成与持续集成介绍(代码片段)

1.集成与持续集成介绍1.1什么是集成简单来说,就是把开发好的代码,提交到系统中,就是集成。1.2什么是持续集成持续集成就是频繁的(一天多次)将代码集成到主干。1.3使用持续集成带来的好处(1)快速发现错误。每完成一... 查看详情

解决elasticsearch启动时常见的错误(代码片段)

注:elasticsearch需要jdk8以上的环境,需要先安装有jdk8以上,才能运行。错误1:不能以root用户运行org.elasticsearch.bootstrap.StartupException:java.lang.RuntimeException:cannotrunelasticsearchasrootatorg.elasticsearch.bootstrap.Elasticsea 查看详情

elasticsearch5.4配置文件重要配置介绍

...bsp;-----------------------------------#配置es的集群名称,默认是elasticsearch,es会自动发现在同一网段下的es,如果在同一网段下有多个集群,就可以用这个属性来区分不同的集群。cluster.name: elasticsea 查看详情

集成测试

集成测试:在单元测试的基础上,将所有模块按照总体设计的要求组装成为子系统或系统进行的测试。集成测试的对象是模块间的接口,其目的是找出在模块接口上和系统体系结构上的问题。集成测试策略:基于层次的集成:自... 查看详情

集成测试

1.定义:  集成测试,也叫组装测试或联合测试。在单元测试的基础上,将所有模块按照设计要求(如根据结构图)组装成为子系统或系统,进行集成测试。2.方案:  ·自顶向下  ·自底向上  ·核心集成  ·高频集成... 查看详情

浅谈持续集成的理解以及实现持续集成,需要做什么?

一、持续集成是什么?持续集成是一种软件开发的实践,即团队开发成员经常集成他们的工作,通常每个成员每天至少集成一次,也就意味着每天可能会发生多次集成。每次集成都通过自动化的构建(包括编译,发布,自动化测... 查看详情

持续集成简介

一、持续集成持续集成是一种软件开发的实践,即团队开发成员经常集成他们的工作,通常每个成员每天至少集成一次,也就意味着每天可能会发生多次集成。每次集成都通过自动化的构建(包括编译,发布,自动化测试)来验... 查看详情

持续集成与devops

持续集成   持续集成(Continuousintegration,简称C1),简单的说持续集成就是频紧地(一天多次)将代码集成到主干,它的好处主要有两个:1、快速发现错误。每完成一次更新,就集成到主干,可以快速发现错误,定位... 查看详情

并非所有术语都适用于 ElasticSearch 查询

】并非所有术语都适用于ElasticSearch查询【英文标题】:NotalltermsworkinElasticSearchQuery【发布时间】:2016-08-0218:42:46【问题描述】:使用https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html所述的术语搜索时,我没有... 查看详情

集成框架 - Apache Camel 与 Spring 集成? [关闭]

】集成框架-ApacheCamel与Spring集成?[关闭]【英文标题】:Integrationframework-apachecamelvsspringintegration?[closed]【发布时间】:2016-10-2614:30:59【问题描述】:我正在评估一个基于EIP(企业集成模式)用于我的应用程序的集成框架。我有apac... 查看详情