我有一个Spring Boot应用程序,其中包含pom.xml中的Spring Data Elasticsearch插件。我创建了一个文档类,希望对其进行索引:
@Document(indexName = "operations", type = "operation")
public class OperationDocument {
@Id
private Long id;
@Field(
type = FieldType.String,
index = FieldIndex.analyzed,
searchAnalyzer = "standard",
indexAnalyzer = "standard",
store = true
)
private String operationName;
@Field(
type = FieldType.Date,
index = FieldIndex.not_analyzed,
store = true,
format = DateFormat.custom, pattern = "dd.MM.yyyy hh:mm"
)
private Date dateUp;
@Field(
type = FieldType.String,
index = FieldIndex.not_analyzed,
store = false
)
private String someTransientData;
@Field(type = FieldType.Nested)
private List<Sector> sectors;
//Getter and setters
我也创建了一个该课程的代码库:
public interface OperationDocumentRepository
extends ElasticsearchRepository<OperationDocument, Long> {
}
我做了一个测试,使用存储库索引了三个示例对象。它相当长,所以只有在必要时才会发布它。事实上,ES服务器中创建的映射忽略了@Field注释设置的配置:
"mappings": {
"operation": {
"properties": {
"operationName": {
"type": "string"
},
"dateUp": {
"type": "long"
},
"someTransientData": {
"type": "string"
},
"sectors": {
"properties": {
"id": {
"type": "long"
},
"sectorName": {
"type": "string"
}
}
}
}
}
}
没有有关分析器的信息,"someTransientData"被存储和索引,而dateUp的类型为Long而不是Date。
直接从服务器请求的示例文档:
{
"_index": "operations",
"_type": "operation",
"_id": "AUyUk2cY3nXeOFxdOlQW",
"_version": 1,
"_score": 1,
"_source": {
"id": null,
"operationName": "Second Operation Name",
"dateUp": 1428421827091,
"someTransientData": "Do not index or store",
"sectors": [
{
"id": 2,
"sectorName": "Health Care"
},
{
"id": 3,
"sectorName": "Construction"
}
]
}
}
我还注意到,当我第二次运行应用程序时,在启动时,只有在索引已经存在时才会打印出以下错误消息:
ERROR 19452 --- [main] .d.e.r.s.AbstractElasticsearchRepository : failed to load elasticsearch nodes : org.elasticsearch.index.mapper.MergeMappingException: Merge failed with failures {[mapper [someTransientData] has different index values, mapper [someTransientData] has different tokenize values, mapper [someTransientData] has different index_analyzer, object mapping [sectors] can't be changed from non-nested to nested, mapper [operationName] has different store values, mapper [operationName] has different index_analyzer, mapper [dateUp] of different type, current_type [long], merged_type [date]]}
这是 Spring Data Elasticsearch 的一个 bug 还是我的操作有误?
我尝试了 Spring Boot 提供的稳定版本和 spring-data-elasticsearch 的最新快照版本。我还尝试了插件提供的嵌入式 Elasticsearch 服务器和当前版本的外部服务器,但结果始终相同。