Skip to content

Commit

Permalink
Merge pull request #387 from europeana/EA-2094_fix_sort_order
Browse files Browse the repository at this point in the history
preserve solr order if sorting is applied
  • Loading branch information
gsergiu authored Jul 23, 2024
2 parents 1bf233a + 9f581d3 commit b97aff5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<fieldType name="long" class="solr.LongPointField"/>

<fieldType name="date" class="solr.DatePointField"
omitNorms="true" docValues="true"/>
omitNorms="true" />
<fieldType name="text" class="solr.TextField">
<analyzer type="index">
<tokenizer class="solr.WhitespaceTokenizerFactory" />
Expand Down Expand Up @@ -126,8 +126,8 @@
required="false" multiValued="true" stored="true" />

<!-- @Field("updated_timestamp") -->
<field name="modified" type="date" indexed="false" required="true"
multiValued="false" stored="true"/>
<field name="modified" type="date" indexed="true" required="true"
multiValued="false" stored="true" />

<!-- @Field("body_value") -->
<field name="body_value" type="text" indexed="true" required="false"
Expand All @@ -142,11 +142,11 @@
<!-- multiValued="false" /> -->

<!-- @Field("created_timestamp") -->
<field name="created" type="date" indexed="false" required="true"
<field name="created" type="date" indexed="true" required="true"
multiValued="false" stored="true" />

<!-- @Field("generated_timestamp") -->
<field name="generated" type="date" indexed="false" required="true"
<field name="generated" type="date" indexed="true" required="true"
multiValued="false" stored="true" />

<!-- @Field("moderationScore" TYPE) -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
that you fully re-index after changing this setting as it can
affect both how text is indexed and queried.
-->
<luceneMatchVersion>7.7.2</luceneMatchVersion>
<luceneMatchVersion>7.7.1</luceneMatchVersion>

<!-- <lib/> directives can be used to instruct Solr to load any Jars
identified and use them to resolve any "plugins" specified in
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package eu.europeana.annotation.web.service.impl;

import java.util.Comparator;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import eu.europeana.annotation.definitions.model.Annotation;

public class AnnotationOrderComparator implements Comparator<Annotation>{

private final ConcurrentHashMap<Long, Integer> order;
public AnnotationOrderComparator(List<Long> annotationIds) {
order = new ConcurrentHashMap<Long, Integer>(annotationIds.size());
int position = 0;
for (Long annotationId : annotationIds) {
order.put(annotationId, position++);
}
}

@Override
public int compare(Annotation o1, Annotation o2) {
return order.getOrDefault(o1.getIdentifier(), Integer.MAX_VALUE)
- order.getOrDefault(o2.getIdentifier(), Integer.MAX_VALUE);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package eu.europeana.annotation.web.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -84,8 +86,7 @@ public AnnotationPage search(Query query, HttpServletRequest request) throws Htt
}

// fetch annotation objects
List<? extends Annotation> annotations = mongoPersistance.getAnnotationList(annotationIds);
protocol.setAnnotations(annotations);
protocol.setAnnotations(fetchAnnotationsFromDB(annotationIds, query));
}


Expand Down Expand Up @@ -116,6 +117,17 @@ public AnnotationPage search(Query query, HttpServletRequest request) throws Htt
return protocol;
}

private List<? extends Annotation> fetchAnnotationsFromDB(List<Long> annotationIds, Query query) {
List<? extends Annotation> annotations = mongoPersistance.getAnnotationList(annotationIds);
if(StringUtils.isNotBlank(query.getSort())) {
//need to ensure same order of annotations
AnnotationOrderComparator comparator = new AnnotationOrderComparator(annotationIds);
annotations.sort(comparator);
}

return annotations;
}

private boolean isIncludeAnnotationsSearch(Query query) {
return SearchProfiles.STANDARD.equals(query.getSearchProfile());
}
Expand Down

0 comments on commit b97aff5

Please sign in to comment.