惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
人人都是产品经理
人人都是产品经理
博客园 - 三生石上(FineUI控件)
aimingoo的专栏
aimingoo的专栏
U
Unit 42
GbyAI
GbyAI
H
Help Net Security
A
Arctic Wolf
SecWiki News
SecWiki News
K
Kaspersky official blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
Blog — PlanetScale
Blog — PlanetScale
B
Blog
Spread Privacy
Spread Privacy
L
Lohrmann on Cybersecurity
C
Check Point Blog
O
OpenAI News
Microsoft Security Blog
Microsoft Security Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
大猫的无限游戏
大猫的无限游戏
Google DeepMind News
Google DeepMind News
Webroot Blog
Webroot Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
P
Palo Alto Networks Blog
A
About on SuperTechFans
S
SegmentFault 最新的问题
Recent Announcements
Recent Announcements
S
Schneier on Security
Martin Fowler
Martin Fowler
WordPress大学
WordPress大学
Jina AI
Jina AI
The Hacker News
The Hacker News
V2EX - 技术
V2EX - 技术
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
腾讯CDC
罗磊的独立博客
G
GRAHAM CLULEY
L
LINUX DO - 热门话题
雷峰网
雷峰网
博客园 - 【当耐特】
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
美团技术团队
M
MIT News - Artificial intelligence
Engineering at Meta
Engineering at Meta
Hacker News: Ask HN
Hacker News: Ask HN
S
Security Affairs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
月光博客
月光博客

Xiaobin's Notes

Mac本地快速部署DeepSeek Electron的原理 如何让大语言模型输出JSON格式 webstorm 的 cpu 占用高 修改Joplin主题样式 ECMAScript 历代版本 新一代包管理器 PNPM React useEffect() Hook Vue 2.x 使用高德地图JS API 2.0加载起点终点路径轨迹 家庭用电插座 MacOS 14.4 引发Java 应用崩溃 重定向广告 Pyenv工具 ElasticSearch集群原理 ElasticSearch集群节点 Elasticsearch Mapping 参数 Elasticsearch的数据类型 Go语言的向后兼容和toolchain规则 Go 1.21 新增特性
Elasticsearch元数据
xbl · 2023-11-16 · via Xiaobin's Notes

Meta-Fields(元数据)

本文基于 Elasticsearch 6.6.0

_all

_all字段是把其它字段拼接在一起的超级字段,所有的字段用空格分开,_all字段会被解析和索引,但是不存储。当你只想返回包含某个关键字的文档但是不明确地搜某个字段的时候就需要使用_all字段。
例子:

1
2
3
4
5
6
PUT my_index/blog/1 
{
"title": "Master Java",
"content": "learn java",
"author": "Tom"
}

_all字段包含:[ “Master”, “Java”, “learn”, “Tom” ]

搜索:

1
2
3
4
5
6
7
8
GET my_index/_search
{
"query": {
"match": {
"_all": "Java"
}
}
}

返回结果:

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
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.39063013,
"hits": [
{
"_index": "my_index",
"_type": "blog",
"_id": "1",
"_score": 0.39063013,
"_source": {
"title": "Master Java",
"content": "learn java",
"author": "Tom"
}
}
]
}
}

使用copy_to自定义_all字段:

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
27
28
29
30
31
32
33
34
35
PUT myindex
{
"mappings": {
"mytype": {
"properties": {
"title": {
"type": "text",
"copy_to": "full_content"
},
"content": {
"type": "text",
"copy_to": "full_content"
},
"full_content": {
"type": "text"
}
}
}
}
}

PUT myindex/mytype/1
{
"title": "Master Java",
"content": "learn Java"
}

GET myindex/_search
{
"query": {
"match": {
"full_content": "java"
}
}
}

_field_names

_field_names字段用来存储文档中的所有非空字段的名字,这个字段常用于exists查询。例子如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
PUT my_index/my_type/1
{
"title": "This is a document"
}

PUT my_index/my_type/2?refresh=true
{
"title": "This is another document",
"body": "This document has a body"
}

GET my_index/_search
{
"query": {
"terms": {
"_field_names": [ "body" ]
}
}
}

结果会返回第二条文档,因为第一条文档没有title字段。
同样,可以使用exists查询:

1
2
3
4
5
6
GET my_index/_search
{
"query": {
"exists" : { "field" : "body" }
}
}

_id

每条被索引的文档都有一个_type和_id字段,_id可以用于term查询、temrs查询、match查询、query_string查询、simple_query_string查询,但是不能用于聚合、脚本和排序。例子如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
PUT my_index/my_type/1
{
"text": "Document with ID 1"
}

PUT my_index/my_type/2
{
"text": "Document with ID 2"
}

GET my_index/_search
{
"query": {
"terms": {
"_id": [ "1", "2" ]
}
}
}

