How to sort by LocalDateTime

Hello I have a list of activities `List inside an main entity:

The property “activities” inside the main entity is annotated with @IndexedEmbedded

Here is my ActivitityEntity:

  @Field(name = "date_sort", index = Index.YES, analyze = Analyze.NO, store = Store.YES, termVector = TermVector.YES, indexNullAs = Field.DEFAULT_NULL_TOKEN)
  @SortableField(forField = "date_sort")
  private LocalDateTime startDateTime;

How can I sort by this field?

    Sort sort = new Sort(
        new SortField("activities.startDateTime", Type.SCORE, Boolean.FALSE));

With score type it is sorting but incorrectly, with other types I am receiving some errors…
And it works only if I added the “name” and “forField” attributes…

Thanks

Hey,

I’d say don’t try to guess at the internals and just use the Hibernate Search DSL :slight_smile:

EDIT: Also, you’re using the wrong field name in your sort. You should use activities.date_sort, not activities.startDateTime.

EDIT: Also, your field is multi-valued so this won’t work in Hibernate Search 5; see my other answer below.

FullTextSession fullTextSession = Search.getFullTextSession(session);
QueryBuilder builder = fullTextSession.getSearchFactory()
    .buildQueryBuilder().forEntity(MyEntity.class).get();
Query luceneQuery = /* ... */;
FullTextQuery query = s.createFullTextQuery( luceneQuery, MyEntity.class );
Sort sort = builder
  .sort()
    .byField("activities.date_sort")
  .createSort();
query.setSort(sort);
List results = query.list();

It would be even easier and arguably more straightforward with Hibernate Search 6+:

SearchSession searchSession = Search.session( entityManager );

List<Book> result = searchSession.search( MyEntity.class ) 
        .where( f -> /*  ... */ )
        .sort( f -> f.field( "activities.date_sort" ) )
        .fetchHits( 20 ); 

Hi I tried with this first solution, but it’s showed this error:

`2022-11-03 13:54:07.638 ERROR 15476 --- [nio-8092-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: unexpected docvalues type NONE for field 'activities.startTime' (expected=SORTED). Use UninvertingReader or index with docvalues.; nested exception is java.lang.IllegalStateException: unexpected docvalues type NONE for field 'activities.startTime' (expected=SORTED). Use UninvertingReader or index with docvalues.] with root cause

java.lang.IllegalStateException: unexpected docvalues type NONE for field 'activities.startTime' (expected=SORTED). Use UninvertingReader or index with docvalues.
	at org.apache.lucene.index.DocValues.checkField(DocValues.java:208) ~[lucene-core-5.5.5.jar:5.5.5 b3441673c21c83762035dc21d3827ad16aa17b68 - sarowe - 2017-10-20 08:57:09]
	at org.apache.lucene.index.DocValues.getSorted(DocValues.java:264) ~[lucene-core-5.5.5.jar:5.5.5 b3441673c21c83762035dc21d3827ad16aa17b68 - sarowe - 2017-10-20 08:57:09]
`

Ah. Well, yes, your example wasn’t using the right field name and I copy-pasted without thinking. You should use activities.date_sort, not activities.startDateTime. I’ll update my answer.

Actually, I just noticed there are multiple activities, and thus multiple “startDateTime”, per indexed entity.

This just won’t work in Hibernate Search 5, because Hibernate Search 5 only supports sorting on single-valued fields. There is no workaround.

You will have to upgrade to Hibernate Search 6, which does support sorting on multi-valued fields.

You will find an extensive migration guide from Hibernate Serach 5 to 6 here.