Elasticsearch索引及字段命名规范

Elasticsearch索引及字段命名规范

实际中使用Elasticsearch,首要需要考虑定义索引、映射以及字段等,本文总结下必要的命名规范。

索引

索引受文件系统的限制。仅可能为小写字母,不能下划线开头。同时需遵守下列规则:

  • 不能包括 , /, *, ?, ", <, >, |, 空格, 逗号, #
  • 7.0版本之前可以使用冒号:,但不建议使用并在7.0版本之后不再支持
  • 不能以这些字符 -, _, + 开头
  • 不能包括 . 或 …
  • 长度不能超过 255 个字符

以上这些命名限制是因为当Elasticsearch使用索引名称作为磁盘上的目录名称,这些名称必须符合不同操作系统的约定。
我猜想未来可能会放开这些限制,因为我们使用uuid关联索引放在磁盘上,而不使用索引名称。

类型

类型名称可以包括除了null的任何字符,不能以下划线开头。7.0版本之后不再支持类型,默认为_doc.

字段

对字段命名规范及不能完全使用空格。点号可以使用,但用于对象类型,举例:

"foo.bar.baz": "abc"

相当于:

"foo": {
  "bar": {
    "baz": "abc"
  }
}

“对象路径”必须是有效的。所以foo . . bar,这就产生了连锁反应,因为它不能解析到有效路径。类似的:

"foo.": {
  "bar": {
    "baz": "abc"
  }
}

同样这个命名也是不容许的,因为以点号结尾会造成混淆。

路由

路由命名也是除了空的任何字符。问题是多个路由值传入查询字符串,使用逗号分隔,如:
?routing=foo,bar

如果路由值包含逗号,会造成路由值解析错误。

总结

本文简要描述了elasticsearch的命名规范。了解其命名规范可以事先确定其数据源中相关数据库的命名规范,避免不必要的转换。

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 Elasticsearch 的 dynamic templates 功能,通过配置模板来实现字段的映射。 首先,在 Spring Boot 配置 Elasticsearch 的时候,可以使用 `ElasticsearchRestTemplate` 来操作 Elasticsearch。在创建 `ElasticsearchRestTemplate` 的时候,可以指定一个 `ElasticsearchConverter` 对象,用于将 Java 对象转换成 Elasticsearch 的文档。在 `ElasticsearchConverter` ,可以使用 `MappingElasticsearchConverter` 对象来实现字段映射。 具体的操作步骤如下: 1. 配置 `ElasticsearchRestTemplate`,指定 `ElasticsearchConverter` 对象: ```java @Configuration public class ElasticsearchConfig { @Bean public ElasticsearchRestTemplate elasticsearchTemplate(RestHighLevelClient client, ElasticsearchConverter converter) { return new ElasticsearchRestTemplate(client, converter); } @Bean public ElasticsearchConverter elasticsearchConverter() { MappingElasticsearchConverter converter = new MappingElasticsearchConverter(elasticsearchMappingContext()); converter.setTypeMapper(new DefaultElasticsearchTypeMapper(null)); return converter; } @Bean public ElasticsearchMappingContext elasticsearchMappingContext() { return new ElasticsearchMappingContext(); } } ``` 2. 在 `ElasticsearchMappingContext` 配置 dynamic templates: ```java @Configuration public class ElasticsearchConfig { @Bean public ElasticsearchMappingContext elasticsearchMappingContext() { ElasticsearchMappingContext context = new ElasticsearchMappingContext(); context.setInitialEntitySet(getInitialEntitySet()); context.setSimpleTypeHolder(customSimpleTypeHolder()); context.setApplicationContext(applicationContext); // 配置 dynamic templates context.setDynamicTemplates(Arrays.asList( new DynamicTemplate("camel_case_to_underscore", new MappingBuilder() .match("*") .unmatch("*_text") .mapping(new ObjectMapping() .setType("text") .setNormalizer("lowercase") .addProperty("type", "keyword") .addProperty("fields", new ObjectMapping() .addProperty("raw", new ObjectMapping() .setType("keyword") ) ) ) ) )); return context; } } ``` 上述代码,我们创建了一个为 `camel_case_to_underscore` 的 dynamic template,并将其应用到所有字段上(使用 `match("*")`),但排除了以 `_text` 结尾的字段(使用 `unmatch("*_text")`)。对于所有匹配的字段,我们都将其映射为 `text` 类型,使用 `lowercase` 正则表达式将其转换为小写,同时添加了一个 `raw` 子字段,用于排序和聚合操作。 这样,我们就完成了将 Java 的驼峰命转换为 Elasticsearch 的下划线命的操作。在使用 `ElasticsearchRestTemplate` 进行 CRUD 操作时,会自动将 Java 对象转换为 Elasticsearch 的文档,并将字段映射为下划线命
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值