简体   繁体   中英

polymorphic queries in hibernate search

I try to simplify my question, I have class A :

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class A {

    @Field
    private String a;
    ....
}

and class B which extends class A:

@Entity 
@Indexed
public class B extends A {


    @Field
    private String b;
    ....
}

another class C which extends A :

@Entity 
@Indexed
public class C extends A {


    @Field
    private String c;
    ....
}

My problem is, when I do polymorphic queries like :

Query query = queryBuilder.keyword().onField("a").matching(a).createQuery();

It works very fine, no problem, but when the field is b or c like :

Q uery query = queryBuilder.keyword().onField("b").matching(b).createQuery();

I have an exception : org.hibernate.search.SearchException: Unable to find field b in C Is there any solution to this, or should I do non polymorphic queries ? May be I have a mistake somewhere. Thanks for helping.

You are basically hitting a limitation of the query DSL. At the moment it only supports the creation of queries for a single indexed type. See also HSEARCH-1851 .

As a workaround you can revert to writing native Lucene queries. Polymorphic queries should work.

If you want to use the DSL you need to create a QueryBuilder instance for the specific subclass you want to target.

For those having the same problem, use lucene TermQuery :

org.apache.lucene.search.TermQuery termQuery = new TermQuery("field", "term"));
luceneBooleanQuery.add(termQuery, BooleanClause.Occur.MUST);

if a field is numeric (int, double, ...) use lucene NumericRangeQuery :

NumericRangeQuery<Double> numericQuery = NumericRangeQuery.newDoubleRange("numericField", minRange, maxRange,  true, true);
luceneBooleanQuery.add(numericQuery, BooleanClause.Occur.MUST);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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