Fabic
Fabic

Reputation: 508

Custom sort and limit publication in Meteor

Short one: I need to regex-search a collection, move perfect matches to first position of the result and then limit that result.

Long one:

In a publication I need to custom sort my results and then (of course after sorting) limit it.

My custom sort is something like sorting by relevance but not exactly. Given these documents:

{ number: '2123' } // let's call this one A
{ number: '21' } // and this one B
{ number: 'A2123' } // and this one C
{ number: '01' } // and this one D

If I'm searching with the term 21 I want to get A, B and C. BUT at first position B. Why that? Because B matches perfect. You see? It's not exactly sorting by relevance but moving a perfect match (there will only be 1 perfect match at most) to first position.

I tried text indexes but they don't match 2123 with the search term 21. (I understand why and that's fine)

So what exactly is my problem? I can regex-search and publish that cursor but it will have too many documents. I could limit it but without sorting I may loose perfect matches which I absolutely need.

So in the end I can't custom sort. And then I had that idea: Why don't I fetch() my cursor (unsorted and unlimited), sort and limit that fetched array and create an "artificial" cursor based on that sorted array. I didn't find a way to achieve that.

Upvotes: 1

Views: 74

Answers (1)

Michel Floyd
Michel Floyd

Reputation: 20256

Here's a bit of a hackish idea:

  1. Subscribe to two different publications, exactMatches and allMatches
  2. exactMatches should return only exact matches, limited to your count
  3. allMatches would return all matches, also limited to your count

Now on the client you can .find().fetch() on the collection and rearrange there in an optimized order.

This approach would (a) guarantee that you see all the exact matches, (b) minimize network traffic, (c) put the sorting load on the client instead of your server.

Come to think of it, you can also render the exact matches separately and before the partial matches so you only really need to sort the latter. In blaze:

{{#each exactMatches}}
  {{> result}}
{{/each}}
{{#each partialMatches}}
  {{>result}}
{{/each}}

Upvotes: 2

Related Questions