springboot30springboot整合全文搜索引擎elasticsearch(代码片段)

哪吒 哪吒     2023-02-26     346

关键词:

🍅 Java学习路线配套文章:Java学习路线总结,搬砖工逆袭Java架构师(全网最强)
🍅 基础推荐:Java基础教程系列
🍅 实战推荐:Spring Boot基础教程
🍅 简介:Java领域优质创作者🏆、CSDN哪吒公众号作者✌ 、Java架构师奋斗者💪
🍅 扫描主页左侧二维码,加入群聊,一起学习、一起进步
🍅 欢迎点赞 👍 收藏 ⭐留言 📝
🍅 文末送书

一、Elasticsearch简介

Elasticsearch是一个分布式、RESTful风格的搜索和数据分析引擎,能够解决不断涌现出的各种用例。作为Elastic Stack的核心,它集中存储您的数据,帮助您发现意料之中以及意料之外的情况。

The Elastic Stack包括Elasticsearch、Kibana、Beats和Logstash,也称为ELK Stack。

能够安全可靠地获取任何来源、任何格式的数据,然后实时地对数据进行搜索、分析和可视化。

Elasticsearch简称ES,ES是一个开源的高扩展的分布式全文搜索引擎,是整个Elastic Stack技术栈的核心,它可以近乎实时的存储、检索数据;本身扩展性很好,可以扩展到上百台服务器,处理PB级别的数据。

PB是数据存储容量的单位,它等于2的50次方个字节,或者在数值上大约等于1000TB。

二、下载与安装

1、Elasticsearch官网下载地址

https://www.elastic.co/cn/downloads/elasticsearch

2、下载成功

3、双击elasticsearch.bat启动

4、启动成功

三、数据格式

Elasticsearch是面向文档型数据库,一条数据在这里就是一个文档。为了方便大家理解,我们将Elasticsearch里存储文档数据和关系型数据库Mysql存储数据的概念进行一个类比:

ES里的Index可以看做一个库,而Types相当于表,Documents则相当于表的行。

Elasticsearch7.X中,Type的概念已经被删除了。

四、索引

1、创建索引

在postman中,向ES服务器发送PUT请求:127.0.0.1:9200/work

由于PUT请求具有幂等性,每次PUT请求创建的结果都是一样的,再次请求时,由于ES中已经存在名为work的索引了,所以会创建失败。

POST是不具有幂等性的,所以POST请求后,结果可能不一样,所以添加索引的时候是不允许使用POST请求的。

什么是幂等性?
在编程中一个幂等操作的特点是其任意多次执行所产生的影响均与一次执行的影响相同。幂等函数,或幂等方法,是指可以使用相同参数重复执行,并能获得相同结果的函数。这些函数不会影响系统状态,也不用担心重复执行会对系统造成改变。

2、查询索引

(1)通过GET请求可以获取单一索引

(2)获取全部索引信息

127.0.0.1:9200/_cat/indices?v

3、删除索引

五、文档

1、创建文档

ES中的文档相当于MySQL中的表数据,数据格式为JSON格式。

由于文档生成时会自动创建一个唯一性标识,因为POST不是幂等性的,PUT是幂等性的,所以这里只能用POST。

可以指定id

2、查询文档

(1)根据id查询


(2)查询所有文档

127.0.0.1:9200/work/_search

3、更改文档内容
(1)修改文档id=1001的内容,恭喜哪吒大佬被评选为“2021博客之星TOP10”,锣鼓喧天,鞭炮旗鼓。

(2)局部更新


(3)局部更新成功,恭喜哪吒成功晋升TOP5。

六、复杂查询

1、指定条件查询

(1)查询name为哪吒的索引(通过请求路径:127.0.0.1:9200/work/_search?q=name:哪吒)

注意:满篇全是截图也不好看,以下就不截图了,望谅解。

(2)请求体查询

get请求:127.0.0.1:9200/work/_search

请求体:


   "query":
       "match":
          "name":"哪吒"
       
    

(3)分页查询

get请求:127.0.0.1:9200/work/_search

请求体:


   "query":
       "match_all":
          
       
    ,
   "from":0,
   "size":2

(4)只获取指定字段 and 根据id排序


   "query":
       "match_all":
         
       
    ,
   "from":0,
   "size":2,
   "_source":["title"],
   "sort":
       "_id":"desc"
   

