简体   繁体   English

Spring 数据 ElasticSearch (7.9.3) 将字段添加到现有索引

[英]Spring Data ElasticSearch (7.9.3) add field to existed index

I trying to add a new field (bioAuto) to existed index (users).我试图向现有索引(用户)添加一个新字段(bioAuto)。 I have POJO for this index:我有这个索引的 POJO:

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import org.springframework.data.elasticsearch.annotations.Setting;

import java.time.LocalDateTime;

@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Document(indexName = IndexName.USERS)
@Setting(settingPath = "elastic/autocomplete_settings.json")
public class User {

    @Id
    private Long id;

    @Field(type = FieldType.Search_As_You_Type)
    private String userName;

    @Field(type = FieldType.Text, analyzer = "autocomplete_whitespace_only", searchAnalyzer = "autocomplete_search")
    private String bioAuto;

}

Field created, but type is wrong, it doesn't connected with my custom analyzers (defined in elastic/autocomplete_settings.json ).字段已创建,但类型错误,它没有与我的自定义分析器连接(在elastic/autocomplete_settings.json中定义)。

Created field type:创建的字段类型:

"bioAuto" : {
      "type" : "text",
      "fields" : {
        "keyword" : {
          "type" : "keyword",
          "ignore_above" : 256
        }
      }
    }

Right field type:右字段类型:

"bioAuto" : {
      "type" : "text",
      "analyzer" : "autocomplete_whitespace_only",
      "search_analyzer" : "autocomplete_search"
    },

If I re-create my index (remove users index and run the application one more time), everything works fine.如果我重新创建索引(删除用户索引并再次运行应用程序),一切正常。 ElasticCloud: 7.9.3, Spring Data version: 4.2.6 ElasticCloud:7.9.3,Spring 数据版本:4.2.6

PS If I execute this command, everything also works fine, the problem in automatic field creation with right type in Spring Data Elastic PS如果我执行此命令,一切正常,Spring Data Elastic 中使用正确类型自动创建字段的问题

PUT /users/_mapping
{
"properties": {
    "bioAuto" : {
          "type" : "text",
          "analyzer" : "autocomplete_whitespace_only",
          "search_analyzer" : "autocomplete_search"
        }
  }
}

Spring Data Elasticsearch only automatically writes a mapping for an index when on application startup an ElasticsearchRepository with an entity for that index finds that the index does not exist. Spring 数据 Elasticsearch 仅在应用程序启动时自动为索引写入映射,当具有该索引的实体的ElasticsearchRepository发现该索引不存在时。 Otherwise nothing is done automatically on an index.否则不会对索引自动执行任何操作。 The mapping is not rewritten.映射不会被重写。 So if you add some property to your entity even though you have the annotations on it, this does not create a new mapping.因此,如果您向实体添加一些属性,即使您有注释,这也不会创建新的映射。

What you can do is - after adding the property and before inserting new data with this new property - use the IndexOperations.putMapping() method to write an updated mapping.您可以做的是 - 在添加属性之后和使用这个新属性插入新数据之前- 使用IndexOperations.putMapping()方法来编写更新的映射。 Important : you can only add new properties/fields to an index not delete or update existing ones;重要提示:您只能将新属性/字段添加到索引中,而不能删除或更新现有的; this is a restriction from Elasticsearch.这是来自 Elasticsearch 的限制。

To see how this might be done automatically on application startup see my answer to this SO question: Spring Data Elasticsearch: detect mapping differences要了解如何在应用程序启动时自动完成此操作,请参阅我对这个 SO 问题的回答: Spring 数据 Elasticsearch:检测映射差异

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM