Skip to content

Commit 1131060

Browse files
committed
Polish for retaining the sort order when using text search sort by score.
Closes gh-3896.
1 parent 132834b commit 1131060

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/TextQuery.java

+24-12
Original file line numberDiff line numberDiff line change
@@ -175,32 +175,44 @@ public Document getFieldsObject() {
175175
public Document getSortObject() {
176176

177177
if (this.sortByScore) {
178-
if (sortByScoreIndex == 0) {
179-
Document sort = new Document();
180-
sort.put(getScoreFieldName(), META_TEXT_SCORE);
181-
sort.putAll(super.getSortObject());
182-
return sort;
183-
}
184-
return fitInSortByScoreAtPosition(super.getSortObject());
178+
179+
int sortByScoreIndex = this.sortByScoreIndex;
180+
181+
return sortByScoreIndex != 0
182+
? sortByScoreAtPosition(super.getSortObject(), sortByScoreIndex)
183+
: sortByScoreAtPositionZero();
185184
}
186185

187186
return super.getSortObject();
188187
}
189188

190-
private Document fitInSortByScoreAtPosition(Document source) {
189+
private Document sortByScoreAtPositionZero() {
190+
191+
Document sort = new Document();
192+
193+
sort.put(getScoreFieldName(), META_TEXT_SCORE);
194+
sort.putAll(super.getSortObject());
195+
196+
return sort;
197+
}
198+
199+
private Document sortByScoreAtPosition(Document source, int sortByScoreIndex) {
191200

192201
Document target = new Document();
193-
int i = 0;
202+
int index = 0;
203+
194204
for (Entry<String, Object> entry : source.entrySet()) {
195-
if (i == sortByScoreIndex) {
205+
if (index == sortByScoreIndex) {
196206
target.put(getScoreFieldName(), META_TEXT_SCORE);
197207
}
198208
target.put(entry.getKey(), entry.getValue());
199-
i++;
209+
index++;
200210
}
201-
if (i == sortByScoreIndex) {
211+
212+
if (index == sortByScoreIndex) {
202213
target.put(getScoreFieldName(), META_TEXT_SCORE);
203214
}
215+
204216
return target;
205217
}
206218

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/TextQueryUnitTests.java

+4-6
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
*/
1616
package org.springframework.data.mongodb.core.query;
1717

18-
import static org.springframework.data.mongodb.test.util.Assertions.*;
19-
20-
import java.util.Map.Entry;
18+
import static org.springframework.data.mongodb.test.util.Assertions.assertThat;
2119

2220
import org.junit.jupiter.api.Test;
2321
import org.springframework.data.domain.Sort;
@@ -103,20 +101,20 @@ public void retainsSortOrderWhenUsingScore() {
103101
query.sortByScore();
104102
query.with(Sort.by(Direction.DESC, "two"));
105103

106-
assertThat(query.getSortObject().entrySet().stream().map(Entry::getKey)).containsExactly("one", "score", "two");
104+
assertThat(query.getSortObject().keySet().stream()).containsExactly("one", "score", "two");
107105

108106
query = new TextQuery(QUERY);
109107
query.with(Sort.by(Direction.DESC, "one"));
110108
query.sortByScore();
111109

112-
assertThat(query.getSortObject().entrySet().stream().map(Entry::getKey)).containsExactly("one", "score");
110+
assertThat(query.getSortObject().keySet().stream()).containsExactly("one", "score");
113111

114112
query = new TextQuery(QUERY);
115113
query.sortByScore();
116114
query.with(Sort.by(Direction.DESC, "one"));
117115
query.with(Sort.by(Direction.DESC, "two"));
118116

119-
assertThat(query.getSortObject().entrySet().stream().map(Entry::getKey)).containsExactly("score", "one", "two");
117+
assertThat(query.getSortObject().keySet().stream()).containsExactly("score", "one", "two");
120118
}
121119

122120
}

0 commit comments

Comments
 (0)