elasticsearch集成springboot详细使用

news2024/9/20 14:34:37
1.es下载&配置

配置JVM

在这里插入图片描述

配置跨域
在这里插入图片描述

配置https和密码

在这里插入图片描述

2.es启动

.\elasticsearch.bat

或 后台启动:
nohup ./bin/elasticsearch&

浏览器访问:https://localhost:9200
输入账户:elastic / 123456
在这里插入图片描述

3.重置es密码

.\elasticsearch-reset-password.bat -u elastic -i

在这里插入图片描述

4.安装图形化插件:elasticsearch-head插件,完成图形化界面的效果,完成索引数据的查看
  1. es核心概念

    面向文档

    可以对文档(而非成行成列的数据)进行索引、搜索、排序、过滤

    Elasticsearch对比传统[关系型数据库]如下:

    Relational DB ‐> Databases ‐> Tables ‐> Rows ‐> Columns
    Elasticsearch ‐> Index ‐> Types ‐> Documents ‐> Fields

集群cluster:一个集群就是由一个或多个节点组织在一起,它们共同持有整个的数据,并一起提供索引和搜索功能。一个集群由 一个唯一的名字标识,这个名字默认就是“elasticsearch”。这个名字是重要的,因为一个节点只能通过指定某个集 群的名字,来加入这个集群。

节点node:一个节点是集群中的一个服务器,作为集群的一部分,它存储数据,参与集群的索引和搜索功能;一个节点可以通过配置集群名称的方式来加入一个指定的集群。默认情况下,每个节点都会被安排加入到一个叫 做“elasticsearch”的集群中

分片和复制 shards&replicas:一个索引可以存储超出单个结点硬件限制的大量数据

ElasticSearch客户端操作:

  • 使用elasticsearch-head插件
  • 使用elasticsearch提供的Restful接口直接访问
  • 使用elasticsearch提供的API进行访问

Elasticsearch的接口语法:

在这里插入图片描述
在这里插入图片描述

查询文档document有三种方式:

  • 根据id查询;
  • 根据关键词查询
  • 根据输入的内容先分词,再查询
  1. elasticsearch集成springboot使用
      <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
1.创建实体

import lombok.Data;
import org.apache.catalina.Store;
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;

@Document(indexName = "article")
@Data
public class Article {
    @Id
    @Field(index = false,type = FieldType.Integer)
    private Integer id;

    /**
     * index:是否设置分词  默认为true
     * analyzer:储存时使用的分词器
     * searchAnalyze:搜索时使用的分词器
     * store:是否存储  默认为false
     * type:数据类型  默认值是FieldType.Auto
     *
     */
    @Field(analyzer = "ik_smart",searchAnalyzer = "ik_smart",store = true,type = FieldType.Text)
    private String title;

    @Field(analyzer = "ik_smart",searchAnalyzer = "ik_smart",store = true,type = FieldType.Text)
    private String context;

    @Field(store = true,type =FieldType.Integer)
    private  Integer hits;
}


2.创建CRUD操作类
import com.cloud.entities.Article;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 *  自定义接口需要继承ElasticsearchRepository<实体类型,主键类型>  基本的crud 分页
 */
@Component
public interface ArticalRepository extends ElasticsearchRepository<Article,Integer> {
    List<Article> findByTitle(String title);


    List<Article> findArticleByTitleOrContext(String title,String context);

    /**
     * 根据标题或内存查询(含分页)
      * @param title
     * @param context
     * @param pageable0
     * @return
     */
    List<Article> findByTitleOrContext(String title, String context, Pageable pageable0);

}

3.创建启动入口类
@SpringBootApplication
@MapperScan("com.cloud.mapper")  //import tk.mybatis.spring.annotation.MapperScan
public class Main8001 {
    public static void main(String[] args) {
        SpringApplication.run(Main8001.class,args);
    }
  }
4.配置springboot配置es
  spring:
    elasticsearch:
    	uris: http://localhost:9200
   		connection-timeout: 5s
        #username: elastic
        #password: 123456
  
5.创建单元测试类
import com.cloud.Main8001;
import com.cloud.entities.Article;
import com.cloud.repository.ArticalRepository;
import jakarta.annotation.Resource;
import org.elasticsearch.client.RestClient;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.data.elasticsearch.core.query.UpdateQuery;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Arrays;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Main8001.class)
public class ArticleTest {


    @Resource
    private RestClient restClient;

    @Resource
    private ElasticsearchTemplate elasticsearchTemplate;

    @Resource
    private ArticalRepository articalRepository;

    /**1.
     * 使用ElasticsearchTemplate
     */
    @Test
    void insert(){
        Article article=new Article();
        article.setTitle("男乒乓");
        article.setId(1);
        article.setContext("中国赢了");
        article.setHits(100);
        elasticsearchTemplate.save(article);
    }

    /**
     * 2.
     * 使用Repository
     */
    @Test
    void insert2(){
        Article article=new Article();
        article.setTitle("男乒乓2");
        article.setId(2);
        article.setContext("中国赢了2");
        article.setHits(120);
        articalRepository.save(article);
    }

