Closed egor-bulgakov closed 7 years ago
We are seeing the same behavior. Any chance of getting a fix for this?
I'm very busy and haven't had the chance to have a look at this. feel free to submit a PR
On following line there's something that's not working correctly. Please feel free to help out, I might have a look tonight: https://github.com/matteodem/meteor-easy-search/blob/master/packages/easysearch:core/lib/core/search-collection.js#L203.
The problem is that there is a sortPosition
hack to maintain sort order of the docs on the client and it doesn't really sort by what's configured when docs are added. I'm not aware of a clean solution to this problem.
Looks like the __sortPosition
is set correctly on the newly created document, but all other documents keeps their old __sortPosition
value. So if we have published 4 documents:
[
{
title: 'First document',
__sortPosition: 0
},
{
title: 'Second document',
__sortPosition: 1
},
{
title: 'Third document',
__sortPosition: 2
},
{
title: 'Fourth document',
__sortPosition: 3
}
]
and a new document is added:
{
title: 'New document',
__sortPosition: 0
}
It will get added at index 1, because the other __sortPosition
values are not updated. The new array of published documents would look like:
[
{
title: 'First document',
__sortPosition: 0
},
{
title: 'New document',
__sortPosition: 0
},
{
title: 'Second document',
__sortPosition: 1
},
{
title: 'Third document',
__sortPosition: 2
},
{
title: 'Fourth document',
__sortPosition: 3
}
]
In this example, if we manage to increment the __sortPosition
for documents with __sortPosition >= 0
the order will be correct. I'm not sure how we can proceed with this. Any suggestions?
I also had a look at this and I tried to find a solution that does not incorporate changes to all
__sortPosition
params. I'm interested in how meteor itself manages changes in the sort order.
Other than investigating that, I don't have any other suggestions.
I tried ordering the documents based on sortPosition
(on the client), then order by sort options provided by the sort
helper function. The first inserted document worked flawlessly, but the next, not so much. Looks like it jumps two positions down for each insert. Wierd... I changed to this, inside lib/core/search-collection.js
:
_getMongoCursor(searchDefinition, options) {
const sort = { __sortPosition: 1, ...this.engine.config.sort() };
return this._collection.find(
{ __searchDefinition: JSON.stringify(searchDefinition), __searchOptions: JSON.stringify(options.props) },
{
transform: (doc) => {
delete doc.__searchDefinition;
delete doc.__searchOptions;
delete doc.__sortPosition;
doc = this.engine.config.transform(doc);
return doc;
},
sort
}
);
}
This is fixed in the latest version v2.1.10
Thank you for creating this powerful package. We actively use it in our projects.
The only annoying issue we're facing so far is incorrect sort order that can be noticed after a new document has been inserted. For some reason the newly created documents go to the second, third place and so on.
Here is the Leaderboard example:
To replicate the issue one needs to insert a new Player document with the top score.
Please advise how this can be fixed