我試圖向現有索引(用戶)添加一個新欄位(bioAuto)。我有這個索引的 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;
}
欄位已創建,但型別錯誤,它與我的自定義分析器(在 中定義elastic/autocomplete_settings.json)無關。
創建的欄位型別:
"bioAuto" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
右欄位型別:
"bioAuto" : {
"type" : "text",
"analyzer" : "autocomplete_whitespace_only",
"search_analyzer" : "autocomplete_search"
},
如果我重新創建我的索引(洗掉用戶索引并再次運行該應用程式),一切正常。ElasticCloud:7.9.3,Spring 資料版本:4.2.6
PS如果我執行這個命令,一切也正常,Spring Data Elastic中使用正確型別自動創建欄位的問題
PUT /users/_mapping
{
"properties": {
"bioAuto" : {
"type" : "text",
"analyzer" : "autocomplete_whitespace_only",
"search_analyzer" : "autocomplete_search"
}
}
}
uj5u.com熱心網友回復:
Spring Data Elasticsearch 僅在應用程式啟動時自動為索引撰寫映射,并且該索引ElasticsearchRepository的物體發現該索引不存在。否則不會對索引自動執行任何操作。映射不會被重寫。因此,如果您向物體添加一些屬性,即使您有注釋,這也不會創建新的映射。
您可以做的是 - 在添加屬性之后和使用此新屬性插入新資料之前- 使用該IndexOperations.putMapping()方法撰寫更新的映射。重要提示:您只能向索引添加新的屬性/欄位,而不能洗掉或更新現有的;這是 Elasticsearch 的限制。
要了解如何在應用程式啟動時自動完成此操作,請參閱我對這個 SO 問題的回答:Spring Data Elasticsearch : 檢測映射差異
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/372281.html