2、多条件查询

must表示and匹配


   "query":
       "bool":
         "must":[
             
                 "match":
                     "name":"哪吒"
                 
             ,
                 "match":
                     "title":"博客专家"
                 
             
         ]
       
    

should表示or匹配


   "query":
       "bool":
         "should":[
             
                 "match":
                     "name":"哪吒"
                 
             ,
                 "match":
                     "name":"CSDN"
                 
             
         ]
       
    

范围匹配:工资大于10000


   "query":
       "bool":
         "should":[
             
                 "match":
                     "name":"哪吒"
                 
             ,
                 "match":
                     "name":"CSDN"
                 
             
         ],
         "filter":
             "range":
                 "money":10000
             
         
       
    

3、部分词汇匹配查询

将每一个值拆解,组成倒排索引,方便进行全文检索。


   "query":
       "match":
         "name":"哪"
       
    

完全匹配


   "query":
       "match_phrase":
         "name":"哪"
       
    

高亮显示


   "query":
       "match":
         "name":"哪"
       
    ,
    "highlight":
        "fields":
            "name":
        
    

4、聚合查询

(1)分组查询


   "aggs":
       "money_group":
         "terms":
             "field":"money"
         
       
    ,
    "size":0

(2)平均值查询


   "aggs":
       "money_avg":
         "avg":
             "field":"money"
         
       
    ,
    "size":0

七、代码实例

1、引入pom

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.guor</groupId>
    <artifactId>es</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>es</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
 
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>7.8.0</version>
        </dependency>
        <!-- elasticsearch的客户端 -->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.8.0</version>
            <exclusions>
                <exclusion>
                    <groupId>org.elasticsearch.client</groupId>
                    <artifactId>elasticsearch-rest-client</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-client</artifactId>
            <version>7.8.0</version>
        </dependency>
        <!-- elasticsearch依赖2.x的log4j -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.8.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.8.2</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.9</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- junit单元测试 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
 
</project>

2、添加索引

package com.guor.es.test;
 
import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
 
public class ESTest 
    public static void main(String[] args) throws Exception
        //创建es客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
 
        //创建索引
        CreateIndexRequest request = new CreateIndexRequest("student");
 
        CreateIndexResponse createIndexResponse = esClient.indices().create(request, RequestOptions.DEFAULT);
 
        boolean acknowledged = createIndexResponse.isAcknowledged();
        System.out.println("创建索引:"+acknowledged);
        // 关闭es客户端
        esClient.close();
    

3、运行出错


解决方法:

将pom中

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.8.0</version>
</dependency>

替换为:

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

4、查询索引

package com.guor.es.test;
 
import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
 
public class ESTest 
    public static void main(String[] args) throws Exception
        //创建es客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new 查看详情  

springboot整合mybatis快速入门

参考技术A注意:1.mybatis中的mapper-locations是mapper的xml文件位置2.mybatis中的type-aliases-package是为了配置xml文件中resultType返回值的包位置,如果未配置请使用全包名如下: 查看详情

springboot整合websocket

...t:在浏览器和服务器之间建立tcp连接,实现全双工通信??springboot使用websocket有两种方式,一种是实现简单的websocket,另外一种是实现STOMP协议。这一篇实现简单的webso 查看详情

企业级springboot教程springboot整合beatlsql

BeetSql是一个全功能DAO工具,同时具有Hibernate优点&Mybatis优点功能,适用于承认以SQL为中心,同时又需求工具能自动能生成大量常用的SQL的应用。beatlsql优点开发效率无需注解,自动使用大量内置SQL,轻易完成增删改查功能,节... 查看详情

企业级springboot教程springboot整合beatlsql

BeetSql是一个全功能DAO工具,同时具有Hibernate优点&Mybatis优点功能,适用于承认以SQL为中心,同时又需求工具能自动能生成大量常用的SQL的应用。beatlsql优点开发效率无需注解,自动使用大量内置SQL,轻易完成增删改查功能,节... 查看详情

springboot整合dubbo+zookeeper

dockerpullzookeeperdockerrun--namezk01-p2181:2181--restartalways-d2e30cac00aca 表明zookeeper已成功启动 Zookeeper和Dubbo•ZooKeeperZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务。它是一个为分布式应用提供一致性服务的软件,... 查看详情

