How to implement full-text search in Meteor / Telescope

I tried to do a telescope search using pure javascript since it looks like FTS for Meteor, and I could not get 2.4 playing well with Meteor yet.

I use the existing pagination model, which is already implemented in the telescope, to display Top / New / Best messages, as well as the session variable for the search keyword, which is set in the router when navigating, for example, / Search / Foobar.

However, it does not seem to work; when I have, say, 100 posts, a regular page subscription only returns with 25 of them, and my search results only show posts in the first 25.

I banged my head against the wall for several days, trying to debug this: sometimes it works, sometimes it doesn't!

Here's the code (I included all the additional search code for reference):

app.js:

var resultsPostsSubscription = function() { var handle = paginatedSearchSubscription( 10, 'searchResults' ); handle.fetch = function() { return limitDocuments( searchPosts( Session.get( 'keyword' ) ), handle.loaded() ); }; return handle; }; var resultsPostsHandle = resultsPostsSubscription(); 

paginated_sub.js:

I duplicated the existing paginatedSubscription because I cannot pass Session var as arg; It must be dynamic. I will probably reorganize later.

 paginatedSearchSubscription = function (perPage/*, name, arguments */) { var handle = new PaginatedSubscriptionHandle(perPage); var args = Array.prototype.slice.call(arguments, 1); Meteor.autosubscribe(function() { var subHandle = Meteor.subscribe.apply(this, args.concat([ Session.get( 'keyword' ), handle.limit(), function() { handle.done(); } ])); handle.stop = subHandle.stop; }); return handle; } 

search.js: (new file, in / shared directory)

 // get all posts where headline, categories, tags or body are LIKE %keyword% searchPosts = function( keyword ) { var query = new RegExp( keyword, 'i' ); var results = Posts.find( { $or: [ { 'headline': query }, { 'categories': query }, { 'tags': query }, { 'body': query } ] } ); return results; }; 

publish.js:

 Meteor.publish( 'searchResults', searchPosts ); 

posts_list.html:

 <template name="posts_results"> {{> posts_list resultsPostsHandle}} </template> 

posts_list.js:

 Template.posts_results.resultsPostsHandle = function() { return resultsPostsHandle; }; 

router.js: there is a navigation search bar that redirects here

 posts_results = function( keyword ) { Session.set( 'keyword' , keyword ); return 'posts_results'; }; Meteor.Router.add({ ... '/search/:keyword':posts_results, ... }) 

Any help would be greatly appreciated!

+4
source share
1 answer

A little late, but here is a complete record on how to implement full-text search in a meteor.

"The easiest way without editing any Meteor code is to use your own mongodb."

+2
source

All Articles