当前位置:   article > 正文

搜索框架ElasticSearch讲解(一)

搜索框架

本博客地址:https://security.blog.csdn.net/article/details/110343231

一、搜索框架Elasticsearch介绍

Elasticsearch是Elastic Stack核心的分布式搜索和分析引擎。它能为所有类型的数据提供实时搜索和分析。无论是结构化文本还是非结构化文本,Elasticsearch都能支持快速搜索的方式有效地对其进行存储和索引。它不仅可以进行简单的数据检索,还可以聚合信息来发现数据中的趋势和模式。

Elasticsearch是一个分布式文档存储,随着数据和查询量的增长,它的分布式特性使得部署可以随之无缝地增长。它不会将信息存储为列数据的行,而是存储已序列化为JSON文档的复杂数据结构。当集群中有多个Elasticsearch节点时,存储的文档将分布在集群中,并且可以从任何节点立即访问。

存储文档时,将在1秒钟内几乎实时地对其进行索引和完全搜索。Elasticsearch使用称为倒排索引的数据结构,该结构支持非常快速的全文本搜索。反向索引列出了出现在任何文档中的每个唯一单词,并标识了每个单词出现的所有文档。

二、部署安装(MacOS)

2.1、安装elasticsearch

1、执行安装命令

# 更新brew
brew update

# 搜索elasticsearch
brew search elasticsearch

# 安装elasticsearch
brew install elasticsearch

# 运行elasticsearch
brew services start elasticsearch
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

2、效果如下所示

命令行效果:
在这里插入图片描述

访问链接:http://127.0.0.1:9200

在这里插入图片描述

2.2、安装Kibana

Kibana是ES的一个配套工具,可以让用户在网页中与ES进行交互

1、执行安装命令

# 安装kibana
brew install kibana

# 启动Kibana
brew services start kibana
  • 1
  • 2
  • 3
  • 4
  • 5

2、效果如下所示

# 本地浏览器访问
http://localhost:5601

# 查看集群健康状态
http://localhost:9200/_cat/health?v

# 查看索引列表
http://localhost:9200/_cat/indices?v
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

如下所示:
在这里插入图片描述

注意看 status字段,他有三个值含义分别如下:

green:一切都很好(集群功能齐全)
yellow:所有数据都可用,但尚未分配一些副本(群集功能齐全)
red:某些数据由于某种原因不可用(群集部分功能)

访问首页的效果为:
在这里插入图片描述

三、基本概念

1、索引(index):一个索引可以理解成一个关系数据库的库

2、类型(type):一种type就像一类表,比如user表、order表

3、映射(mapping):mapping定义了每个字段的类型等信息,相当于关系型数据库中的表结

4、文档(document):一个document相当于关系型数据库中的一⾏行记录

5、字段(field):相当于关系型数据库表的字段

6、集群(cluster):集群由一个或多个节点组成,一个集群有一个默认名称"elasticsearch"

7、节点(node):集群的节点,一台机器或者一个进程

8、分片和副本(node):副本是分片的副本。分⽚有主分片(primary Shard)和副本分片(replica Shard)之分。Index数据在物理上被分布在多个主分片中,每个主分片只存放部分数据。每个主分⽚可以有多个副本,叫副本分片,是主分片的复制。

9、核心数据类型

9.1、字符串(text):用于全文索引,该类型的字段将通过分词器进⾏分词。
字符串(keyword):不分词,只能搜索该字段的完整的值

9.2、数值型:long、integer、short、byte、double、float、half_float、scaled_float

9.3、数值型(boolean):boolean

9.4、二进制(binary):该类型的字段把值当做经过 base64 编码的字符串,默认不存储,且不可搜索

9.5、范围类型:范围类型表示值是一个范围,而不是⼀个具体的值。类型有:integer_range、float_range、long_range、double_range、date_range

9.6、日期(date):由于Json没有date类型,所以es通过识别字符串是否符合format定义的格式来判断是否为date类型。format默认为:strict_date_optional_time||epoch_millis,如:2022-01-01"、“2022/01/01 12:10:30” 这种字符串格式

四、索引基本操作

4.1、创建索引PUT请求

请求

http://localhost:9200/blablabla
  • 1

响应

在这里插入图片描述

