Elesticesearch

news/2024/7/7 5:59:37 标签: elasticsearch, 搜索引擎, java

1. 概述

应用场景:
给你一个巨大的文档数据,文档中存储着许多非结构化的数据,如下:

{“id” : “1”, “name” : “佛山张学友”, “age” : “15”},
{“name” : “广州周润发”, “height” : “182”},
…中间有10000万行…
{“style” : “music”, “name” : “深圳薛之谦”},
{“name” : “东莞赵丽颖”}

找出name的属性值为 “深圳薛之谦” 的数据。

方案一:解析文档数据,按顺序查找第一行数据、第二行数据 … 第n-1行数据(此时找到name属性值为 “深圳薛之谦” 的数据,提取)、第n行数据。
方案二:为name属性建立索引,然后通过name索引找到属性值为 “深圳薛之谦” 的name数据列,然后提取所有相关的数据;

方案一,在少量数据占据极大的优势,因为不用维护索引表,同时查找效率也较高;
方案二,在大量数据占据特大的优势,因为可以通过索引快速找到相关的列。

而方案二的这种思想就是Elesticesearch的设计理念。

同时,Elesticesearch还引入中文分词插件。他会对搜索词进行分词,然后再进行查找(跟我们的搜索引擎类似,对搜索关键词进行分词后再查找。)


2. Elesticesearch VS MySQL

Elesticesearch做的更多的是在大数据中快速的检索出与搜索词相关的数据; MySQL更多的是存储少量数据并操作,在大量的数据并不能发挥出很大的查询性能。

例如:
在百亿数据中查找包含 “深圳薛之谦” 的数据,

  1. MySQL有强大的索引功能,但是还是需要在小时的时间成本上,才能搜索相关的数据出来;
  2. Elesticesearch可以在毫秒级别响应,同时可以根据综合评分,搜索出其他相关的数据,例如深圳薛之谦的住处、行业等数据(可以类比搜索引擎)。

3. 安装

  1. ElasticSearch
    下载地址:https://www.elastic.co/cn/

  2. ElasticSearch连接可视化界面
    下载地址:https://github.com/mobz/elasticsearch-head.git(npm install+npm run start)
    在这里插入图片描述
    连接会出现跨域问题(后端9200端口,前端9100端口),修改ElasticSearch的config-elasticsearch.yml文件,添加跨域请求

http:
  cors:
    enabled: true
    allow-origin: "*"

连接成功!
在这里插入图片描述

  1. Kibana
    下载地址:https://www.elastic.co/cn/kibana/
    在这里插入图片描述
    在这里插入图片描述
    Kibana汉化(config-kibana.yml):
    在这里插入图片描述
  2. ik分词器
    下载地址:https://github.com/medcl/elasticsearch-analysis-ik
    下载后放到ElasticSearch的插件中去
    在这里插入图片描述
    利用kibana对输入进行分词操作:
    在这里插入图片描述

所有软件都放在微信公众号上:可乐大数据
发送:ElasticSearch

3. ES的一些名词

数据库ES
数据库索引
types
documents
字段fields

物理设计:
将索引切成多个文件,然后存储在不同的ElasticSearch服务器上(DB:将数据库划分成多个表)

倒排索引:
类比搜索引擎,如果查找 “李可乐玩ElasticSearch” 应该怎么在百亿数据中对应关键句子呢?

正序索引:将输入进行分词 ==> “李可乐”、“玩”、“ElasticSearch”。然后到相关文档(document)查找包含这些词,然后返回对应的句子即可。
逆序索引:将字典统计出来,字典的key就是单词,value就是文档id,如下图。

key:李可乐
value:文档1、文档2
key:玩
value:文档1
key:ElasticSearch
value:文档3、文档4

4. 常用命令

methodurl地址描述
PUTlocalhost:9200/索引名称/类型名称/文档id创建文档(指定文档id)
POSTlocalhost:9200/索引名称/类型名称创建文档(随机文档id)
POSTlocalhost:9200/索引名称/类型名称/文档id/_update修改文档
DELETElocahost:9200/索引名称/类型名称/文档id删除文档,通过文档id
GETlocalhost:9200/索引名称/类型名称/文档id查询文档,通过文档id
POSTlocalhost:9200/索引名称/类型名称/_search查询所有数据
# 插入数据
PUT /test_db/user/1
{
  "name" : "李可乐",
  "age" : 14
}

PUT /test_db/user/2
{
  "name" : "张三"
}

PUT /test_db/user/3
{
  "name" : "李四",
  "age" : 15
}


PUT /test_db/user/4
{
  "name" : "王五"
}


PUT /test_db/user/5
{
  "name" : "赵六"
}




# 查询所有
GET /test_db/user/_search

# 查询 + 条件(模糊查询,top-k返回)
GET /test_db/user/_search?q=name:李可乐

# 多条件查询
GET /test_db/user/_search
{
  "query": {
    "match": {
      "name": "李"
    }
  },
  "sort": [
    {
      "age": {
        "order": "desc"
      }
    }
  ],
  "from": 0,
  "size": 1
}

# 查询单个
GET /test_db/user/1


# 更新
POST /test_db/user/1/_update
{
  "doc" : {
    "name" : "李可乐"
  }
}

5. SpringBoot集成ES

1. 导入maven依赖
<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>

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

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

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.83</version>
        </dependency>
        
    </dependencies>
  1. ES连接配置类
java">@Configuration
public class ElasticSearchClientConfig {

    @Bean
    public RestHighLevelClient restHighLevelClient(){
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("127.0.0.1", 9200, "http")
                )
        );
        return client;
    }
}
  1. 一些相关的CRUD
