忿忿的泥巴坨 发表于 4 天前

Elasticsearch Synthetic _source

https://i-blog.csdnimg.cn/direct/edeb76fa27d6476c91ab4b2bb232a038.png
_source 字段包罗索引时传入的原始 JSON 文档体。_source 字段自己不被索引(因此不可搜刮),但会被存储,以便在执行获取请求(如 get 或 search)时返回。
如果磁盘使用很紧张,可以考虑以下选项:


[*]使用 synthetic _source,在检索时重建源内容,而不是存储在磁盘上。这样可以减少磁盘使用,但会导致 Get 和 Search 查询中访问 _source 变慢。
[*]完全禁用 _source 字段。这样可以减少磁盘使用,但会禁用依靠 _source 的功能。

什么是 synthetic _source?

当文档被索引时,有些字段,好比必要生成 doc_values 或 stored fileds,来自 _source 的字段值会根据数据类型复制到独立的列表 doc_values 中(磁盘上的不同数据结构,用于模式匹配),这样可以独立搜刮这些值。当在这些小列表中找到所需值后,返回原始文档。由于只搜刮了小列表,而不是整个文档的所有字段值,搜刮所需的时间会减少。固然这种处置惩罚方式提升了速度,但会在小列表和原始文档中存储重复的数据。
更多阅读:


[*] Elasticsearch:inverted index,doc_values 及 source
[*] Elasticsearch: 明白 mapping 中的 store 属性
Synthetic _source 是一种索引配置模式,可以改变文档在摄取时的处置惩罚方式,以节流存储空间并制止数据重复。它会创建独立的列表,但不会生存原始的原始文档。相反,在找到值后,会使用小列表中的数据重建 _source 内容。由于没有存储原始文档,仅在磁盘上存储 “列表”,可以节流大量存储空间。
PUT idx
{
"settings": {
    "index": {
      "mapping": {
      "source": {
          "mode": "synthetic"
      }
      }
    }
}
}   必要留意的是,由于 _source 值是在文档被检索时即时重建的,因此必要额外时间来完成重建。这会为用户节流存储空间,但会降低搜刮速度。固然这种即时重建通常比直接生存源文档并在查询时加载更慢,但它节流了大量存储空间。通过在不必要时不加载 _source 字段,可以制止额外的耽误。
Synthetic _source 现在被广泛使用于 logsdb 及 TSDB。它可以帮我们节流很多的磁盘空间。
   Elasticsearch 8.17 Logsdb:企业降本增效利器

支持的字段

Synthetic _source 支持所有字段类型。根据实现细节,不同字段类型在使用 synthetic _source 时具有不同属性。
大多数字段类型使用现有数据构建 synthetic _source,最常见的是 doc_values 和 stored fields。对于这些字段类型,不必要额外空间来存储 _source 字段内容。由于 doc_values 的存储布局,生成的 _source 字段相比原始文档会有修改。
对于其他所有字段类型,字段的原始值会按原样存储,方式与非 synthetic 模式下的 _source 字段相同。这种环境下不会有修改,_source 中的字段数据与原始文档相同。同样,使用 ignore_malformed 或 ignore_above 的字段的格式错误值也必要按原样存储。这种方式存储效率较低,由于为 _source 重建所需的数据除了索引字段所需的其他数据(如 doc_values)外,还会额外存储。

Synthetic _source 限定

某些字段类型有额外限定,这些限定记录在字段类型文档的 synthetic _source 部分。
Synthetic _source 不支持仅存储源的快照仓库。要存储使用 synthetic _source 的索引,请选择其他类型的仓库。

Synthetic _source 修改

启用 synthetic _source 时,检索到的文档相比原始 JSON 会有一些修改。

数组被移动到叶子字段