4.2、查看索引GET请求

请求

http://localhost:9200/blablabla
  • 1

响应

在这里插入图片描述

4.3、批量获取索引GET请求

请求

http://localhost:9200/blablabla,malamala
  • 1

响应

在这里插入图片描述

4.4、获取全部索引GET请求

请求

http://localhost:9200/_all
  • 1

响应(太长了,不放json格式数据了)

在这里插入图片描述

4.5、使用_cat获取全部索引GET请求

请求

http://localhost:9200/_cat/indices?v
  • 1

响应

在这里插入图片描述

4.6、关闭索引不删除POST请求

请求

http://localhost:9200/blablabla/_close
  • 1

响应

在这里插入图片描述

4.7、打开索引POST请求

请求

http://localhost:9200/blablabla/_open
  • 1

响应

在这里插入图片描述

4.8、删除索引DELETE请求

请求

http://localhost:9200/blablabla
  • 1

响应

在这里插入图片描述

五、映射的介绍与使用

type:text 可分词
type:keyword 不可分词

5.1、创建Mapping PUT请求

请求

http://localhost:9200/blablabla/_mapping
  • 1

请求体

{
	"properties":{	//字段的信息
		"name":{
			"type":"text"
		},
		"team_name":{
			"type":"text"
		},
		"position":{
			"type":"keyword"
		},
		"play_year":{
			"type":"keyword"
		},
		"jerse_no":{
			"type":"keyword"
		}
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

响应

{
    "acknowledged": true
}
  • 1
  • 2
  • 3

5.2、查看Mapping信息GET请求

请求

http://localhost:9200/blablabla/_mapping
  • 1

响应

{
    "blablabla": {
        "mappings": {
            "properties": {
                "jerse_no": {
                    "type": "keyword"
                },
                "name": {
                    "type": "text"
                },
                "play_year": {
                    "type": "keyword"
                },
                "position": {
                    "type": "keyword"
                },
                "team_name": {
                    "type": "text"
                }
            }
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

5.3、批量获取Mapping信息GET请求

请求

http://localhost:9200/blablabla,malamala/_mapping
  • 1

响应

{
    "blablabla": {
        "mappings": {
            "properties": {
                "jerse_no": {
                    "type": "keyword"
                },
                "name": {
                    "type": "text"
                },
                "play_year": {
                    "type": "keyword"
                },
                "position": {
                    "type": "keyword"
                },
                "team_name": {
                    "type": "text"
                }
            }
        }
    },
    "malamala": {
        "mappings": {}
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

5.4、获取所有Mapping信息第一种方式GET请求

请求

http://localhost:9200/_mapping
  • 1

响应

{
    "blablabla": {
        "mappings": {
            "properties": {
                "jerse_no": {
                    "type": "keyword"
                },
                "name": {
                    "type": "text"
                },
                "play_year": {
                    "type": "keyword"
                },
                "position": {
                    "type": "keyword"
                },
                "team_name": {
                    "type": "text"
                }
            }
        }
    },
    "malamala": {
        "mappings": {}
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

5.5、获取所有Mapping信息第二种方式GET请求

请求

http://localhost:9200/_all/_mapping
  • 1

响应

{
    "blablabla": {
        "mappings": {
            "properties": {
                "jerse_no": {
                    "type": "keyword"
                },
                "name": {
                    "type": "text"
                },
                "play_year": {
                    "type": "keyword"
                },
                "position": {
                    "type": "keyword"
                },
                "team_name": {
                    "type": "text"
                }
            }
        }
    },
    "malamala": {
        "mappings": {}
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

5.6、增加Mapping字段POST请求

Mapping 只可增加字段不可修改字段

请求

http://localhost:9200/blablabla/_mapping
  • 1

请求体

{
	"properties":{	
		"name":{
			"type":"text"
		},
		"team_name":{
			"type":"text"
		},
		"position":{
			"type":"keyword"
		},
		"play_year":{
			"type":"keyword"
		},
		"jerse_no":{
			"type":"keyword"
		},
		"country":{  // 增加的国家字段
			"type":"keyword"
		}
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

响应

{
    "acknowledged": true
}
  • 1
  • 2
  • 3
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/862312
推荐阅读
相关标签
  

闽ICP备14008679号