_index

多索引查询时,有时候只需要在特地索引名上进行查询,_index字段提供了便利,也就是说可以对索引名进行term查询、terms查询、聚合分析、使用脚本和排序。

_index是一个虚拟字段,不会真的加到索引中,对_index进行term、terms查询(也包括match、query_string、simple_query_string),但是不支持prefix、wildcard、regexp和fuzzy查询。

举例,2个索引2条文档

1
2
3
4
5
6
7
8
9
PUT index_1/my_type/1
{
"text": "Document in index 1"
}

PUT index_2/my_type/2
{
"text": "Document in index 2"
}

对索引名做查询、聚合、排序并使用脚本新增字段:

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
27
28
29
30
31
GET index_1,index_2/_search
{
"query": {
"terms": {
"_index": ["index_1", "index_2"]
}
},
"aggs": {
"indices": {
"terms": {
"field": "_index",
"size": 10
}
}
},
"sort": [
{
"_index": {
"order": "asc"
}
}
],
"script_fields": {
"index_name": {
"script": {
"lang": "painless",
"inline": "doc['_index']"
}
}
}
}

_meta

_parent

_parent用于指定同一索引中文档的父子关系。下面例子中现在mapping中指定文档的父子关系,然后索引父文档,索引子文档时指定父id,最后根据子文档查询父文档。

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
PUT my_index
{
"mappings": {
"my_parent": {},
"my_child": {
"_parent": {
"type": "my_parent"
}
}
}
}


PUT my_index/my_parent/1
{
"text": "This is a parent document"
}

PUT my_index/my_child/2?parent=1
{
"text": "This is a child document"
}

PUT my_index/my_child/3?parent=1&refresh=true
{
"text": "This is another child document"
}


GET my_index/my_parent/_search
{
"query": {
"has_child": {
"type": "my_child",
"query": {
"match": {
"text": "child document"
}
}
}
}
}

_routing

路由参数,ELasticsearch通过以下公式计算文档应该分到哪个分片上:

1
shard_num = hash(_routing) % num_primary_shards

默认的_routing值是文档的_id或者_parent,通过_routing参数可以设置自定义路由。例如,想把user1发布的博客存储到同一个分片上,索引时指定routing参数,查询时在指定路由上查询:

1
2
3
4
5
6
PUT my_index/my_type/1?routing=user1&refresh=true 
{
"title": "This is a document"
}

GET my_index/my_type/1?routing=user1

在查询的时候通过routing参数查询:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
GET my_index/_search
{
"query": {
"terms": {
"_routing": [ "user1" ]
}
}
}

GET my_index/_search?routing=user1,user2
{
"query": {
"match": {
"title": "document"
}
}
}

在Mapping中指定routing为必须的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
PUT my_index2
{
"mappings": {
"my_type": {
"_routing": {
"required": true
}
}
}
}

PUT my_index2/my_type/1
{
"text": "No routing value provided"
}

_source

存储的文档的原始值。默认_source字段是开启的,也可以关闭:

1
2
3
4
5
6
7
8
9
10
PUT tweets
{
"mappings": {
"tweet": {
"_source": {
"enabled": false
}
}
}
}

但是一般情况下不要关闭,除非你不想做以下操作:

  • 使用update、update_by_query、reindex
  • 使用高亮
  • 数据备份、改变mapping、升级索引
  • 通过原始字段debug查询或者聚合

_type

每条被索引的文档都有一个_type和_id字段,可以根据_type进行查询、聚合、脚本和排序。例子如下:

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
PUT my_index/type_1/1
{
"text": "Document with type 1"
}

PUT my_index/type_2/2?refresh=true
{
"text": "Document with type 2"
}

GET my_index/_search
{
"query": {
"terms": {
"_type": [ "type_1", "type_2" ]
}
},
"aggs": {
"types": {
"terms": {
"field": "_type",
"size": 10
}
}
},
"sort": [
{
"_type": {
"order": "desc"
}
}
],
"script_fields": {
"type": {
"script": {
"lang": "painless",
"inline": "doc['_type']"
}
}
}
}

_uid

_uid_type + _id 的组合,可用于查询、聚合、脚本和排序。例子如下:

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
PUT my_index/my_type/1
{
"text": "Document with ID 1"
}

PUT my_index/my_type/2?refresh=true
{
"text": "Document with ID 2"
}

GET my_index/_search
{
"query": {
"terms": {
"_uid": [ "my_type#1", "my_type#2" ]
}
},
"aggs": {
"UIDs": {
"terms": {
"field": "_uid",
"size": 10
}
}
},
"sort": [
{
"_uid": {
"order": "desc"
}
}
],
"script_fields": {
"UID": {
"script": {
"lang": "painless",
"inline": "doc['_uid']"
}
}
}
}