Native Lucene search - no fields in TopDocs

Could you expaint me why IndexReader returns me the documents with fields = 0?

Im using the version 6.1.2.Final

Thanks a lot.

I moved your message to a new topic.

Please don’t revive old topics of other people with a question of your own: just create a new topic.

As to your question… Only stored fields are returned by IndexSearcher#doc/IndexReader#document. See the javadoc of IndexReader#document:

NOTE: only the content of a field is returned, if that field was stored during indexing.

You need to make sure that the content of fields is stored during indexing, which in the case of Hibernate Search means marking fields as projectable (@GenericField(projectable = Projectable.YES)). Of course you will need to reindex for these changes to take effect.

Also, you’re doing native Lucene stuff, and that’s going to get more complex as you run into corner cases. For example you won’t be able to (easily) retrieve the value of fields in nested documents.

I’d suggest you use the more user-friendly Hibernate Search APIs instead?

EntityManager em = ...;
QueryBuilder queryBuilder = new QueryBuilder( new StandardAnalyzer() );
Query query1 = queryBuilder.createBooleanQuery( "title", term );
List<Document> documents = Search.session( em ).search( Product.class )
        .extension( LuceneExtension.get() )
        .select( f -> f.document() )
        .where( f -> f.fromLuceneQuery( query1 ) )
        .fetch( 10 );

That requires an EntityManager, which you seem to be trying to avoid, but I’m not sure you should: with most configurations, instantiating an EntityManager is almost neutral performance-wise as long as you don’t access the database, since the JDBC connection is retrieved on first access to the database (JPA query, persist, flush, …).

1 Like

Problem solved, thanks. :+1: