Hello,
I’ve a field defined this way:
@Column(name = "NBS_NAME", length = 256)
@KeywordField(searchable = Searchable.YES, sortable = Sortable.YES, projectable = Projectable.NO,
valueBridge = @ValueBridgeRef(type = LowerCaseValueBridge.class))
protected String name;
But when I try to search by this field:
Sort sort = new Sort(new SortField(sortField, SortField.Type.STRING, reverse));
searchSession
.search(Arrays.asList(NodeDocument.class))
.extension(LuceneExtension.get())
.select(f -> f.composite(
f.score(),
f.composite(EntityReference::id, f.entityReference()),
f.composite(EntityReference::type, f.entityReference()),
f.field("tenant")))
.where(f -> f.fromLuceneQuery(query))
.sort(lsf -> lsf.fromLuceneSort(sort))
.fetch(offset, limit);
An error is thrown:
unexpected docvalues type SORTED_SET for field 'name' (expected=SORTED). Re-index with correct docvalues type.
Any tip?
Hey,
My tip would be to use the sort DSL, which will spare you that kind of problems by hiding the Lucene specifics:
searchSession
.search(Arrays.asList(NodeDocument.class))
.extension(LuceneExtension.get())
.select(f -> f.composite(
f.score(),
f.composite(EntityReference::id, f.entityReference()),
f.composite(EntityReference::type, f.entityReference()),
f.field("tenant")))
.where(f -> f.fromLuceneQuery(query))
.sort(lsf -> lsf.field(sortField).order( reverse ? SortOrder.DESC : SortOrder.ASC ))
.fetch(offset, limit);
If you really want to do Lucene…
Just know Hibernate Search always uses SortedSet docvalues for text fields, not the simple (single-valued) “sorted” doc values. I don’t remember why, it was probably because “sorted” docvalues don’t bring much and make it harder to change the cardinality of a field.
I’m afraid you’ll have to dig into the internals of Hibernate Search to see how sorts are actually implemented.
Yes, using the sort DSL seems to work.
Thanks!