【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`字段。

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

相关推荐
Elastic 中国社区官方博客13 小时前
在 Windows 上使用 Docker 运行 Elastic Open Crawler
大数据·windows·爬虫·elasticsearch·搜索引擎·docker·容器
Britz_Kevin19 小时前
从零开始的云计算生活——第三十三天,关山阻隔,ELK日志分析
elasticsearch·#elk·#logstash·#kibana
Elasticsearch19 小时前
AI 驱动的仪表板:从愿景到 Kibana-CSDN博客
elasticsearch
EmpressBoost2 天前
docker run elasticsearch 报错
elasticsearch·docker·容器
不平衡的叉叉树2 天前
ES组合使用must与should时的注意事项
elasticsearch
小阳睡不醒2 天前
小白成长之路-Elasticsearch 7.0 配置
大数据·elasticsearch·excel
极限实验室2 天前
Console 纳管 Elasticsearch 9(二):日志监控
数据库·elasticsearch
Roc-xb2 天前
如何关闭Elasticsearch的安全认证的解决方法
java·elasticsearch
TinpeaV3 天前
Elasticsearch8 Windows安装教程
windows·spring boot·后端·elasticsearch
舒一笑3 天前
Elasticsearch 的模糊查询相关概念
elasticsearch