    /**
     * 批量保存
     */
    @Test
    void insert3(){
        Article article=new Article();
        article.setTitle("男乒乓3");
        article.setId(3);
        article.setContext("中国赢了2");
        article.setHits(130);
        articalRepository.saveAll(Arrays.asList(article));
    }


    @Test
    void update(){
        Article article= articalRepository.findById(2).get();
        article.setTitle("篮球");
        articalRepository.save(article);
    }

    @Test
    void update2(){
        Article article=new Article();
        article.setId(2);
        article.setTitle("网球");
        elasticsearchTemplate.update(article);
    }

    /**
     *  查询全部数据
     */

    @Test
    void findAll(){
        Iterable<Article> articles=articalRepository.findAll();
        articles.forEach(System.out::println);
    }


    /**
     * 根据id删除
     */

    @Test
    void deleteById(){
        articalRepository.deleteById(3);
    }

    /**
     * 删除:传入实体类删
     */
    @Test
    void delete(){
        Article article=new Article();
        article.setId(3);
        articalRepository.delete(article);
    }



    /**
     * 删除索引里面所有数据
     */
    @Test
    void deleteAll(){
        articalRepository.deleteAll();
    }


}

  1. 测试工具:postman
  2. es 参考资料:

https://cloud.tencent.com/developer/article/2249187

https://www.hadoopdoc.com/elasticsearch/elasticsearch-begin-tutorial

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2038384.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

C:指针和数组之间的关系-学习笔记

目录 闲话&#xff1a; 引言&#xff1a; 1、数组名的理解 2、指针访问数组 3、一维数组传参的本质 4、二级指针 5、指针数组 6、指针数组模拟二维数组 结语&#xff1a; 闲话&#xff1a; 指针这个模块更新的比较慢&#xff0c;主要是小编还得学习指针的知识点&#…

ubuntu18.04 设置静态地址

修改配置文件 sudo vim /etc/netplan/01-network-manager-all.yaml 代码如下&#xff1a; network: version: 2 renderer: NetworkManager ethernets: ens33: # 配置的网卡名称&#xff0c;可以使用ifconfig -a查看本机的网卡 dhcp4: no # 关闭动态IP设置 …

黑神画Ⅹ--自主人工智能代理:从概念到实际应用

自主人工智能代理通过独立执行任务并做出最终决策来改变技术。与传统人工智能不同&#xff0c;它们能够分析、规划、适应并从经验中学习。机器学习和自然语言处理的进步扩大了它们在个人助理、聊天机器人、管理系统和自动驾驶汽车中的应用&#xff0c;展示了它们在各个领域的潜…

springmail发送邮件如何实现邮件动态内容?

springmail发送邮件怎么样&#xff1f;springmail发信优化方法&#xff1f; SpringMail作为一个强大的邮件发送框架&#xff0c;提供了多种方式来实现邮件内容的动态生成。AokSend将探讨如何通过SpringMail发送邮件&#xff0c;并动态生成邮件内容以满足不同的需求。 springm…

【图像去雾系列】使用SSR/MSR/MSRCR/MSRCP/automatedMSRCR算法对单图像进行图像增强,达到去雾效果

目录 一 图像去雾算法概述 二 SSR/MSR/MSRCR算法 三 实践 一 图像去雾算法概述 近些年来,出现了众多的单幅图像去雾算法,其主要可以分为 3 类:基于图像增强的去雾算法、基于图像复原的去雾算法和基于 CNN 的去雾算法。 ▲基于图像增强的去雾算法 通过图像增强技术突出图…

“泰山众筹:革新消费模式“

亲爱的伙伴们&#xff0c;是否渴望探索一种集日常消费与财富增长于一体的创新方式&#xff1f;今天&#xff0c;让我带您领略泰山众筹的魅力&#xff0c;这一革命性的消费增值理念&#xff0c;将彻底颠覆您的消费体验&#xff0c;让每一笔支出都化作通往财富之路的基石。 ✨ 泰…

了解LVS,配置LVS

项目一、LVS 1.集群Cluster Cluster: 集群是为了解决某个特定问题将堕胎计算机组合起来形成的单个系统 LB&#xff1a;负载均衡 HA&#xff1a;高可用 HPC&#xff1a;高性能计算 2.分布式 分布式是将一个请求分成三个部分&#xff0c;按照功能拆分&#xff0c;使用微服…

MySQL的InnoDB的页里面存了些什么 --InnoDB存储梳理(三)

文章目录 创建新表页的信息新增一条数据根据页号找数据信息脚本代码py_innodb_page_info根据地址计算页号根据页号计算起始地址 主要介绍表空间索引页里面有哪些内容&#xff0c;数据在表空间文件里面是怎么组织的 创建新表页的信息 CREATE TABLE test8 (id bigint(20) NOT N…

Nginx Web UI 部署

