Search code examples
javahibernatelucenehibernate-search

Lucene query on @indexedEmbedded objects id


Im trying to use a org.apache.lucene.search.Query to fetch all entries with a Object @indexedEmbedded with a certain @id id. This simply does not work with my current code. My code as follows:

The Search

FullTextSession fullTextSession = getFullTextSession();
QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Request.class).get();

org.apache.lucene.search.Query query = queryBuilder.keyword()
                .onField("keyword.id")
                .matching(keywordId)
                .createQuery();

FullTextQuery fullTextQuery = fullTextSession
                .createFullTextQuery(query, Request.class);

return fullTextQuery.list();

Request.class

@Entity
@Indexed
public class Request etc etc..

    @ManyToOne
    @IndexedEmbedded
    private Keyword keyword;

Keyword.class

@Entity
@Indexed
public class Keyword etc etc..

    @Id
    @GeneratedValue
    private Long id;

I don't receive any errors when executing this code, the result just isn't restricted to the Request objects with a keyword with the supplied id.

I use this in junction with a org.apache.lucene.search.BooleanQuery later on, but even when trying this itself it doesn't work. I'm aware that this might be the wrong approach so any suggestions would be appreciated.

Thanks!


Solution

  • The indexing properties was not set properly, the index of the request class was simply not indexed as it should have been. Added following code to fix the misstake:

    <property name="hibernateProperties">
        <props>
            ...
            <prop key="hibernate.search.default.indexBase">/var/lucene/indexes</prop>
            <prop key="hibernate.search.default.directory_provider">filesystem</prop>
            <prop key="hibernate.search.lucene_version">LUCENE_CURRENT</prop>
        </props>
    </property>