Hi there!
I have a Boolean field defined this way:
@GenericField(searchable = Searchable.YES, projectable = Projectable.YES)
private Boolean hasAttachments = Boolean.FALSE;
It seems to be indexed, according to Luke:
But I’m not able to search using this field. For example, this Lucene query does not return any result:
hasAttachments:0
So I wonder if I’m doing something wrong or I would need to implement a custom bridge to handle this field type.
Thanks!
Hey
Boolean fields should work just fine. You could search by them like:
List<Smth> result = searchSession.search( Smth.class )
.where( f -> f.match().field( "hasAttachments" )
.matching( false ) )
.fetchHits( 20 );
Where were you trying to run that Lucene query from?
Yes, this work. The problem is when I build a Lucene query, which does not return any result:
QueryParser qp = new QueryParser("text", new WhitespaceAnalyzer());
Query query = qp.parse("hasAttachments: true");
SearchResult<NodeMail> result = searchSession.search(NodeMail.class)
.extension(LuceneExtension.get())
.where(f -> f.fromLuceneQuery(query))
.fetch(20);
Ohh ok,
sooo if you try looking at the actual resulting query from:
Query query = qp.parse("hasAttachments: 0");
you’ll see that the parser produces a terms query (TermQuery
) but since it’s an int field what you really want to get there is an PointRangeQuery
… (the query that will get created when using the dsl).
with that said… if using provided predicates does not cover your needs, you can try creating a custom query parser that behaves differently for the numeric fields, and instead of terms query creates a point range one. We’ve added support of numeric and date fields in query string predicates with [HSEARCH-4558] - Hibernate JIRA, but it is only going to be available with Hibernate Search 7.2
Or alternatively you can go with the bridge as you’ve suggested in your original question. See this example from the documentation where booleans are indexed as strings.
Ok, many thanks for the info.