Skip to content

TextQuery ignores sort order when using sortByScore. #3896

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
yoobi opened this issue Dec 2, 2021 · 0 comments
Closed

TextQuery ignores sort order when using sortByScore. #3896

yoobi opened this issue Dec 2, 2021 · 0 comments
Assignees
Labels
type: bug A general bug

Comments

@yoobi
Copy link

yoobi commented Dec 2, 2021

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();
}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Dec 2, 2021
@christophstrobl christophstrobl changed the title text search sort not working with sortByScore() TextQuery ignores sort order when using sortByScore. Dec 7, 2021
@christophstrobl christophstrobl added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Dec 7, 2021
@jxblum jxblum closed this as completed in 1131060 Dec 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants