欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ElasticSearch 实用学习笔记 (从入门到精通)

程序员文章站 2022-06-30 22:58:47
...

ElasticSearch

学习笔记记录自狂神说 Java,传送门

一、学习大纲

  1. 安装
  2. 生态圈
  3. 分词器 lk
  4. RestFul 操作 ES
  5. CRUD
  6. SpringBoot 继承 ElasticSearch (从原理分析!!!)
  7. 爬虫爬取数据!!! 京东
  8. 实战,模拟全文检索

搜索相关使用 ES(大数据量下使用)

Lucene 是一套信息检索工具包 (Jar 包,不包含 搜索引擎系统)! Solr

包含的:索引结构!读写索引的工具!排序,搜索规则… 工具类

Lucene 和 EslasticSearch 关系:

ElasticSearch 是基于 Lucene 做了一些封装 和 增强

二、ElasticSearch 概述

简称 es

  • 一个开源的高扩展分布式全文检索引擎
  • 近乎实时的存储,检索数据
  • es使用 java 开发并使用 Lucene 作为其核心来实现所有索引 和 搜索功能
  • 它的目的是通过简单的 RESTFul API,来隐藏 Lucene 的复杂性,从而让全文搜索变得简单

三、ElasticSearch 安装

  • JDK 1.8 先决条件
  1. 下载,解压 华为云镜像下载地址

  2. 熟悉目录:

bin: 启动文件
	config: 配置文件
	log4j: 日志文件
	jvm.options: java 虚拟机先关的配置
	elasticsearch.xml:	elasticsearch 的配置文件!
lib: 相关 jar 包
logs: 日志
modules: 功能模块
plugins: 插件 ik	
	
  1. 启动,访问 9200
  2. 访问测试:localhost:9200

ElasticSearch 实用学习笔记 (从入门到精通)

ElasticSearch 实用学习笔记 (从入门到精通)

安装可视化插件 es head 插件

  1. 下载地址:https://github.com/mobz/elasticsearch-head/
  2. 启动
npm install
npm run start

在 elasticSearch.yml 配置跨域

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

安装 kibana

  1. 下载,解压
  2. 国际化

找到 config 下的 kibana.yml 文件,修改最后一行为 i18n.locale: “zh-CN”

启动
ElasticSearch 实用学习笔记 (从入门到精通)

ElasticSearch 实用学习笔记 (从入门到精通)

四、ES 核心概念

  1. 索引
  2. 字段类型 (mapping)
  3. 文档(documents)

集群、节点、索引、类型、文档、分片、映射是什么?

ElasticSearch 是面向文档,关系型数据库 和 elasticSearch 客观的对比! 一切都是 JSON

{

}

名词对应

ElasticSearch Relational DB
索引(indices) 数据库(database)
types 表(tables)
documents 行(rows)
fields 字段(columns)

elasticSearch (集群)中可以包含多个索引(数据库),每个索引中可以包含多个类型(表),每个类型下又包含多个文档(行),每个文档又包含多个字段(列)

物理设计

elasticSearch 一个就是一个集群

文档

一条条记录

user
	zs: 15
	ls: 22

类型

自动识别, string,

索引

数据库

五、IK 分词器插件

下载好的添加到 plugin 中

跳过,第 8 集

  • elasticsearch-plugin 可以通过这个命令来查看加载进来的插件

  • ik_smart(最少切分) 和 ik_max_word(最细粒度划分)

  • kibana 测试

  • 自定义分词

六、 Rest 风格说明

基础 Rest 命令

method url 地址 描述
PUT localhost:9200/索引名称/类型名称/文档 id 创建文档(指定文档 id)
POST localhost:9200/索引名称/类型名称 创建文档(随机文档 id)
POST localhost:9200/索引名称/类型名称/文档id/_update 修改文档
DELETE localhost:9200/索引名称/类型名称/文档id 删除文档
GET localhost:9200/索引名称/类型名称/文档id 查询文档通过文档 id
POST localhost:9200/索引名称/类型名称/_seaarch 查询所有数据

基本测试

6.1 创建索引

  1. 创建一个索引
PUT /索引名/~类型名~/文档id
{
  "name":"Gorit",
  "age": 18,
  "gender": "male"
}

返回值,数据成功添加

