ES 文档看起来有点混乱。
我需要为数据创建一个索引,如下所示:
{
"company_id": uuid, //indexable
"author_id": uuid, //indexable
"image_url": str,
"created_at": datetime, //indexable
"text": array //indexable
}
文本字段是一个语言数组。 Langs是动态的,可能只有en,也可能是it、fr、hy,如:
en => Hello
fr => Bonjour
或
ru => Привет
hy => Բարև
所以我在理解索引方面遇到了问题。在列表中,您可以看到哪些字段可索引,哪些字段不可索引。
我尝试用这个手动设置映射:
$params = [
'index' => 'posts_index',
'body' => [
'mappings' => [
'properties' => [
'text' => [
'type' => 'object',
'properties' => []
],
'company_id' => [
'type' => 'keyword',
'index' => true,
],
'author_id' => [
'type' => 'keyword',
'index' => true,
],
'image_url' => [
'type' => 'text',
'index' => false,
],
'created_at' => [
'type' => 'date',
'index' => true,
'format' => 'yyyy-MM-dd HH:mm:ss'
],
'updated_at' => [
'type' => 'date',
'index' => false,
'format' => 'yyyy-MM-dd HH:mm:ss'
],
],
],
],
];
然后我创建了 11000 条测试记录,在 Kibana 中我看到索引大约需要 36MB 如果我删除索引并创建 11000 个文档而不手动映射,则索引大约需要 21MB 并且看起来所有字段都是可索引的。我只是想使用尽可能少的存储空间,但另一方面,让某些字段可索引很重要。
该怎么办?
这是一个有趣的用例,因为不同语言的文本字段都有动态键。 ES 可以处理动态映射,您应该小心映射以避免扰乱性能。
你可以尝试这个映射:
$params = [
'index' => 'posts_index',
'body' => [
'mappings' => [
'dynamic_templates' => [
[
'texts' => [
'path_match' => 'text.*',
'mapping' => [
'type' => 'text',
'index' => true
]
]
]
],
'properties' => [
'text' => [
'type' => 'object',
'enabled' => true
],
'company_id' => [
'type' => 'keyword'
],
'author_id' => [
'type' => 'keyword'
],
'image_url' => [
'type' => 'text',
'index' => false
],
'created_at' => [
'type' => 'date',
'format' => 'yyyy-MM-dd HH:mm:ss'
],
'updated_at' => [
'type' => 'date',
'index' => false,
'format' => 'yyyy-MM-dd HH:mm:ss'
]
],
],
],
];
用于处理添加到文本字段的任何新语言的
dynamic_templates
部分,我删除了 index
设置,因为 keywords
默认情况下已建立索引。