阅读elastic search index module文档
This commit is contained in:
@@ -1,3 +1,60 @@
|
|||||||
|
- [ElasticSearch](#elasticsearch)
|
||||||
|
- [简介](#简介)
|
||||||
|
- [用例场景](#用例场景)
|
||||||
|
- [安装](#安装)
|
||||||
|
- [add elasticsearch GPG keys](#add-elasticsearch-gpg-keys)
|
||||||
|
- [Add Elasticsearch 8.x APT Repository](#add-elasticsearch-8x-apt-repository)
|
||||||
|
- [install elastic search](#install-elastic-search)
|
||||||
|
- [Indices, documents, and fields](#indices-documents-and-fields)
|
||||||
|
- [Documents and fields](#documents-and-fields)
|
||||||
|
- [metadata field](#metadata-field)
|
||||||
|
- [Mappings and data types](#mappings-and-data-types)
|
||||||
|
- [向ElasticSearch中添加数据](#向elasticsearch中添加数据)
|
||||||
|
- [General content](#general-content)
|
||||||
|
- [Timestamped data](#timestamped-data)
|
||||||
|
- [查询和分析数据](#查询和分析数据)
|
||||||
|
- [Rest Api](#rest-api)
|
||||||
|
- [query language](#query-language)
|
||||||
|
- [Query DSL](#query-dsl)
|
||||||
|
- [Query DSL分析](#query-dsl分析)
|
||||||
|
- [ES | QL](#es--ql)
|
||||||
|
- [使用ElasticSearch Api索引和查询数据](#使用elasticsearch-api索引和查询数据)
|
||||||
|
- [创建索引](#创建索引)
|
||||||
|
- [向索引中添加数据](#向索引中添加数据)
|
||||||
|
- [向索引中添加单个document](#向索引中添加单个document)
|
||||||
|
- [向索引中添加多个document](#向索引中添加多个document)
|
||||||
|
- [定义mapping和data type](#定义mapping和data-type)
|
||||||
|
- [使用dynamic mapping](#使用dynamic-mapping)
|
||||||
|
- [手动指定索引的mapping](#手动指定索引的mapping)
|
||||||
|
- [将dynamic mapping和手动指定mapping相结合](#将dynamic-mapping和手动指定mapping相结合)
|
||||||
|
- [搜索索引](#搜索索引)
|
||||||
|
- [搜索所有文档](#搜索所有文档)
|
||||||
|
- [match请求](#match请求)
|
||||||
|
- [删除索引](#删除索引)
|
||||||
|
- [全文本搜索和过滤](#全文本搜索和过滤)
|
||||||
|
- [创建索引](#创建索引-1)
|
||||||
|
- [批量插入数据](#批量插入数据)
|
||||||
|
- [执行full-text search](#执行full-text-search)
|
||||||
|
- [`match`](#match)
|
||||||
|
- [contains all terms](#contains-all-terms)
|
||||||
|
- [应匹配terms的最小个数](#应匹配terms的最小个数)
|
||||||
|
- [search across mutli fields](#search-across-mutli-fields)
|
||||||
|
- [filter \& exact matches](#filter--exact-matches)
|
||||||
|
- [bool query](#bool-query)
|
||||||
|
- [date range](#date-range)
|
||||||
|
- [exact match](#exact-match)
|
||||||
|
- [组合搜索](#组合搜索)
|
||||||
|
- [使用Query DSL分析数据](#使用query-dsl分析数据)
|
||||||
|
- [get metrics](#get-metrics)
|
||||||
|
- [计算订单的平均值](#计算订单的平均值)
|
||||||
|
- [在单个请求中计算订单的多个metrics](#在单个请求中计算订单的多个metrics)
|
||||||
|
- [根据category对订单进行分组](#根据category对订单进行分组)
|
||||||
|
- [date\_histogram](#date_histogram)
|
||||||
|
- [Calendar intervals](#calendar-intervals)
|
||||||
|
- [compare category performance](#compare-category-performance)
|
||||||
|
- [analyze daily sale performance](#analyze-daily-sale-performance)
|
||||||
|
- [pipeline aggregation demo](#pipeline-aggregation-demo)
|
||||||
|
|
||||||
# ElasticSearch
|
# ElasticSearch
|
||||||
## 简介
|
## 简介
|
||||||
ElasticSearch是一个分布式的搜索和分析引擎、可缩放的数据存储、矢量数据库(vector database)。
|
ElasticSearch是一个分布式的搜索和分析引擎、可缩放的数据存储、矢量数据库(vector database)。
|
||||||
79
elastic search/02_elastic_search_index.md
Normal file
79
elastic search/02_elastic_search_index.md
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
# index modules
|
||||||
|
## index module introduce
|
||||||
|
index module用于对索引进行创建,并且控制和索引相关的各个方面。
|
||||||
|
|
||||||
|
### 索引设置
|
||||||
|
索引级别的设置可以针对每个索引来进行设置,设置分类如下:
|
||||||
|
- static:static设置只能在如下时机应用于索引
|
||||||
|
- 索引创建时
|
||||||
|
- 索引处于closed状态
|
||||||
|
- 使用`update-index-settings` api,并且带有`reopen=true`的查询参数,带有该参数时,会关闭受影响索引,更新后将受影响索引重新开启
|
||||||
|
- dynamic:dynamic索引可以在索引处于活跃状态时,通过`udpate-index-settings` api进行修改
|
||||||
|
|
||||||
|
> #### closed index
|
||||||
|
> 当一个索引处于`closed`状态时,所有针对该索引的read/write操作都会被阻塞,所有可以用于`opened`状态索引的操作,其执行对于`closed`状态的索引来说都不允许。
|
||||||
|
>
|
||||||
|
> 对于`closed`状态的索引,既无法向其中新增文档,也无法在索引中检索文档。
|
||||||
|
>
|
||||||
|
> 处于closed状态的索引能够通过`open index api`来重新开启。
|
||||||
|
|
||||||
|
### static index settings
|
||||||
|
#### `index.number_of_shards`
|
||||||
|
索引拥有的primary shards数量,默认为1,该配置项只能在索引创建时被设置。`即使索引处于closed状态,该配置项也无法修改。`
|
||||||
|
|
||||||
|
> `index.number_of_shards`最大为1024
|
||||||
|
|
||||||
|
## Analysis
|
||||||
|
index analysis module充当了可配置的analyzer注册中心,analyzer可用于将`string`类型字段转换为独立的terms,这将用于:
|
||||||
|
- 将文档string field的字段转化为terms,并且将terms添加到倒排索引中,令文档可以被搜索
|
||||||
|
- analyzer被用于高级查询,例如`match`查询,将用户输入的查询字符串分割为search terms用于查询
|
||||||
|
|
||||||
|
## index shard allocation
|
||||||
|
index shard allocation提供了可针对单个索引的设置,用于控制node中shard的分配:
|
||||||
|
- shard allocating filtering:控制shard被分配给哪个node
|
||||||
|
- delayed allocation:`节点离开导致未分配shard`的延迟分配
|
||||||
|
- total shards per node:相同索引在同一node中,shards数量的上限
|
||||||
|
- data tier allocation:控制对data tier分配的索引
|
||||||
|
|
||||||
|
## index blocks
|
||||||
|
index blocks限制了针对特定索引的操作类型。操作阻塞的类型可以分为:
|
||||||
|
- 读操作阻塞
|
||||||
|
- 写操作阻塞
|
||||||
|
- 元数据操作阻塞
|
||||||
|
|
||||||
|
针对索引操作的阻塞可以通过`dynamic index setting`来进行新增和移除。并且,阻塞也可以通过特定的api来进行添加和移除。
|
||||||
|
|
||||||
|
针对wrtie blocks设置的修改,一旦修改成功,那么所有的在途写操作都已经完成。
|
||||||
|
|
||||||
|
### index block settings
|
||||||
|
#### `index.blocks.read_only`
|
||||||
|
如果该配置项设置为true,该索引和索引的元数据都是只读的,当设置为`false`时,允许写操作和元数据变更。
|
||||||
|
|
||||||
|
#### `index.blocks.read_only_allow_delete`
|
||||||
|
类似于`index.blocks.write`,但是可以对index进行删除。不要针对`index.blocks.read_only_allow_delete`进行手动设置或移除。`disk-based shard allocator`会根据磁盘剩余空间自动添加或移除该配置项。
|
||||||
|
|
||||||
|
从索引中删除文档释放资源(而不是删除索引本身)会暂时增加索引的大小,故而在node的磁盘空间不足时可能无法实现。当`index.blocks.read_only_allow_delete`被设置为true时,`并不允许删除索引中的文档`。但是,删除索引本身的操作只需要极少量的额外磁盘空间,并且几乎可以立即删除索引所占用的空间,故而删除索引本身的操作仍然被允许。
|
||||||
|
|
||||||
|
> elastic search在磁盘占用高于`flood stage watermark`时,会自动为索引增加` read-only-allow-delete`阻塞;当磁盘占用率跌倒`high watermark`之下时,则是会自动释放该阻塞
|
||||||
|
|
||||||
|
#### `index.blocks.read`
|
||||||
|
如果设置为true,会阻塞针对index的读操作
|
||||||
|
|
||||||
|
#### `index.blocks.write`
|
||||||
|
如果设置为true,会阻塞针对索引的写操作,和`index.blocks.read_only`不同,本设置项并不影响metadata。
|
||||||
|
|
||||||
|
例如,为索引设置write block后,仍然可以对metadata进行变更,但是设置了`index.blocks.read_only`后,无法对元数据进行变更
|
||||||
|
|
||||||
|
#### `index.blocks.metadata`
|
||||||
|
如果设置为true,会禁止对元数据的读取和变更
|
||||||
|
|
||||||
|
### 增加index block示例
|
||||||
|
```bash
|
||||||
|
# PUT /<index>/_block/<block>
|
||||||
|
PUT /my-index-000001/_block/write
|
||||||
|
```
|
||||||
|
参数示例:
|
||||||
|
- `index`: 由`,`分隔的列表或通配符表达式,代表该请求的索引名称
|
||||||
|
- 默认情况下,需要显式指定
|
||||||
|
|
||||||
|
|
||||||
@@ -1,302 +0,0 @@
|
|||||||
# Elastic Search
|
|
||||||
## 倒排索引
|
|
||||||
ES为文档型数据库,重要用于全文检索数据。ES通过倒排索引来加速全文检索。
|
|
||||||
## 索引基础操作
|
|
||||||
### 索引创建
|
|
||||||
若要创建自定义索引,可向ES服务器发送`PUT`请求。
|
|
||||||
|
|
||||||
```
|
|
||||||
PUT http://${url}:${port}/${ind_name}
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
PUT请求具有幂等性,如果对相同名称索引进行重复创建,那么第二次创建索引时请求会失败,提示待创建索引已经存在。
|
|
||||||
|
|
||||||
### 索引查询
|
|
||||||
如果想要查询已经创建的索引信息,可以向ES发送`GET`请求。
|
|
||||||
|
|
||||||
```
|
|
||||||
GET http://${url}:${port}/${ind_name}
|
|
||||||
```
|
|
||||||
|
|
||||||
### 查看所有索引
|
|
||||||
|
|
||||||
```
|
|
||||||
GET http://${url}:${port}/_cat/indices?v
|
|
||||||
```
|
|
||||||
|
|
||||||
### 删除索引
|
|
||||||
如果想要删除已经存在的索引,可以向ES发送`DELETE`请求。
|
|
||||||
|
|
||||||
```
|
|
||||||
DELETE http://${url}:${port}/${ind_name}
|
|
||||||
```
|
|
||||||
|
|
||||||
### 向索引中添加数据
|
|
||||||
可以通过向ES发送`POST`请求来向索引中添加文档内容,其中待添加数据为JSON格式
|
|
||||||
```
|
|
||||||
POST http://${url}:${port}/${ind_name}/_doc
|
|
||||||
|
|
||||||
==========JSON CONTENT BODY=============
|
|
||||||
```
|
|
||||||
|
|
||||||
向索引中添加内容是非幂等的,一个索引中可以包含多条数据。
|
|
||||||
|
|
||||||
### 查询索引中所有的数据
|
|
||||||
```
|
|
||||||
GET http://${url}:${port}/${ind_name}/_search
|
|
||||||
```
|
|
||||||
### 修改索引中指定文档的内容
|
|
||||||
如果要对索引中的文档内容进行修改,有两种修改的方式:
|
|
||||||
- 覆盖性修改,用新内容覆盖旧内容
|
|
||||||
- 局部修改,只针对原JSON格式数据中的部分内容进行修改
|
|
||||||
|
|
||||||
#### 覆盖性修改
|
|
||||||
如果要对索引中的文档进行覆盖性修改,可以向ES发送`PUT`请求:
|
|
||||||
```
|
|
||||||
PUT http://${url}:${port}/${ind_name}/_doc/${doc_id}
|
|
||||||
|
|
||||||
==========JSON CONTENT BODY=============
|
|
||||||
```
|
|
||||||
|
|
||||||
#### 局部数据更新
|
|
||||||
如果要对索引中的文档内容进行部分更新,可以向ES发送`POST`请求:
|
|
||||||
```
|
|
||||||
POST http://${url}:${port}/${ind_name}/_update/${doc_id}
|
|
||||||
|
|
||||||
==========JSON CONTENT BODY=============
|
|
||||||
```
|
|
||||||
### 对索引中的文档内容进行删除
|
|
||||||
```
|
|
||||||
DELETE http://${url}:${port}/${ind_name}/_doc/${doc_id}
|
|
||||||
```
|
|
||||||
|
|
||||||
### 索引内容条件查询
|
|
||||||
通过GET请求进行索引中文档的条件查询
|
|
||||||
```
|
|
||||||
GET http://${url}:${port}/${ind_name}/_search?q=${doc_field_name}:${doc_field_val}
|
|
||||||
```
|
|
||||||
|
|
||||||
通过POST请求进行索引中文档的条件查询
|
|
||||||
```
|
|
||||||
POST http://${url}:${port}/${ind_name}/_search
|
|
||||||
|
|
||||||
{
|
|
||||||
"query":{
|
|
||||||
"match":{
|
|
||||||
"category":"sword"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
查询索引中所有的文档
|
|
||||||
```
|
|
||||||
POST http://${url}:${port}/${ind_name}/_search
|
|
||||||
|
|
||||||
{
|
|
||||||
"query":{
|
|
||||||
"match_all":{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### 索引内容分页查询
|
|
||||||
```
|
|
||||||
POST http://${url}:${port}/${ind_name}/_search
|
|
||||||
|
|
||||||
{
|
|
||||||
"query":{
|
|
||||||
"match_all":{
|
|
||||||
},
|
|
||||||
"from":0, // 查询偏移量,从0开始
|
|
||||||
"size":10 // 查询条数
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
### 索引中只查询文档的特定字段
|
|
||||||
如下示例只查询了文档中title和category字段的内容
|
|
||||||
```
|
|
||||||
POST http://${url}:${port}/${ind_name}/_search
|
|
||||||
|
|
||||||
{
|
|
||||||
"query":{
|
|
||||||
"match_all":{
|
|
||||||
},
|
|
||||||
"from":0, // 查询偏移量,从0开始
|
|
||||||
"size":10, // 查询条数
|
|
||||||
"_source":[
|
|
||||||
"title",
|
|
||||||
"category"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### 索引查询后对查询结果排序
|
|
||||||
```
|
|
||||||
POST http://${url}:${port}/${ind_name}/_search
|
|
||||||
|
|
||||||
{
|
|
||||||
"query":{
|
|
||||||
"match_all":{
|
|
||||||
},
|
|
||||||
"from":0, // 查询偏移量,从0开始
|
|
||||||
"size":10, // 查询条数
|
|
||||||
"_source":[
|
|
||||||
"title",
|
|
||||||
"category"
|
|
||||||
],
|
|
||||||
"sort":{
|
|
||||||
"price":{
|
|
||||||
"order":"desc"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### 多个查询条件进行组合
|
|
||||||
#### and
|
|
||||||
```
|
|
||||||
POST http://${url}:${port}/${ind_name}/_search
|
|
||||||
|
|
||||||
{
|
|
||||||
"query":{
|
|
||||||
"bool":{
|
|
||||||
"must":[
|
|
||||||
{
|
|
||||||
"match":{
|
|
||||||
"category":"sword"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"match":{
|
|
||||||
"price":100
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
#### or
|
|
||||||
```
|
|
||||||
POST http://${url}:${port}/${ind_name}/_search
|
|
||||||
|
|
||||||
{
|
|
||||||
"query":{
|
|
||||||
"bool":{
|
|
||||||
"should":[
|
|
||||||
{
|
|
||||||
"match":{
|
|
||||||
"category":"sword"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"match":{
|
|
||||||
"price":100
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### 索引范围查询
|
|
||||||
```
|
|
||||||
POST http://${url}:${port}/${ind_name}/_search
|
|
||||||
|
|
||||||
{
|
|
||||||
"query":{
|
|
||||||
"bool":{
|
|
||||||
"should":[
|
|
||||||
{
|
|
||||||
"match":{
|
|
||||||
"category":"sword"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"match":{
|
|
||||||
"price":100
|
|
||||||
}
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"filter":{
|
|
||||||
"range":{
|
|
||||||
"price":{
|
|
||||||
"gt":1000
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
### 索引分组查询
|
|
||||||
```
|
|
||||||
POST http://${url}:${port}/${ind_name}/_search
|
|
||||||
|
|
||||||
{
|
|
||||||
"aggs":{
|
|
||||||
${group_name}:{
|
|
||||||
"terms":{
|
|
||||||
"field":"price"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"size":0 // 只展示聚合后的结果,不展示原始数据
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### 统计求平均值
|
|
||||||
```
|
|
||||||
POST http://${url}:${port}/${ind_name}/_search
|
|
||||||
|
|
||||||
{
|
|
||||||
"aggs":{
|
|
||||||
${group_name}:{
|
|
||||||
"avg":{
|
|
||||||
"field":"price"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"size":0 // 只展示统计后的结果,不展示原始数据
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### 索引字段配置
|
|
||||||
可以通过如下方式来配置索引信息
|
|
||||||
```
|
|
||||||
POST http://${url}:${port}/${ind_name}/_mapping
|
|
||||||
|
|
||||||
|
|
||||||
{
|
|
||||||
"properties":{
|
|
||||||
${field_1}:{
|
|
||||||
"type":"text", // 该字段内容可被分词
|
|
||||||
"index":true // 该字段内容可被索引
|
|
||||||
},
|
|
||||||
${field_2}:{
|
|
||||||
"type":"keyword", // 该字段内容不可被分词
|
|
||||||
"index":true // 该字段内容可被索引
|
|
||||||
},
|
|
||||||
${field_3}:{
|
|
||||||
"type":"keyword", // 该字段内容不可被分词
|
|
||||||
"index":false // 该字段内容不可被索引,在索引查询时将该字段设置为查询条件会报错
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user