【问题标题】:@IndexedEmbedded on class containing other @IndexedEmbedded fields@IndexedEmbedded 在包含其他 @IndexedEmbedded 字段的类上
【发布时间】:2021-11-21 15:20:23
【问题描述】:

我正在尝试管理一个类的休眠搜索索引,该类的字段由@IndexedEmbedded 在自定义@Embeddable 实体上映射。该实体还包含@MappedSuperclass 中的其他@IndexedEmbedded 字段。 这些是涉及的实体:

@Builder
@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Indexed
@GeoPointBinding(fieldName = "location") 
public class Insertion {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "INSERTION_SEQ")
    private Long id;

    @NotNull
    @GenericField
    private Boolean publicated;

    @NotNull
    @Latitude
    private Double latitude;

    @NotNull
    @Longitude
    private Double longitude;

    @JsonIgnore
    private Point location;

    @FullTextField(analyzer = "generic_text")
    @KeywordField(name="city_sort", sortable = Sortable.YES, normalizer = "sort")
    private String city;

    @NotNull
    @Embedded
    @Valid
    @IndexedEmbedded
    private Amount amount;
    
    @IndexedEmbedded(name = "insertion_mate_preferences")
    @Embedded
    @Valid
    private MatePreference matePreferences;

}

还有这些类:

@SuperBuilder
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Embeddable
public class MatePreference extends BaseProfile {
    
    @NotNull
    @GenericField
    private Integer minAge;
    
    @NotNull
    @GenericField
    private Integer maxAge;
    
}
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
@MappedSuperclass
public abstract class BaseProfile implements BaseProfileView {

    @GenericField
    private Boolean smoker;

    @GenericField
    private Boolean children;

    @GenericField
    private Boolean hasAnimals;

    @GenericField
    private Boolean student;

    @GenericField
    private Boolean employed;
    
    @GenericField
    private String animalsDescription;
    
    @Enumerated(EnumType.STRING)
    @GenericField
    private Genre genre;
    
    @Enumerated(EnumType.STRING)
    @GenericField
    private OccupationSector occupation;
    
    @Enumerated(EnumType.STRING)
    @ElementCollection(fetch = FetchType.EAGER)
    @GenericField
    @IndexedEmbedded
    private Set<Personality> personalities;

    @Enumerated(EnumType.STRING)
    @ElementCollection(fetch = FetchType.EAGER)
    @GenericField
    @IndexedEmbedded
    private Set<Lifestyle> lifestyles;
    
    @ElementCollection(fetch = FetchType.EAGER)
    @GenericField
    @IndexedEmbedded
    private Set<Language> languages;
    
    @Enumerated(EnumType.STRING)
    @GenericField
    private StudyTitle titleOfStudy;
    
    @Enumerated(EnumType.STRING)
    @GenericField
    private StudyField studyField;
}

运行应用程序时,Hibernate Search 会抛出以下错误:

org.hibernate.search.util.common.SearchException: HSEARCH000520: Hibernate Search encountered failures during bootstrap. Failures:

    Hibernate ORM mapping: 
        type 'it.friendshome.api.common.model.insertion.Insertion': 
            path '.matePreferences<no value extractors>.languages': 
                index 'Insertion': 
                    field 'insertion_mate_preferences': 
                        failures: 
                          - HSEARCH400520: Duplicate index field definition: 'languages'. Index field names must be unique. Look for two property mappings with the same field name, or two indexed-embeddeds with prefixes that lead to conflicting index field names, or two custom bridges declaring index fields with the same name.
            path '.matePreferences<no value extractors>.lifestyles': 
                index 'Insertion': 
                    field 'insertion_mate_preferences': 
                        failures: 
                          - HSEARCH400520: Duplicate index field definition: 'lifestyles'. Index field names must be unique. Look for two property mappings with the same field name, or two indexed-embeddeds with prefixes that lead to conflicting index field names, or two custom bridges declaring index fields with the same name.
            path '.matePreferences<no value extractors>.personalities': 
                index 'Insertion': 
                    field 'insertion_mate_preferences': 
                        failures: 
                          - HSEARCH400520: Duplicate index field definition: 'personalities'. Index field names must be unique. Look for two property mappings with the same field name, or two indexed-embeddeds with prefixes that lead to conflicting index field names, or two custom bridges declaring index fields with the same name.
        type 'it.friendshome.api.common.model.profile.Profile': 
            path '.languages': 
                index 'Profile': 
                    index schema root: 
                        failures: 
                          - HSEARCH400520: Duplicate index field definition: 'languages'. Index field names must be unique. Look for two property mappings with the same field name, or two indexed-embeddeds with prefixes that lead to conflicting index field names, or two custom bridges declaring index fields with the same name.
            path '.lifestyles': 
                index 'Profile': 
                    index schema root: 
                        failures: 
                          - HSEARCH400520: Duplicate index field definition: 'lifestyles'. Index field names must be unique. Look for two property mappings with the same field name, or two indexed-embeddeds with prefixes that lead to conflicting index field names, or two custom bridges declaring index fields with the same name.
            path '.personalities': 
                index 'Profile': 
                    index schema root: 
                        failures: 
                          - HSEARCH400520: Duplicate index field definition: 'personalities'. Index field names must be unique. Look for two property mappings with the same field name, or two indexed-embeddeds with prefixes that lead to conflicting index field names, or two custom bridges declaring index fields with the same name.
        type 'it.friendshome.api.common.model.search.Search': 
            path '.matePreference<no value extractors>.languages': 
                index 'Search': 
                    field 'matePreference': 
                        failures: 
                          - HSEARCH400520: Duplicate index field definition: 'languages'. Index field names must be unique. Look for two property mappings with the same field name, or two indexed-embeddeds with prefixes that lead to conflicting index field names, or two custom bridges declaring index fields with the same name.
            path '.matePreference<no value extractors>.lifestyles': 
                index 'Search': 
                    field 'matePreference': 
                        failures: 
                          - HSEARCH400520: Duplicate index field definition: 'lifestyles'. Index field names must be unique. Look for two property mappings with the same field name, or two indexed-embeddeds with prefixes that lead to conflicting index field names, or two custom bridges declaring index fields with the same name.
            path '.matePreference<no value extractors>.personalities': 
                index 'Search': 
                    field 'matePreference': 
                        failures: 
                          - HSEARCH400520: Duplicate index field definition: 'personalities'. Index field names must be unique. Look for two property mappings with the same field name, or two indexed-embeddeds with prefixes that lead to conflicting index field names, or two custom bridges declaring index fields with the same name.

我使用以下依赖项运行它:

关于如何管理这种情况有什么建议吗?

【问题讨论】:

    标签: java hibernate elasticsearch quarkus hibernate-search


    【解决方案1】:

    此代码不正确:

        @GenericField
        @IndexedEmbedded
        private Set<Language> languages;
    

    使用@GenericField,您要求Hibernate Search 为语言生成一个“值”字段(字符串、整数等)。这通常不起作用,因为 Hibernate Search 不知道如何将 Language 转换为字符串/整数/...,所以这是第一个问题。

    但是第二个问题,也是 Hibernate Search 报告的问题,是通过将@IndexedEmbedded 放在同一个属性上,您还要求 Hibernate Search 生成一个“对象”(复合)字段,@GenericField 中的“值”字段同名。所以有一个冲突:“值”字段和“对象”字段都具有相同的名称,“语言”。

    您应该在使用@IndexedEmbedded 的任何地方删除@GenericField


    请注意,从技术上讲,您可以在同一属性上同时使用 @GenericField@IndexedEmbedded,只要您使用 @GenericField(name = ...)@IndexedEmbedded(name = ...) 在任一注释上明确设置不同的名称.但是除非您使用custom value bridge,否则@GenericField 将无法工作,而且我认为您的意图并不是要这样做。所以请不要这样做,除非你有真正的原因并且@IndexedEmbedded 没有做你想做的事。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-09
      • 1970-01-01
      相关资源
      最近更新 更多