映射注释有什么用,我需要它吗?(Spring Data ElasticSearch)
What's the use of a mapping annotation and do I need it?(Spring Data ElasticSearch)
我正在使用 Spring Data ElasticSearch 3.1.0.RELEASE 与 ElasticSearch 合作,我是新手对 ElasticSearch 本身了解不多。
Here(spring-data-elastic docs) I see that the mappings(schema) for the documents are auto-generated using metadata(annotations) very much the same way as in the Spring Data MongoDB in a dynamic way, but in our organization, all entities are annotated with @Mapping 注释并引用 JSON 文档,这反映了它们的结构,因此对于每个文档实体 - JSON 文件被写入,尽管所有实体都有各自的注释。
一小段示例class 提示我在说什么
@Document(indexName = "sampleIndex", type = "sample", shards = 16, createIndex = false)
@Mapping(mappingPath = "/elasticsearch/mappings/sample.json")
public final class Sample {
@Id
private String id;
@Field(type = FieldType.Long)
private long sampleId;
@Field(type = FieldType.Keyword)
private SampleObject sampleObject;
@Field(type = FieldType.Nested)
private Map<String, String> data;
以及各自的/elasticsearch/mappings/sample.json
文件
{
"samples": {
"mappings": {
"sample": {
"properties": {
"sampleId": {
"type": "long"
},
"sampleObject": {
"type": "string"
},
"data": {
"type": "nested"
},
....
虽然我在 (spring-data-elastic docs) and I don't see any meaningful JavaDocs.
中甚至没有看到任何提及,但我大概能理解这个注释背后的整个想法
无论如何,我仍然不明白这个注释是如何工作的,有什么用例以及为什么需要它,从我的角度来看,我会完全删除它,因为模式可以基于自动生成在 Spring 的其他注释上。还是我错了?
与问题相关的其他要点:我对此也不确定:此映射 JSON 文件是否覆盖实体中通过注释定义的结构?
ElasticsearchOperations
接口有一个方法 putMapping(class<?>)
。此方法可用于将索引映射写入索引。默认的非反应性存储库实现在创建索引时执行此操作。
默认方法实现检查 class 上是否有 @Mapping
注释。如果是,则使用此映射定义。如果此注释不存在,则检查 class 并检查属性上的 @Field
注释。
因此在您的情况下,属性上的注释不用于编写索引映射。
我建议使用 class 上的属性,因为您更有可能在 class 中更改某些映射 属性 而在 json 中忘记它] 文件。
例如,在您的代码中,class sampleObject
被定义为 keyword
但在映射中它是一个字符串。只看代码的人可能会错过不同的定义。
我正在使用 Spring Data ElasticSearch 3.1.0.RELEASE 与 ElasticSearch 合作,我是新手对 ElasticSearch 本身了解不多。
Here(spring-data-elastic docs) I see that the mappings(schema) for the documents are auto-generated using metadata(annotations) very much the same way as in the Spring Data MongoDB in a dynamic way, but in our organization, all entities are annotated with @Mapping 注释并引用 JSON 文档,这反映了它们的结构,因此对于每个文档实体 - JSON 文件被写入,尽管所有实体都有各自的注释。
一小段示例class 提示我在说什么
@Document(indexName = "sampleIndex", type = "sample", shards = 16, createIndex = false)
@Mapping(mappingPath = "/elasticsearch/mappings/sample.json")
public final class Sample {
@Id
private String id;
@Field(type = FieldType.Long)
private long sampleId;
@Field(type = FieldType.Keyword)
private SampleObject sampleObject;
@Field(type = FieldType.Nested)
private Map<String, String> data;
以及各自的/elasticsearch/mappings/sample.json
文件
{
"samples": {
"mappings": {
"sample": {
"properties": {
"sampleId": {
"type": "long"
},
"sampleObject": {
"type": "string"
},
"data": {
"type": "nested"
},
....
虽然我在 (spring-data-elastic docs) and I don't see any meaningful JavaDocs.
中甚至没有看到任何提及,但我大概能理解这个注释背后的整个想法无论如何,我仍然不明白这个注释是如何工作的,有什么用例以及为什么需要它,从我的角度来看,我会完全删除它,因为模式可以基于自动生成在 Spring 的其他注释上。还是我错了?
与问题相关的其他要点:我对此也不确定:此映射 JSON 文件是否覆盖实体中通过注释定义的结构?
ElasticsearchOperations
接口有一个方法 putMapping(class<?>)
。此方法可用于将索引映射写入索引。默认的非反应性存储库实现在创建索引时执行此操作。
默认方法实现检查 class 上是否有 @Mapping
注释。如果是,则使用此映射定义。如果此注释不存在,则检查 class 并检查属性上的 @Field
注释。
因此在您的情况下,属性上的注释不用于编写索引映射。
我建议使用 class 上的属性,因为您更有可能在 class 中更改某些映射 属性 而在 json 中忘记它] 文件。
例如,在您的代码中,class sampleObject
被定义为 keyword
但在映射中它是一个字符串。只看代码的人可能会错过不同的定义。