jquery - Hibernate Search:如何查询父类中的字段?

标签 jquery hibernate elasticsearch querydsl hibernate-search

我正在尝试使用Hibernate Search进行查询。
我想匹配一个从父级扩展的类中的字段(子类是@Indexed一个)。
到目前为止,这是我所做的。
-价格是索引的类别-

@Indexed
@Table(name = "price_products")
public class PriceEntity extends AbstractPrice {

    .....
    .....
    .....

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "productId")
    private ECommerceProductEntity parent;
    

    public ProductEntity getParent() {
        return parent;
    }

    public void setParent(ProductEntity parent) {
        this.parent = parent;
    }

    
}
-产品是父类-
@Entity
@Indexed
@Table(name = "products")
public class ProductEntity extends AbstractProduct {


    @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
    private List<PriceEntity> prices;

    /.others fields../


    @Field(termVector = TermVector.YES)
    @Size(max = 255)
    private String tags;

    /.others getter and setters../
    

    public String getTags() {
        return tags;
    }

    public void setTags(String tags) {
        this.tags = tags;
    }

    public List<PriceEntity> getPrices() {
        return prices;
    }

    public void setPrices(PriceEntity> prices) {
        this.prices = prices;
    }

    public void addPrice(ECommercePriceEntity price) {
        ...
    }

}
-AbstractProduct由产品扩展-
@MappedSuperclass
public abstract class AbstractProduct {
    /.others fields../
    @NotBlank
    @Field(termVector = TermVector.YES)
    private String description;
    /getter end setters/
}
-查询-
FullTextEntityManager fullTextEntityManager
        = Search.getFullTextEntityManager(entityManager);
fullTextEntityManager.createIndexer().startAndWait();

FullTextEntityManager fullTextEntityManager2
        = Search.getFullTextEntityManager(entityManager);

QueryBuilder queryBuilder = fullTextEntityManager2.getSearchFactory()
        .buildQueryBuilder()
        .forEntity(PriceEntity.class)
        .get();

Query myQuery = queryBuilder
        .bool()
        .should(queryBuilder.keyword().withConstantScore()
                .onField("products.description").boostedTo(9l).matching(query)
                .createQuery())
        .should(queryBuilder.phrase().withConstantScore()
                .onField("products.description").boostedTo(5l).sentence(query)
                .createQuery())

        .should(queryBuilder.keyword().withConstantScore()
                .onField("products.tags").boostedTo(3l).matching(query)
                .createQuery())
        .should(queryBuilder.phrase().withConstantScore()
                .onField("products.tags").boostedTo(1l).sentence(query)
                .createQuery())
        .createQuery();

org.hibernate.search.jpa.FullTextQuery jpaQuery
        = fullTextEntityManager2.createFullTextQuery(myQuery, PriceEntity.class);
jpaQuery.setProjection("description");

List<PriceEntity> queryResults = jpaQuery.getResultList();
与上面的代码我得到以下错误
org.hibernate.search.exception.SearchException: Unable to find field products.description in ... 
如果给queryBuilder的给定实体是PriceEntity,如何通过字段“描述”(在AbstractProduct中)和标签(在ProductEntity中)进行查询?
提前致谢

最佳答案

您应该使用@IndexedEmbedded

@Indexed
@Table(name = "price_products")
public class PriceEntity extends AbstractPrice {

    .....

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "productId")
    @IndexedEmbedded // <= ADD THIS
    private ECommerceProductEntity parent;
@Entity
@Indexed
@Table(name = "products")
public class ProductEntity extends AbstractProduct {


    @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
    @ContainedIn // <= ADD THIS
    private List<PriceEntity> prices;
然后重新索引所有数据。
然后使用“ parent ”。如果要访问产品字段,则在查询价格时添加前缀:
FullTextEntityManager fullTextEntityManager
        = Search.getFullTextEntityManager(entityManager);
fullTextEntityManager.createIndexer().startAndWait();

FullTextEntityManager fullTextEntityManager2
        = Search.getFullTextEntityManager(entityManager);

QueryBuilder queryBuilder = fullTextEntityManager2.getSearchFactory()
        .buildQueryBuilder()
        .forEntity(PriceEntity.class)
        .get();

Query myQuery = queryBuilder
        .bool()
        .should(queryBuilder.keyword().withConstantScore()
                .onField("parent.description").boostedTo(9l).matching(query)
                .createQuery())
        .should(queryBuilder.phrase().withConstantScore()
                .onField("parent.description").boostedTo(5l).sentence(query)
                .createQuery())

        .should(queryBuilder.keyword().withConstantScore()
                .onField("parent.tags").boostedTo(3l).matching(query)
                .createQuery())
        .should(queryBuilder.phrase().withConstantScore()
                .onField("parent.tags").boostedTo(1l).sentence(query)
                .createQuery())
        .createQuery();

org.hibernate.search.jpa.FullTextQuery jpaQuery
        = fullTextEntityManager2.createFullTextQuery(myQuery, PriceEntity.class);
jpaQuery.setProjection("parent.description");

List<PriceEntity> queryResults = jpaQuery.getResultList();
参见this section of the documentation

关于jquery - Hibernate Search:如何查询父类中的字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62691860/

相关文章:

javascript - 在 JavaScript/jQuery 中更新 CSS

elasticsearch - Elasticsearch术语查询作为子句中的mysql?

elasticsearch - elasticsearch.yml:配置文件会实时写入吗?

java - JPA 2.1 StoredProcedureQuery 与 PostgreSQL 和 REF_CURSORs

elasticsearch - 没有在字段ElasticSearch上声明类型[geo-point]的处理程序

javascript - 为什么模态框不会慢慢淡出?

javascript - 使用 Chartist.js 如何更改圆环图笔划的颜色?

jquery - 渲染响应不适用于 jquery ajax 请求

hibernate - Grails 按长度排序

java - 使用涉及日期限制的 JPA2 通过 EntityManger 重写 Hibernate SessionFactory 查询