java">@SpringBootTest(classes = TestApp.class)
public class ESApp {

    @Autowired
    private RestHighLevelClient client;


    /**
     * 索引操作
     * @throws Exception
     */

    @Test
    public void createIndex() throws Exception{
        IndicesClient indicesClient = client.indices();
        CreateIndexRequest request = new CreateIndexRequest("cola_index_1");
        CreateIndexResponse createIndexResponse = indicesClient.create(request, RequestOptions.DEFAULT);
        System.out.println(createIndexResponse.toString());
    }

    @Test
    public void existIndex() throws Exception{
        GetIndexRequest getIndexRequest = new GetIndexRequest("cola_index_1");
        boolean exists = client.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
        System.out.println(exists);
    }

    @Test
    public void deleteIndex() throws Exception{
        DeleteIndexRequest request = new DeleteIndexRequest("cola_index_1");
        AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);
        System.out.println(response.isAcknowledged());
    }


    /**
     * 文档操作
     * @throws Exception
     */

    @Test
    public void addDocument() throws Exception{
        User user = new User("1", "张三");
        IndexRequest request = new IndexRequest("cola_index_1");

        // cola_index_1/_doc/1
        request.id("1");

        request.source(JSON.toJSONString(user), XContentType.JSON);

        IndexResponse response = client.index(request, RequestOptions.DEFAULT);

        System.out.println(response.toString());

    }


    @Test
    public void updateDocument() throws Exception{
        User user = new User("1", "小张三");

        UpdateRequest request = new UpdateRequest("cola_index_1", "1");

        UpdateRequest updateRequest = request.doc(JSON.toJSONString(user), XContentType.JSON);

        UpdateResponse response = client.update(updateRequest, RequestOptions.DEFAULT);

        System.out.println(response.toString());

    }


    @Test
    public void deleteDocument() throws Exception{

        DeleteRequest request = new DeleteRequest("cola_index_1", "1");

        DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);

        System.out.println(response.toString());

    }


    @Test
    public void getDocument() throws Exception{

        SearchRequest request = new SearchRequest("cola_index_1");

        SearchResponse response = client.search(request, RequestOptions.DEFAULT);

    }

}

http://www.niftyadmin.cn/n/271675.html

相关文章

松下机器人进行数据备份与恢复的具体方法

松下机器人进行数据备份与恢复的具体方法 数据备份 如下图所示,找到备份选项,按确认, 如下图所示,选择保存,按确认键, 如下图所示,选择USB存储设备,选择存储路径,按确认键, 选择需要备份的数据,如下图所示,默认情况下是勾选“全部数据”, 如下图所示,显…

JAVA IO 模型详解

什么是IO I/O&#xff08;Input/Outpu&#xff09; 即输入&#xff0f;输出 。 从计算机结构的视角来看的话&#xff0c; I/O 描述了计算机系统与外部设备之间通信的过程。 从应用程序的视角来看的话&#xff0c;我们的应用程序对操作系统的内核发起 IO 调用&#xff08;系统调…

ref在Vue2、Vue3中的使用

文章目录 前言一、ref在Vue2中的用法二、ref在Vue3中的用法 前言 记录一下ref在Vue2与Vue3中的使用&#xff0c;ref可以获取DOM元素&#xff0c;也可以获取子组件的数据、方法。 一、ref在Vue2中的用法 给元素绑定一个ref&#xff0c;然后在js中通过this.$refs获取DOM。 ref命…

Prompt炼丹炉——一系列Prompt自动优化的实践记录

原由 随着对ChatGPT的使用深入和各种杂七杂八的论文阅读&#xff0c;我得到了一个很强烈的认知&#xff1a;让普通人直接与ChatGPT对话以期得到良好结果是过分乐观到大可不必的。所以&#xff0c;一定会出现足够多的&#xff0c;以openai为基础&#xff0c;以广义prompt为核心…

Mybatis-Flex快速入门教程

目录 一、Mybatis-Flex是什么&#xff1f; 二、Mybatis-Flex的有什么特点&#xff1f; 三、Mybatis-Flex和同类框架对比 四、Mybatis-Flex支持的数据库类型 五、快速入门 &#xff08;1&#xff09;引入依赖 &#xff08;2&#xff09;创建数据库 &#xff08;3&#…

redis五大命令kv设计建议内存淘汰

什么是redis&#xff1f;主要作用&#xff1f; redis(remote dictionary server)远程字典服务&#xff1a;是一个开源的使用ANSI C语言编写&#xff0c;支持网络、可基于内存可持久化的日志型、key-value数据库&#xff0c;并提供多种语言的api redis的数据存在内存中&#xff…

【Docker】2、Docker 基本操作【镜像操作】

目录 一、镜像相关命令(1) 镜像名称格式(2) 常见镜像命令 二、从 DockerHub 拉取镜像案例三、镜像导出和导入案例四、拉取 Redis 镜像练习 一、镜像相关命令 (1) 镜像名称格式 &#x1f50b; 镜像名称一般由两部分组成&#xff1a;[repository]:[tag] &#x1f50b; 若没有指…

《编程思维与实践》1040.字符串消除

《编程思维与实践》1040.字符串消除 题目 思路 每次消除都可能会受到第一次插入字符的影响,所以难以直接判断在哪个位置插入哪个字符后消除的字符数最多. 因此考虑暴力枚举: 在每个位置依此插入A,B,C 对所有情况消除的字符数进行比较,求出最大值. 对于字符串的插入可以利用str…