springboot整合阿里druid数据源

全配置在application配置文件(方式1)maven依赖<!--druid的启动器--><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.1.17< 查看详情

springboot-thymeleaf模板引擎整合及基本用法总结(代码片段)

...下前四篇,你的赞就是对我最大的支持,感谢大家!(一)SpringBoot起飞之路-HelloWorld(二)SpringBoot起飞之路-入门原理分析(三)SpringBoot起飞之路-YAML配置小结(入门必知必会)(四)SpringBoot起飞之路-静态资源处理说明:太忙啦,同时全放... 查看详情

求一套springboot视频教程

最好百度云分享~非常感谢!SpringBoot2.1.6视频教程-加密百度网盘免费资源在线学习  链接:https://pan.baidu.com/s/1gIJ9MsAMoOBv5k2YAX3-kQ提取码:b3di   SpringBoot2.1.6视频教程-加密第9章SpringBoot缓存第8章开发者工具与单元测试... 查看详情

springboot30springboot整合全文搜索引擎elasticsearch(代码片段)

...345;基础推荐:Java基础教程系列🍅实战推荐:SpringBoot基础教程🍅简介:Java领域优质创作者🏆、CSDN哪吒公众号作者✌、Java架构师奋斗者💪🍅扫描主页左侧二维码,加入群聊,一起学习、... 查看详情

[springboot系列]springboot如何整合ssmp

文章目录基于SpringBoot实现SSMP整合基于SpringBoot实现SSMP整合SpringBoot之所以好用,就是它能方便快捷的整合其他技术,这里我们先介绍四种技术的整合:整合JUnit整合MyBatis整合MyBatis-Plus整合Druid整合JUnitSpringBoot技术的定位用于简化... 查看详情

springboot1.5.9整合websocket

一.WebSocket介绍  1.WebSocket是什么?    WebSocket是协议,是HTML5开始提供的基于TCP(传输层)的一种新的网络协议,    它实现了浏览器与服务器全双工(full-duplex)通信,即允许服务器主动发送消息给客户端    WebSocket使得客... 查看详情

springboot入门基础:介绍

一.SpringBoot初级(一)SpringBoot入门SpringBoot简介构件SpringBoot项目以及启动器讲解SpringBoot入门HelloWorld(二)SpringBoot整合Web开发整合Servlet整合Filter整合Listener访问静态资源文件上传(三)SpringBoot视图层技术整合jsp技术整合freemarker... 查看详情

springboot完成ssm整合之springboot整合junit(代码片段)

SpringBoot🍌掌握基于SpringBoot框架的程序开发步骤🍌使用SpringBoot配置信息修改服务器配置今日目标:基于SpringBoot的完成SSM整合项目开发第一步一、SpringBoot整合junit回顾Spring整合junit@RunWith(SpringJUnit4ClassRunner.class)@C... 查看详情

springboot:springboot整合mybatis案例

文章目录SpringBoot整合Mybatis案例一、导入依赖二、编写配置文件三、编写功能代码 查看详情

springboot整合其他框架--springboot整合mybatis(代码片段)

1.SpringBoot整合Mybatis需求:SpringBoot整合MyBatis。实现步骤:搭建SpringBoot工程引入mybatis起步依赖,添加mysql驱动编写DataSource和MyBatis相关配置定义表和实体类编写dao和mapper文件/纯注解开发测试1.0公共步骤1.0.1搭建SpringBoot... 查看详情

springboot整合其他框架--springboot整合junit(代码片段)

1.SpringBoot整合Junit1.1实现步骤分析搭建SpringBoot工程引入starter-test起步依赖和junit依赖编写测试类添加测试相关注解@RunWith(SpringRunner.class)@SpringBootTest(classes=启动类.class)编写测试方法1.2搭建SpringBoot工程1.3导入相关依赖pom.xml... 查看详情

springboot整合其他框架--springboot整合redis(代码片段)

1.SpringBoot整合Redis搭建SpringBoot工程引入redis起步依赖配置redis相关属性注入RedisTemplate模板编写测试方法,测试1.1搭建SpringBoot工程1.2引入redis起步依赖<dependency><groupId>org.springframework.boot</groupId><arti 查看详情