Skip to content

TextQuery ignores sort order when using sortByScore. #3896

Closed
@yoobi

Description

@yoobi

Hello,

First of all here is my dependency : "org.springframework.boot:spring-boot-starter-data-mongodb-reactive:2.6.1"

Here is my query:

// Pageable: Page request [number: 0, size 5, sort: updateTime: DESC]
val textQuery = TextQuery.queryText(TextCriteria().matchingAny(search))
textQuery.with(pageable.sort)
textQuery.sortByScore()
textQuery.addCriteria(Account::owner isEqualTo owner)
textQuery.skip(pageable.pageNumber.toLong())
textQuery.limit(pageable.pageSize)
return template.find(textQuery.also { println(it) }, Account::class.java)

However when I print the query I get this:

Query: { 
    "$text" : { "$search" : "ema"},
    "owner" : { "$oid" : "1234567898"}
}, 
Fields: { "score" : { "$meta" : "textScore"} }, 
Sort: { 
    "score" : { "$meta" : "textScore"},
     "updateTime" : -1
}

When looking at the source code you can clearly see that using sortByScore() ignores the order of sort and puts it in front of the Sort

@Override
public Document getSortObject() {

	if (this.sortByScore) {
		Document sort = new Document();
		sort.put(getScoreFieldName(), META_TEXT_SCORE);
		sort.putAll(super.getSortObject());
		return sort;
	}

	return super.getSortObject();
}

the pageable sort is put at the end and therefore not doing anything. How can I sort by score and by the pageable ?


Possible fix:

@Override
public Document getSortObject() {

	if (this.sortByScore) {
		Document sort = new Document();
		sort.putAll(super.getSortObject());
                sort.put(getScoreFieldName(), META_TEXT_SCORE);
		return sort;
	}

	return super.getSortObject();
}

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions