1. 基本概念
1.1 文档
- ElasticSearch是面向文档的,文档是所有可搜索数据的最小单位
- 文档会被序列化成JSON格式,保存在ES中
- 每个文档都有一个unique ID
#查看前10条文档,了解文档格式
POST kibana_sample_data_ecommerce/_search
{
}
1.1.1 文档元数据
{
"_index" : "kibana_sample_data_ecommerce", // 文档所属索引名
"_type" : "_doc", // 文档所属的类型
"_id" : "LY5Jc4IBJLxforJ4rVB5", // 文档唯一ID
"_score" : 1.0, // 相关性打分
"_source" : { // 文档的原始json数据
....
}
}
1.2 索引
索引是文档的容器,是一类文档的集合
#查看索引相关信息
GET kibana_sample_data_ecommerce
{
"kibana_sample_data_ecommerce" : {
"aliases" : { },
// mapping定义文档的字段信息
"mappings" : {
"properties" : {
"category" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword"
}
}
},
// settings索引配置信息
"settings" : {
"index" : {
"routing" : {
"allocation" : {
"include" : {
"_tier_preference" : "data_content"
}
}
},
"number_of_shards" : "1", // 分片数为1
"auto_expand_replicas" : "0-1",
"provided_name" : "kibana_sample_data_ecommerce",
"creation_date" : "1659791584389",
"number_of_replicas" : "0", // 副本数0
"uuid" : "iH1-4-krQA6tda3W8Psmbw",
"version" : {
"created" : "7110299"
}
}
}
}
}
}
以下为基本操作
// 查看索引的文档总数
GET kibana_sample_data_ecommerce/_count
// 查看indices
GET _cat/indices/kibana*?format=json
// 查看状态为绿的索引
GET _cat/indices?health=green&format=json
// 按照文档个数排序
GET /_cat/indices?s=docs.count:desc&format=json
// 查看具体的字段
GET /_cat/indices/kibana*?h=health,index,pri,rep,docs.count,mt&format=json
// 索引占用内存查看
GET /_cat/indices?v&h=i,tm&s=tm:desc
1.3节点
- 节点是一个ElasticSearch的实例
- 每个节点都有一个名字,通过配置文件配置或启动时-E node.name=node1指定
- 每个节点启动后,会分配一个UID,保存在data目录下
1.3.1 Master-eligible Nodes和Master Nodes
- 每个节点启动后,默认就是一个Master-eligible节点,可以设置node.master=false禁止
- Master-eligible节点可以参加主节点选举称为Master节点
- 每个节点保存了集群的状态,只有Master节点可以修改集群状态:
- 所有的节点信息
- 所有的索引及其相关的Mapping和Setting信息
- 分片的路由信息
1.3.2 Data Node和Coordinating Node
- Data Node: 可以保存数据的节点,负责保存分片数据
- Coordinating Node:负责接收client的请求,将请求分发到合适的节点,将结果进汇集,每个节点默认起到协调节点作用
1.4 分片
- 主分片,用于解决数据水平扩展的问题。通过主分片,可以将数据分布到集群内的所有节点上
- 一个分片是一个运行的Lucene的实例
- 主分片数在创建索引时指定,后续不允许修改,除非reindex
- 副本,用于解决数据的高可用问题。副本分片是主分片的拷贝
- 副本分片数,可以动态调整
- 增加副本数,可以一定程度提高服务的可用性(读取的吞吐)
ES7.0开始,默认主分片设置为1,解决了over-sharding问题:
- 影响搜索结果的相关性打分,影响统计结果的准确性
- 单个节点分配过多分片,会导致资源浪费,同时也会影响性能
1.5 集群相关查询示例
假设你启动了集群
# 查看集群节点信息
GET _cat/nodes?v
# 查看集群节点详细信息
GET /_nodes/es-node1,es-node2
# 查看节点指定字段信息
GET /_cat/nodes?v&h=id,ip,port,v,m
# 查看集群健康状态
GET _cluster/health
GET _cluster/health?level=shards
GET /_cluster/health/kibana_sample_data_ecommerce,kibana_sample_data_flights
GET /_cluster/health/kibana_sample_data_flights?level=shards
# 查看集群元数据等信息
GET /_cluster/state
#cluster get settings
GET /_cluster/settings
GET /_cluster/settings?include_defaults=true
# 查看分片情况
GET _cat/shards?v
GET _cat/shards?v&h=index,shard,prirep,state,unassigned.reason
2. 基本操作
2.1 文档写入
# create document. 自动生成 _id
POST users/_doc
{
"user" : "shenjian",
"post_date" : "2022-08-07T14:12:12",
"message" : "trying out Kibana"
}
# 创建指定ID文档,存在则替换
POST users/_doc/1
{
"user" : "Domi",
"post_date" : "2022-08-07T14:12:12",
"message" : "trying out Elasticsearch"
}
2.2 文档查询
# 查询id为1的文档
GET users/_doc/1
# 查询字段order_id值为584677的文档
GET kibana_sample_data_ecommerce/_search?q=order_id:584677
# 查询所有文档
GET kibana_sample_data_ecommerce/_search
{
"query": {
"match_all": {}
}
}
# _source指定返回字段 sort排序 form size分页 term查询指定字段
POST kibana_sample_data_ecommerce/_search
{
"_source": ["currency", "order_date"],
"sort": [
{
"order_date": {
"order": "desc"
}
}
],
"from": 0,
"size": 2,
"query": {
"bool": {
"must": [
{
"term": {
"currency": {
"value": "EUR"
}
}
}
]
}
}
}
# 嵌套对象查询
GET community/_search
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "address_component",
"query": {
"bool": {
"must": [
{
"match": {
"address_component.city": "佛山"
}
}
]
}
}
}
}
]
}
}
}
2.3 文档更新
# 在原文档上增加字段
POST users/_update/1/
{
"doc":{
"post_date" : "2019-05-15T14:12:12",
"message" : "trying out Elasticsearch"
}
}
# 批量查询后更新
POST kibana_sample_data_ecommerce/_update_by_query
{
"script": "ctx._source.customer_first_name='domi'",
"query": {
"bool": {
"must": [
{
"term": {
"customer_first_name.keyword": {
"value": "Eddie"
}
}
}
]
}
}
}
2.4 文档删除
# 删除文档
DELETE users/_doc/1
2.5 批量操作[不常用,了解,后续通过python或java客户端批量操作]
# 批量操作
POST _bulk
{ "index" : { "_index" : "test", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_id" : "2" } }
{ "create" : { "_index" : "test2", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }
# 批量查询
GET /_mget
{
"docs" : [
{"_index" : "test","_id" : "1"},
{"_index" : "test","_id" : "2"}
]
}
# URI中指定index
GET /test/_mget
{
"docs" : [
{"_id" : "1"},{"_id" : "2"}
]
}
# 查询指定索引指定字段
GET /_mget
{
"docs" : [
{
"_index" : "test",
"_id" : "1",
"_source" : false
},
{
"_index" : "test",
"_id" : "2",
"_source" : ["field3", "field4"]
},
{
"_index" : "test",
"_id" : "3",
"_source" : {
"include": ["user"],
"exclude": ["user.location"]
}
}
]
}
### msearch 操作
POST kibana_sample_data_ecommerce/_msearch
{}
{"query" : {"match_all" : {}},"size":1}
{"index" : "kibana_sample_data_flights"}
{"query" : {"match_all" : {}},"size":2}
# 删除索引,清除数据
DELETE users
DELETE test
DELETE test2
3. 倒排索引
3.1 倒排索引简介
对于书通过目录查找对应章节内容的方式属于正排索引,而对于想查询文本,如我爱中国在书籍中出现的次数与具体位置,则是倒排索引的范畴。
3.2 倒排索引核心组成
-
单词词典(Term Dictionary)
记录所有文档的单词,记录单词到倒排列表的关联关系。单词词典一般比较大,可以通过B+树或哈希拉链法实现,以满足高性能的插入和查询 -
倒排列表(Posting List)
记录了单词对应的文档集合,由倒排索引项组成:- 文档ID
- 词频TF - 该单词在文档中出现的次数,用于相关性评分
- 位置(Position) - 单词在文档中分词的位置。用于语句搜索
- 偏移(Offset) - 记录单词的开始结束位置,用于高亮显示
ElasticSearch的JSON文档中的每个字段,都有自己的倒排索引。可以指定对某些字段不做索引,这样可以节省存储空间,但该字段无法被搜索
欢迎关注公众号算法小生或沈健的技术博客shenjian.online
本文档介绍了Elasticsearch的基本概念,包括文档、索引、节点、分片和集群管理。详细阐述了文档的写入、查询、更新和删除操作,以及倒排索引的工作原理。通过对Elasticsearch的操作示例,帮助读者快速理解其核心功能。
1984

被折叠的 条评论
为什么被折叠?