#! Deprecation: [types removal] Specifying types in document index requests is deprecated, use the typeless endpoints instead (/{index}/_doc/{id}, /{index}/_doc, or /{index}/_create/{id}).
{
  "_index" : "test",
  "_type" : "type1",
  "_id" : "1", 
  "_version" : 1, // 修改次数
  "result" : "created", // 状态
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

  1. 创建索引规则
PUT /test1/
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      },
      "age": {
        "type": "long"
      },
      "birthday": {
        "type": "date"
      }
    }
  }
}

返回值

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "test1"
}

es 默认配置字段类型!

6.2 查询

GET test

# 结果
{
  "test" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "age" : {
          "type" : "long"
        },
        "gender" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1603203146037",
        "number_of_shards" : "1",
        "number_of_replicas" : "1",
        "uuid" : "q47lWt_4ToOBo1rxQ1pPNw",
        "version" : {
          "created" : "7060299"
        },
        "provided_name" : "test"
      }
    }
  }
}


GET test1

{
  "test1" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "age" : {
          "type" : "long"
        },
        "birthday" : {
          "type" : "date"
        },
        "name" : {
          "type" : "text"
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1603203453667",
        "number_of_shards" : "1",
        "number_of_replicas" : "1",
        "uuid" : "a-upVXJwR7u7JZztTjyVGg",
        "version" : {
          "created" : "7060299"
        },
        "provided_name" : "test1"
      }
    }
  }
}

扩展:通过 _cat/ 可以获得 es 当前很多的信息

# 打印健康状态
GET _cat/health

GET _cat/indices?v

6.3 修改索引

提交 PUT,覆盖即可

修改数据

PUT /test/type1/1
{
  "name":"Gorit111",
  "age": 18,
  "gender": "male"
}

修改结果

#! Deprecation: [types removal] Specifying types in document index requests is deprecated, use the typeless endpoints instead (/{index}/_doc/{id}, /{index}/_doc, or /{index}/_create/{id}).
{
  "_index" : "test",
  "_type" : "type1",
  "_id" : "1",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}

新的方法 POST 命令更新

POST /test/_doc/1/_update
{
  "doc": {
      "name":"张三"
  }
}

// 结果
{
  "_index" : "test",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 3,
  "_seq_no" : 2,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "张三",
    "age" : 18,
    "gender" : "male"
  }
}

6.4 删除索引

删除索引!!!

DELETE test

通过 delete 命令实现删除,根据你的请求来判断删除的是索引 还是 文档

七、关于文档的操作

基本操作

  1. 添加数据(添加多条记录)
PUT /gorit/user/1
{
  "name": "CodingGorit",
  "age": 23,
  "desc": "一个独立的个人开发者",
  "tags": ["Python","Java","JavaScript"]
}

PUT /gorit/user/2
{
  "name": "龙",
  "age": 20,
  "desc": "全栈工程师",
  "tags": ["Python","JavaScript"]
}

结果:

#! Deprecation: [types removal] Specifying types in document index requests is deprecated, use the typeless endpoints instead (/{index}/_doc/{id}, /{index}/_doc, or /{index}/_create/{id}).
{
  "_index" : "gorit",
  "_type" : "user",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}
  1. 获取数据
GET /gorit/user/_search   # 查询所有数据

GET /gorit/user/1 # 查询单个数据
  1. 更新数据 PUT
PUT /gorit/user/3
{
  "name": "李四222",
  "age": 20,
  "desc": "Java开发工程师",
  "tags": ["Python","Java"]
}

# PUT 更新字段不完整,数据会被滞空
  1. post _update , 推荐使用这种方式!
# 修改方式和 PUT 一样会使数据滞空
POST /gorit/user/1
{
  "doc": {
    "name": "coco"
  }
}

# 修改数据不会滞空, 效率更加高效
POST /gorit/user/1/_update
{
  "doc": {
    "name": "coco"
  }
}

简单的搜索!

# 查询一条记录
GET /gorit/user/1

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

# 条件查询 [精确匹配] ,如果我们没有个这个属性设置字段,它会背默认设置为 keyword,这个 keyword 字段就是使用全匹配来匹配的,如果是 text 类型,模糊查询就会起效果
GET /gorit/user/_search?q=name:coco

复杂的查询搜索:select(排序、分页、高亮、模糊查询、精确查询)!

https://www.bilibili.com/video/BV17a4y1x7zq?p=11

八、集成 SpringBoot

九、实战

爬虫

前后端分离

搜索高亮