Hi folks!
I need help with composite sort.
Here’s my situation.
I use:
- Hibernate search 7.1.1.Final
- Lucene backend
- Mapper ORM
My entities structure:
@Indexed
public class SourceModel {
@Id
@Column(name = "id", nullable = false)
@JdbcTypeCode(SqlTypes.BIGINT)
@GenericField(name = "idSort", sortable = Sortable.YES)
private Long id;
@FullTextField(name = "title", analyzer = "stop")
@KeywordField(name = "titleSort", sortable = Sortable.YES, normalizer = "lowercase")
@Column(name = "title")
private String title;
//My data is not updated after creation.
//I do not need reindexing on update
@IndexedEmbedded(structure = ObjectStructure.NESTED)
@IndexingDependency(reindexOnUpdate = ReindexOnUpdate.SHALLOW)
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "source_id")
private List<CalculationModel> calculations;
@IndexedEmbedded(structure = ObjectStructure.NESTED)
@IndexingDependency(reindexOnUpdate = ReindexOnUpdate.SHALLOW)
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "source_id")
private List<SourceMetricModel> sourceMetrics;
}
@Indexed
public class CalculationModel {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", nullable = false)
@JdbcTypeCode(SqlTypes.BIGINT)
private Long id;
@GenericField(indexNullAs = "0")
@Column(name = "calc_year")
private Integer year;
@GenericField(sortable = Sortable.YES, indexNullAs = "0")
@Column(name = "citescore")
private Float citeScore;
@GenericField(indexNullAs = "0")
@Column(name = "source_id")
private Long sourceId;
}
@Indexed
public class SourceMetricModel {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", nullable = false)
@JdbcTypeCode(SqlTypes.BIGINT)
private Long id;
@GenericField(indexNullAs = "0")
@Column(name = "source_id")
private Long sourceId;
@GenericField(indexNullAs = "0")
@Column(name = "cov_year")
private Integer year;
@GenericField(name = "sjr", sortable = Sortable.YES, indexNullAs = "0")
@Column(name = "sjr_value")
private Float sjrValue;
}
Here comes a strange (at least for me) situation.
When I perform search and sort like this, everything works perfectly
public SearchResult<SourceModel> getYearlyMetrics(YearlyMetricsRequest request) {
return searchSession.search(SourceModel.class)
.where(f -> f.match().field("calculations.year").matching(request.getYear()))
.sort(
sort -> sort.field("calculations.citeScore").asc()
.filter(f -> f.match().field("calculations.year").matching(request.getYear()))
.then().field("titleSort").asc()
)
.fetch(0, 100);
}
BUT!!! When I try to do this (which is hierarchically is exactly the same and has the same types of data),
public SearchResult<SourceModel> getYearlyMetrics(YearlyMetricsRequest request) {
return searchSession.search(SourceModel.class)
.where(f -> f.match().field("calculations.year").matching(request.getYear()))
.sort(
sort -> sort.field("sourceMetrics.sjr").asc()
.filter(f -> f.match().field("sourceMetrics.year").matching(request.getYear()))
.then().field("titleSort").asc()
)
.fetch(0, 100);
}
I get an exception
org.hibernate.search.util.common.AssertionFailure: This iterator can only move forward (no advancing to the same doc twice, no going backward) -- this may indicate a bug or a missing test in Hibernate Search.
Full stacktrace:
org.hibernate.search.util.common.AssertionFailure: This iterator can only move forward (no advancing to the same doc twice, no going backward) -- this may indicate a bug or a missing test in Hibernate Search. Please report it: https://hibernate.org/community/
at org.hibernate.search.backend.lucene.lowlevel.join.impl.ChildDocIds.advanceExactParent(ChildDocIds.java:40)
at org.hibernate.search.backend.lucene.lowlevel.docvalues.impl.DoubleMultiValuesToSingleValuesSource$2.advanceExact(DoubleMultiValuesToSingleValuesSource.java:154)
at org.hibernate.search.backend.lucene.lowlevel.docvalues.impl.NumericDoubleValues$RawNumericDocValues.advanceExact(NumericDoubleValues.java:99)
at org.apache.lucene.search.comparators.FloatComparator$FloatLeafComparator.getValueForDoc(FloatComparator.java:70)
at org.apache.lucene.search.comparators.FloatComparator$FloatLeafComparator.copy(FloatComparator.java:95)
at org.apache.lucene.search.MultiLeafFieldComparator.copy(MultiLeafFieldComparator.java:84)
at org.apache.lucene.search.TopFieldCollector$TopFieldLeafCollector.collectCompetitiveHit(TopFieldCollector.java:115)
at org.apache.lucene.search.TopFieldCollector$SimpleFieldCollector$1.collect(TopFieldCollector.java:207)
at org.apache.lucene.search.ConjunctionBulkScorer.score(ConjunctionBulkScorer.java:171)
at org.apache.lucene.search.BulkScorer.score(BulkScorer.java:38)
at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:769)
at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:554)
at org.hibernate.search.backend.lucene.search.extraction.impl.LuceneCollectors.collectMatchingDocs(LuceneCollectors.java:96)
at org.hibernate.search.backend.lucene.search.query.impl.LuceneSearcherImpl.collectMatchingDocs(LuceneSearcherImpl.java:142)
at org.hibernate.search.backend.lucene.search.query.impl.LuceneSearcherImpl.doSearch(LuceneSearcherImpl.java:98)
at org.hibernate.search.backend.lucene.search.query.impl.LuceneSearcherImpl.search(LuceneSearcherImpl.java:75)
at org.hibernate.search.backend.lucene.search.query.impl.LuceneSearcherImpl.search(LuceneSearcherImpl.java:32)
at org.hibernate.search.backend.lucene.work.impl.SearchWork.execute(SearchWork.java:41)
at org.hibernate.search.backend.lucene.orchestration.impl.LuceneSyncWorkOrchestratorImpl$WorkExecution.execute(LuceneSyncWorkOrchestratorImpl.java:156)
at org.hibernate.search.backend.lucene.orchestration.impl.LuceneSyncWorkOrchestratorImpl.doSubmit(LuceneSyncWorkOrchestratorImpl.java:90)
at org.hibernate.search.backend.lucene.orchestration.impl.LuceneSyncWorkOrchestratorImpl.doSubmit(LuceneSyncWorkOrchestratorImpl.java:33)
at org.hibernate.search.engine.backend.orchestration.spi.AbstractWorkOrchestrator.submit(AbstractWorkOrchestrator.java:139)
at org.hibernate.search.backend.lucene.orchestration.impl.LuceneSyncWorkOrchestratorImpl.submit(LuceneSyncWorkOrchestratorImpl.java:59)
at org.hibernate.search.backend.lucene.orchestration.impl.LuceneSyncWorkOrchestrator.submit(LuceneSyncWorkOrchestrator.java:27)
at org.hibernate.search.backend.lucene.search.query.impl.LuceneSearchQueryImpl.doSubmit(LuceneSearchQueryImpl.java:202)
at org.hibernate.search.backend.lucene.search.query.impl.LuceneSearchQueryImpl.doFetch(LuceneSearchQueryImpl.java:177)
at org.hibernate.search.backend.lucene.search.query.impl.LuceneSearchQueryImpl.fetch(LuceneSearchQueryImpl.java:99)
at org.hibernate.search.backend.lucene.search.query.impl.LuceneSearchQueryImpl.fetch(LuceneSearchQueryImpl.java:40)
at org.hibernate.search.engine.search.query.dsl.spi.AbstractSearchQueryOptionsStep.fetch(AbstractSearchQueryOptionsStep.java:203)
at com.elsevier.kd.repository.fts.YearlyMetricsFtsRepository.getYearlyMetrics(YearlyMetricsFtsRepository.java:23)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:355)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:196)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:768)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:138)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:768)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:720)
at com.elsevier.kd.repository.fts.YearlyMetricsFtsRepository$$SpringCGLIB$$0.getYearlyMetrics(<generated>)
at com.elsevier.kd.service.YearlyMetricsService.getYearlyMetrics(YearlyMetricsService.java:26)
at com.elsevier.kd.controller.YearlyMetricsController.search(YearlyMetricsController.java:22)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:255)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:188)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:118)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:926)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:831)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1089)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:979)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:914)
at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:590)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885)
at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:658)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:195)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:140)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:164)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:140)
at org.springframework.web.filter.AbstractRequestLoggingFilter.doFilterInternal(AbstractRequestLoggingFilter.java:289)
at com.elsevier.kd.appweb.filter.TraceFilter.doFilterInternal(TraceFilter.java:61)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:164)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:140)
at com.elsevier.kd.appweb.filter.TrailingSlashRedirectFilter.doFilter(TrailingSlashRedirectFilter.java:30)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:164)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:140)
at com.elsevier.kd.appweb.filter.logging.LoggingFilter.doFilterInternal(LoggingFilter.java:50)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:164)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:140)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:164)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:140)
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:164)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:140)
at org.springframework.web.filter.ServerHttpObservationFilter.doFilterInternal(ServerHttpObservationFilter.java:113)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:164)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:140)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:164)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:140)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:167)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:90)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:483)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:115)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:93)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:344)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:384)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:904)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1741)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52)
at java.base/java.lang.VirtualThread.run(VirtualThread.java:329)
What am I doing wrong here?
Thank you all in advance!