目录 1. 安装Docker 2. 拉取镜像 3. 启动程序 4. 访问测试 1. 安装Docker 准备一台虚拟机&#xff0c;关闭防火墙和selinu&#xff0c;进行时间同步 下载docker并配置加速器 # step 1: 安装必要的一些系统工具 sudo yum install -y yum-utils device-mapper-persisten…

8B 端侧小模型 | 能力全面对标GPT-4V!单图、多图、视频理解端侧三冠王,这个国产AI开源项目火爆全网

这两天&#xff0c; Github上一个 国产开源AI 项目杀疯了&#xff01;一开源就登上了 Github Trending 榜前列&#xff0c;一天就获得将近600 star。 这个项目就是国内大模型四小龙之一面壁智能最新大打造的面壁「小钢炮」 MiniCPM-V 2.6 。它再次刷新端侧多模态天花板&#xf…

Cobalt Strike 4.8 用户指南-第一节-Cobalt Strike介绍及安装

一、欢迎使用Cobalt Strike Cobalt Strike 是一个用于对手模拟和红队行动的平台。用于执行有针对性的攻击并模拟高级威胁行为者的后渗透行动。本节介绍 Cobalt Strike 功能集支持的攻击过程。本手册的其余部分将详细讨论了这些功能。 # 概述 图中的 Intrumentation & Tel…

数据重塑之数据去重

下面内容摘录自&#xff1a; 4章7节&#xff1a;用R做数据重塑&#xff0c;数据去重和数据的匹配-CSDN博客文章浏览阅读23次。数据重塑是数据分析和数据清洗中的重要步骤&#xff0c;其中包括数据去重和数据匹配。理解这两个概念以及它们的实现方法对于有效处理和分析数据至关重…

告别转换难题,四款PDF转CAD工具分享

CAD很难搞&#xff0c;将PDF转换为CAD更难搞&#xff0c;想要快速且完整的将PDF文件转换为CAD&#xff0c;自然不是靠一点点的复制重做&#xff0c;直接用PDF转CAD工具就能搞定。那用什么工具呢&#xff1f;我这就给你们捋捋几个神器是怎么帮我们搞定这个难题的。 1.PDF365在线…

milvus helm k8s开启权限管理,attu管理

version:2.4.5 apiVersion: v1 kind: ConfigMap metadata: name: my-release-milvus 该configMap 添加 &#xff0c;然后重启milvus 集群可生效 user.yaml: |-common:security:authorizationEnabled: true或者直接在value.yaml 中添加该配置 extraConfigFiles:user.yaml: |com…

程序员 10 个摸鱼神器分享给大家

问&#xff1a;程序员该不该上班摸鱼&#xff1f; 答&#xff1a;认真上班是劳动换取报酬&#xff0c;上班摸鱼才是从老板那赚钱。 曝光&#xff0c;程序员的 10 个摸鱼神器 摸鱼一时爽&#xff0c;一直摸一直爽 方案一&#xff1a;实物摸鱼方案二&#xff1a;命令行斗地主方案…

抖音用户主页视频数据爬虫详解(点赞,收藏,分享等)

一. 首先进行抓包分析&#xff0c;&#xff0c;&#xff0c;随便找个主页&#xff0c;f12&#xff0c;关键词搜索&#xff0c;发现这个包是以post开头 二.查看请求参数&#xff1a; 我们复制curl在spiderbox里面快速形成请求 对headers&#xff0c;params进行尝试删减&#x…

Linux下用gdb找到cpu占用率最高的线程

我们调试程序的时候&#xff0c;有时候会发现当程序运行时&#xff0c;会出现cpu占用率很高的情况。 一般情况下&#xff0c;程序执行时&#xff0c;cpu占用率比较高的话&#xff0c;就会影响其它程序的执行&#xff0c;所以就需要对程序进行优化&#xff0c;查找程序运行时&a…

亚马逊云科技产 Amazon Neptune 图数据库服务体验

目录 图数据库为什么使用图数据库Amazon Neptune实践登陆创建 S3 存储桶notebook图神经网络快速构建加载数据配置端点Gremlin 查询删除环境删除 S3 存储桶 总结 图数据库 图数据库是一种专门用于存储和处理图形数据结构的数据库管理系统。图形数据结构由节点&#xff08;Node&…

轻松打造:基于本地知识库的私有GPT助手定制教程”

背景知识 众所周知&#xff0c;目前大模型 LLM 的能力已经非常强大&#xff0c;chatgpt 已经可以很好的解决通用型问题&#xff0c;但是对于垂直专业领域的问题处理的还不够好。如果要利用 LLM 大模型根据已有的特定领域的知识&#xff0c;推理出该领域特定问题的答案&#xf…

node.js part1

Node.js Node.js 是一个跨平台JavaScript 运行环境&#xff0c;使开发者可以搭建服务器端的 JavaScript 应用程序。作用&#xff1a;使用Node.js编写服务器端程序 编写数据接口&#xff0c;提供网页资源浏览功能等等 前端工程化&#xff1a;为后续学习Vue和React等框架做铺垫. …