有同学问,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
[
{
"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`字段。
总之,选择哪种字段类型取决于你的具体需求和数据结构。