简体   繁体   English

Hibernate-search:通过在 hibernate-search 中以编程方式注册字段,基类字段未在弹性搜索中注册

[英]Hibernate-search: Base class fields are not registered in elastic-search by registering the fields programmatically in hibernate-search

I have certain fields declared in base class and I want to register those fields for certain sub classes only (entities).我在基类中声明了某些字段,我只想为某些子类(实体)注册这些字段。

So that I don't want to annotate those fields in base class by @Field and though just registering programmatically for certain entities is enough.所以我不想通过@Field 注释基类中的这些字段,尽管仅以编程方式为某些实体注册就足够了。

But fields declared in the base entity are not registered/mapped to elastic-search and if try to search then it throws below exception.但是在基本实体中声明的字段未注册/映射到弹性搜索,如果尝试搜索,则会引发以下异常。

I have also tried by moving those field to entities itself instead of declaring it in the base class and registering those fields programmatically works for me, so only if the fields are in base class doesn't work.我还尝试通过将这些字段移动到实体本身而不是在基类中声明它并以编程方式注册这些字段对我有用,因此只有当字段在基类中时才不起作用。

Here is my sample code这是我的示例代码

public class BaseEntity{

@Column(name = "created_timestamp")
private String createdTimeStamp;

@Column(name = "created_by")
private ZonedDateTime createdBy;

//getter and setter

}
@Entity
@Indexed    
public class BookEntity extends BaseEntity{

//other fields
}
@Entity
@Indexed
public class PaperBookEntity extends BaseEntity{

//other fields
}
public class HibernateSearchFieldMappingService{

@Autowired
private SearchMapping searchMapping;

 @Override
  public <T extends BaseEntity> void registerAuditFields(Class<T> entityClass) {
    LOG.info("Registering audit fields (createdTimeStamp and createdBy) of entity {}", entityClass);
    IndexedMapping indexedMapping = searchMapping.entity(entityClass).indexed();
    FieldMapping fieldMapping = indexedMapping.property("createdTimeStamp", ElementType.FIELD).field();
    fieldMapping.dateBridge(Resolution.SECOND);
    fieldMapping.sortableField();
    indexedMapping.entity(entityClass).indexed().property("createdBy", ElementType.FIELD).field().normalizer("lowercase");
  }

  }

Here is my stack trace这是我的堆栈跟踪

org.hibernate.search.exception.SearchException: Unable to find field createdBy in com.*.*.*.BookEntity
    at org.hibernate.search.engine.spi.DocumentBuilderIndexedEntity.objectToString(DocumentBuilderIndexedEntity.java:1052)
    at org.hibernate.search.query.dsl.impl.FieldContext.objectToString(FieldContext.java:75)
    at org.hibernate.search.query.dsl.impl.ConnectedMultiFieldsTermQueryBuilder.buildSearchTerm(ConnectedMultiFieldsTermQueryBuilder.java:149)
    at org.hibernate.search.query.dsl.impl.ConnectedMultiFieldsTermQueryBuilder.createQuery(ConnectedMultiFieldsTermQueryBuilder.java:113)
    at org.hibernate.search.query.dsl.impl.ConnectedMultiFieldsTermQueryBuilder.createQuery(ConnectedMultiFieldsTermQueryBuilder.java:72)

Hibernate versions that I use are,我使用的休眠版本是,

hibernate-search-elasticsearch, hibernate-search-orm = 5.11.4.Final休眠搜索弹性搜索,休眠搜索orm = 5.11.4.Final

The programmatic API in Hibernate Search 5 does not recognize "inherited" properties, only properties declared directly on the type you're mapping. Hibernate Search 5 中的编程 API 不识别“继承”属性,只能识别直接在您映射的类型上声明的属性。 See HSEARCH-1108 .HSEARCH-1108

The problem has been solved in Hibernate Search 6, so you could solve it by upgrading.这个问题在Hibernate Search 6 中已经解决了,所以你可以通过升级来解决它。 Be aware that Hibernate Search 6 is in Beta and APIs are quite different.请注意,Hibernate Search 6 处于 Beta 阶段,API 非常不同。 See here for a getting started guide.请参阅此处获取入门指南。

To make this work in Hibernate Search 5 without upgrading to 6, the usual solution is not to use the programmatic API, but to override getters in classes where you want the fields to appear, and annotate the overridden getters.为了在不升级到 6 的情况下在 Hibernate Search 5 中完成这项工作,通常的解决方案不是使用编程 API,而是在您希望字段出现的类中覆盖 getter,并注释覆盖的 getter。

For example:例如:

@MappedSuperclass
public class BaseEntity{

@Column(name = "created_by")
private String createdBy;

@Column(name = "created_timestamp")
private ZonedDateTime createdTimeStamp;

public String getCreatedBy() {
    return createdBy;
}

public String getCreatedTimeStamp() {
    return createdTimeStamp;
}
}
@Entity
@Indexed    
public class BookEntity extends BaseEntity{

@Override
@Field(normalizer = @Normalizer(definition = "lowercase"))
public String getCreatedBy() {
    return super.getCreatedBy();
}

@Override
@Field
@SortableField
public String getCreatedTimeStamp() {
    return createdTimeStamp;
}
}
@Column(name = "created_timestamp")
private String createdBy;

@Column(name = "created_by")
private ZonedDateTime createdTimeStamp;

Shouldn't it be不应该是

@Column(name = "created_timestamp")
private String createdTimeStamp;;

@Column(name = "created_by")
private ZonedDateTime createdBy

Another assumption **Column name is created_by instead createdBy另一个假设 **列名是created_by而不是createdBy

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

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