【Elasticsearch】`nested`和`flattened`字段在索引时有显著的区别

有同学问,nested查询效率不高为啥不直接扁平化查询呢?就跟之前的普通结构查询一样,这就有些想当然了,因为扁平化的结构在存储时,其实跟我们想的不一样,接下来给出扁平化在索引时的存储结构(尤其是当嵌套对象为数组时)

在 Elasticsearch 中,`nested`和`flattened`字段在索引时有显著的区别,这些区别主要体现在数据的存储方式、查询能力和性能上。以下是详细的对比:


1.数据存储方式

`nested`字段

• 存储方式:`nested`字段将数组中的每个对象作为独立的隐藏文档进行索引。每个嵌套对象可以独立于其他对象进行查询。

• 示例:

```json

PUT my-index

{

"mappings": {

"properties": {

"comments": {

"type": "nested",

"properties": {

"author": { "type": "text" },

"content": { "type": "text" }

}

}

}

}

}

PUT my-index/_doc/1

{

"comments": [

{ "author": "Alice", "content": "Great post!" },

{ "author": "Bob", "content": "Very useful." }

]

}

```

在内部,Elasticsearch 会将每个`comments`对象索引为独立的隐藏文档,类似于:

```json

{

"comments.author": ["Alice", "Bob"],

"comments.content": ["Great post!", "Very useful."]

}[

{

"author": "Alice",

"content": "Great post!"

},

{

"author": "Bob",

"content": "Very useful."

}

]

```

`flattened`字段

• 存储方式:`flattened`字段将整个对象扁平化为一个单一字段,所有键值对都被存储在一个字段中。这种方式适合简单的键值对数据,但不适合复杂的数据结构。

• 示例:

```json

PUT my-index

{

"mappings": {

"properties": {

"comments": {

"type": "flattened"

}

}

}

}

PUT my-index/_doc/1

{

"comments": [

{ "author": "Alice", "content": "Great post!" },

{ "author": "Bob", "content": "Very useful." }

]

}

```

在内部,Elasticsearch 会将`comments`扁平化为:

```json

{

"comments.author": ["Alice", "Bob"],

"comments.content": ["Great post!", "Very useful."]

}

```

2.查询能力

`nested`字段

• 查询方式:使用`nested`查询,可以独立查询数组中的每个对象。

• 示例:

```json

GET my-index/_search

{

"query": {

"nested": {

"path": "comments",

"query": {

"bool": {

"must": [

{ "match": { "comments.author": "Alice" }},

{ "match": { "comments.content": "Great post!" }}

]

}

}

}

}

}

```

这个查询会匹配`author`为`Alice`且`content`为`Great post!`的评论。

`flattened`字段

• 查询方式:使用普通查询,但无法独立查询数组中的每个对象。

• 示例:

```json

GET my-index/_search

{

"query": {

"bool": {

"must": [

{ "match": { "comments.author": "Alice" }},

{ "match": { "comments.content": "Great post!" }}

]

}

}

}

```

这个查询会匹配包含`author`为`Alice`和`content`为`Great post!`的文档,但无法保证它们属于同一个评论对象。

3.性能

`nested`字段

• 性能:`nested`字段的查询性能通常比普通字段慢,因为每个嵌套对象都被索引为独立的隐藏文档。查询时需要额外的处理来匹配嵌套对象。

• 适用场景:适用于需要独立查询数组中的每个对象的场景。

`flattened`字段

• 性能:`flattened`字段的查询性能通常比`nested`字段高,因为数据结构更简单,索引和查询更高效。

• 适用场景:适用于简单的键值对数据,不需要复杂的查询。

4.总结

• `nested`字段:

• 优点:支持复杂的查询,可以独立查询数组中的每个对象。

• 缺点:查询性能较低,索引和查询更复杂。

• 适用场景:需要独立查询数组中的每个对象,或需要支持复杂的嵌套查询。

• `flattened`字段:

• 优点:查询性能高,索引和查询更简单。

• 缺点:不支持复杂的查询,无法独立查询数组中的每个对象。

• 适用场景:简单的键值对数据,不需要复杂的查询。


5.是否可以在索引时选择使用`nested`或`flattened`?

在索引时,你可以根据需求选择使用`nested`或`flattened`字段,但它们的存储方式和查询能力有显著区别。以下是选择的建议:

• 如果需要独立查询数组中的每个对象,或者需要支持复杂的嵌套查询,应使用`nested`字段。

• 如果数据结构简单,不需要复杂的查询,且需要更高的性能,应使用`flattened`字段。

总之,选择哪种字段类型取决于你的具体需求和数据结构。

相关推荐
{⌐■_■}9 小时前
【git】提交修改、回撤、回滚、Tag 操作讲解,与reset (--soft、--mixed、--hard) 的区别
大数据·git·elasticsearch
screamn11 小时前
ElasticSearch详解
大数据·elasticsearch·jenkins
不是乖小孩11 小时前
elasticsearch在windows上的配置
大数据·elasticsearch·jenkins
罗技12311 小时前
推荐给 Easysearch 新用户的几个 Elasticsearch 可视化工具
大数据·elasticsearch·jenkins
光仔December1 天前
【Elasticsearch入门到落地】8、RestClient操作索引库-基础介绍及导入demo
elasticsearch·搜索引擎·全文检索·索引·映射
risc1234561 天前
【Elasticsearch】Retrieve inner hits获取嵌套查询的具体的嵌套文档来源,以及父子文档的来源
elasticsearch
铭毅天下1 天前
极限网关可视化——Elasticsearch 请求流量分析实战
大数据·elasticsearch·搜索引擎·全文检索·jenkins
宇智波云1 天前
ubuntu22.4搭建单节点es8.1
运维·elasticsearch
risc1234561 天前
【Elasticsearch】搜索时排序规则
elasticsearch
铭毅天下1 天前
Elasticsearch 中如何限制和指定 IP 地址的访问?
大数据·tcp/ip·elasticsearch·搜索引擎·全文检索