spring 如何为所有字段生成Map?

k7fdbhmy  于 2022-11-21  发布在  Spring
关注(0)|答案(1)|浏览(135)

我有一个Person类:

@Document(indexName = "person")
@Data
@EqualsAndHashCode(callSuper = true)
public class Person extends BaseEntity implements Serializable {

  @Field(type=FieldType.Keyword)
  private String firstName;

  @Field(type=FieldType.Keyword)
  private String lastName;

  @MultiField(
      mainField = @Field(type = FieldType.Keyword),
      otherFields = {
          @InnerField(type = FieldType.Text, suffix = "ngrams", analyzer = "ik_max_word", searchAnalyzer = "ik_smart")
      })
  private String fullName;

  @Field
  private String maidenName;
}

我有一个在启动时创建索引的现有代码:

final IndexOperations indexOperations = this.elasticsearchOperations.indexOps(clazz);
      indexOperations.putMapping();

现在,我有一个需求,从它生成Map,并创建Map一次。有人能帮助我如何将它与现有的代码集成,以包括字段的Map,使他们成为静态的?

eh57zj3b

eh57zj3b1#

只需检查是否需要创建索引,如果需要,则创建索引并写入Map:

indexOperations = operations.indexOps(entityClass);

if (!indexOperations.exists()) {
    indexOperations.createWithMapping();
}

相关问题