elasticsearch - Spring Data Elasticsearch 的@Field 注释不起作用

标签 elasticsearch spring-data spring-data-elasticsearch

我在 pom.xml 中有一个带有 Spring Data Elasticsearch 插件的 Spring Boot 应用程序。我创建了一个我想索引的文档类:

@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 Elastic Search 的错误还是我做错了什么?

我试了spring boot提供的稳定版和spring-data-elasticsearch的最后一个snapshot。我还尝试了插件提供的嵌入式 Elasticsearch 服务器和当前版本的外部服务器。我总是得到相同的结果。

最佳答案

我终于可以复制并解决问题了。事实上,我使用 ElasticTemplate 来索引和搜索文档而不是存储库,因为我的业务逻辑变得更加复杂(使用聚合等)。

之后,我删除了未使用的 OperationDocumentRespository。似乎需要存储库才能在启动时将类型映射发布到 ES 服务器。我认为拥有 @Document 类就足够了,但事实并非如此。

所以我们在这里有两个选择:

  • 保留 OperationDocumentRepository
  • 将此行添加到应用程序启动:

    elasticsearchTemplate.putMapping(OperationDocument.class);
    

关于elasticsearch - Spring Data Elasticsearch 的@Field 注释不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29496081/

相关文章:

java - 找不到类型用户的属性索引

java - 向 Spring Data Repository 添加自定义功能的问题

elasticsearch - Spring Data支持的ElasticSearch版本

elasticsearch - 如何集成 Spring Data Elasticsearch(存储库接口(interface))和模板查询?

elasticsearch - 将用户数据与ElasticSearch混合

groovy - Elasticsearch : _score always 0 in Groovy script

java - 使用 spring-data-mongodb 持久化包含对象的对象

elasticsearch - 如何保留某些索引比其他索引更多的时间?

c# - 如何使用NEST执行子聚合?

elasticsearch - Splunk-Elasticsearch集成