Synthetic _source 中的数组会被移动到叶子字段。比方:
由于 _source 值是通过 “doc values” 列表中的值重建的,因此原始 JSON 会被做一些修改。比方,数组会被移到叶子节点。
PUT idx/_doc/1
{
"foo": [
    {
      "bar": 1
    },
    {
      "bar": 2
    }
]
} 将变为:
{
"foo": {
    "bar":
}
} 这可能导致某些数组消散:
PUT idx/_doc/1
{
"foo": [
    {
      "bar": 1
    },
    {
      "baz": 2
    }
]
} 将变为:
{
"foo": {
    "bar": 1,
    "baz": 2
}
}
字段名称与映射同等

Synthetic _source 使用映射中字段的原始名称。当与动态映射一起使用时,字段名中带点(.)的字段默认被表明为多个对象,而在禁用子对象的对象中,字段名中的点会被生存。比方:
PUT idx/_doc/1
{
"foo.bar.baz": 1
} 将变为:
{
"foo": {
    "bar": {
      "baz": 1
    }
}
}
怎样将索引配置为 synthetic _source 模式

测试代码:在此测试中,将 synthetic _source 模式下的索引与尺度索引进行对比。
PUT index
{
"settings": {
    "index": {
      "mapping": {
      "source": {
          "mode": "synthetic"
      }
      }
    }
}
}
测试

尺度索引使用 multi-field 来阐明怎样通过全文搜刮和聚合检索文档,并在 _source 内容中包罗已禁用字段的值。
PUT test_standard
{
"mappings": {
    "properties": {
      "disabled_field": {
      "enabled": false
      },
      "multi_field": {
      "type": "text",
      "fields": {
          "keyword": {
            "type": "keyword"
          }
      }
      }
    }
}
} 让我们导入一些示例文档:
PUT test_standard/_doc/1
{
"multi_field": "Host_01",
"disabled_field" : "Required for storage 01"
}

PUT test_standard/_doc/2
{
"multi_field": "Host_02",
"disabled_field" : "Required for storage 02"
}

PUT test_standard/_doc/3
{
"multi_field": "Host_03",
"disabled_field" : "Required for storage 03"
} 全文搜刮会检索带有 _source 内容的文档:
GET test_standard/_search
{
"query": {
    "match": {
      "multi_field": "host_01"
    }
}
} 效果:文档通过对已分析的字段进行全文搜刮被检索到。返回的效果包罗 _source 中的所有值,包罗已被禁用的字段:
{
"took": 17,
"timed_out": false,
"_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
},
"hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 0.9808291,
    "hits": [
      {
      "_index": "test_standard",
      "_id": "1",
      "_score": 0.9808291,
      "_source": {
          "multi_field": "Host_01",
          "disabled_field": "Required for storage 01"
      }
      }
    ]
}
} 这里,synthetic _source 模式下的索引使用 multi-fields 来阐明 “text” 数据类型怎样用于 “doc values” 列表,以及禁用字段中的值怎样不可用。
PUT test_synthetic
{
"settings": {
    "index": {
      "mapping": {
      "source": {
          "mode": "synthetic"
      }
      }
    }
},
"mappings": {
    "properties": {
      "keyword_field": {
      "type": "keyword"
      },
      "multi_field": {
      "type": "text",
      "fields": {
          "keyword": {
            "type": "keyword"
          }
      }
      },
      "text_field": {
      "type": "text"
      },
      "disabled_field": {
      "enabled": false
      },
      "skill_array_field": {
      "properties": {
          "language": {
            "type": "text"
          },
          "level": {
            "type": "text"
          }
      }
      }
    }
}
} 让我们导入一些示例文档:
PUT test_synthetic/_doc/1
{
"keyword_field": "Host_01",
"disabled_field": "Required for storage 01",
"multi_field": "Some info about computer 1",
"text_field": "This is a text field 1",
"skills_array_field": [
    {
      "language": "ruby",
      "level": "expert"
    },
    {
      "language": "javascript",
      "level": "beginner"
    }
],
"foo": [
    {
      "bar": 1
    },
    {
      "bar": 2
    }
],
"foo1.bar.baz": 1
}

