Skip to content
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

Reactive search results. #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,40 @@ Template.searchResult.helpers({
});
```

### Use reactive data in search results

By default, the contents returned by `getData()` is not reactive in the sense of it reflecting the current state of the mongo database. To use reactive content, do the following when creating a SearchSource object on the client

```js
var options = {
keepHistory: 1000 * 60 * 5,
localSearch: true,
collection: Documents, // Collection to reflect on the client
subscriptionName: 'documents.byIds' // Use subscription by this name
};

// Some fields
const fields = [ 'name', 'description' ];

const DocumentSearch = new SearchSource('docs', fields, options);
```

and on the server, publish the documents by ids

```js
Meteor.publish('documents.byIds', function (ids) {
check(ids, [ String ]);

return Documents.find({
_id: {
$in: ids
}
});
});
```

and the rest is as usual. One thing to notice is that `getData()` cannot return a cursor if reactive items are used. This is because we sort documents on a score field that is not attached to the documents itself, so mongo cannot take care of the ordering.

### Searching

Finally we can invoke search queries by invoking following API.
Expand Down
33 changes: 32 additions & 1 deletion lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ SearchSource.prototype._loadData = function(query, options) {
var self = this;
var version = 0;
var historyKey = query + EJSON.stringify(options);

if(this._canUseHistory(historyKey)) {
this._updateStore(this.history[historyKey].data);
this.metaData.set(this.history[historyKey].metadata);
Expand Down Expand Up @@ -149,6 +150,36 @@ SearchSource.prototype.getData = function(options, getCursor) {
transform: transform
});

var collection = this.options.collection;

if(collection) {
var ids = _.pluck(cursor.fetch(), '_id');

if (!this.options.subscriptionName) {
throw Error('subscritionName is missing');
}

var sub = Meteor.subscribe(this.options.subscriptionName, ids);

if (!sub.ready())
return [];

var docs = collection.find({
_id: {
$in: ids
}
}, {
transform: transform
}).fetch();

var sortIds = _.invert(_.object(_.pairs(ids)));
var sorted = _.sortBy(docs, function(x) {
return sortIds[x._id];
});

return sorted;
}

if(getCursor) {
return cursor;
}
Expand Down Expand Up @@ -234,4 +265,4 @@ SearchSource.prototype._getRegExpFilterRegExp = _.once(function() {
return "\\" + c;
}).join("|");
return new RegExp("(" + regExpCharsReplace + ")", "g");
});
});