PUT test_synthetic/_doc/2
{
"keyword_field": "Host_02",
"disabled_field": "Required for storage 02",
"multi_field": "Some info about computer 2",
"text_field": "This is a text field 2",
"skills_array_field": [
    {
      "language": "C",
      "level": "guru"
    },
    {
      "language": "javascript",
      "level": "beginner"
    }
],
"foo": [
    {
      "bar": 1
    },
    {
      "bar": 2
    }
],
"foo1.bar.baz": 2
}

PUT test_synthetic/_doc/3
{
"keyword_field": "Host_03",
"disabled_field": "Required for storage 03",
"multi_field": "Some info about computer 3",
"text_field": "This is a text field 3",
"skills_array_field": [
    {
      "language": "golang",
      "level": "beginner"
    }
],
"foo": [
    {
      "bar": 1
    },
    {
      "bar": 2
    }
],
"foo1.bar.baz": 3
} 搜刮 “keyword” 数据类型时必要准确匹配。另外,禁用字段中的值也不再可用。
GET test_synthetic/_search
{
"query": {
    "match": {
      "keyword_field": "Host_01"
    }
}
} 响应:
{
"took": 1,
"timed_out": false,
"_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
},
"hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 0.9808291,
    "hits": [
      {
      "_index": "test_synthetic",
      "_id": "1",
      "_score": 0.9808291,
      "_source": {
          "keyword_field": "Host_01",
          "disabled_field": "Required for storage 01",
          "multi_field": "Some info about computer 1",
          "text_field": "This is a text field 1",
          "skills_array_field": [
            {
            "language": "ruby",
            "level": "expert"
            },
            {
            "language": "javascript",
            "level": "beginner"
            }
          ],
          "foo": [
            {
            "bar": 1
            },
            {
            "bar": 2
            }
          ],
          "foo1.bar.baz": 1
      }
      }
    ]
}
} 我们再做一次搜刮:
GET test_synthetic/_search
{
"query": {
    "match": {
      "multi_field": "info"
    }
}
} 响应是:
{
"took": 1,
"timed_out": false,
"_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
},
"hits": {
    "total": {
      "value": 3,
      "relation": "eq"
    },
    "max_score": 0.13353139,
    "hits": [
      {
      "_index": "test_synthetic",
      "_id": "2",
      "_score": 0.13353139,
      "_source": {
          "keyword_field": "Host_02",
          "disabled_field": "Required for storage 02",
          "multi_field": "Some info about computer 2",
          "text_field": "This is a text field 2",
          "skills_array_field": [
            {
            "language": "C",
            "level": "guru"
            },
            {
            "language": "javascript",
            "level": "beginner"
            }
          ],
          "foo": [
            {
            "bar": 1
            },
            {
            "bar": 2
            }
          ],
          "foo1.bar.baz": 2
      }
      },
      {
      "_index": "test_synthetic",
      "_id": "3",
      "_score": 0.13353139,
      "_source": {
          "keyword_field": "Host_03",
          "disabled_field": "Required for storage 03",
          "multi_field": "Some info about computer 3",
          "text_field": "This is a text field 3",
          "skills_array_field": [
            {
            "language": "golang",
            "level": "beginner"
            }
          ],
          "foo": [
            {
            "bar": 1
            },
            {
            "bar": 2
            }
          ],
          "foo1.bar.baz": 3
      }
      },
      {
      "_index": "test_synthetic",
      "_id": "1",
      "_score": 0.13353139,
      "_source": {
          "keyword_field": "Host_01",
          "disabled_field": "Required for storage 01",
          "multi_field": "Some info about computer 1",
          "text_field": "This is a text field 1",
          "skills_array_field": [
            {
            "language": "ruby",
            "level": "expert"
            },
            {
            "language": "javascript",
            "level": "beginner"
            }
          ],
          "foo": [
            {
            "bar": 1
            },
            {
            "bar": 2
            }
          ],
          "foo1.bar.baz": 1
      }
      }
    ]
}
}
更多阅读,请参考官方文档:_source field | Elastic Documentation

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: Elasticsearch Synthetic _source