meteorhacks/search-source

Name: search-source

Owner: meteorhacks

Description: Reactive Data Source for Search

Created: 2014-12-23 08:15:29.0

Updated: 2017-11-13 19:33:40.0

Pushed: 2017-02-06 05:48:53.0

Homepage:

Size: 41

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

search-source

Reactive Data Source for building search solutions with Meteor

If you are new to search source, it's a good idea to look at this introductory article on MeteorHacks.

Installation
or add meteorhacks:search-source
Creating a source in client
options = {
epHistory: 1000 * 60 * 5,
calSearch: true

fields = ['packageName', 'description'];

ageSearch = new SearchSource('packages', fields, options);
Define the data source on the server

In the server, get data from any backend and send those data to the client as shown below. You need to return an array of documents where each of those object consists of _id field.

Just like inside a method, you can use Meteor.userId() and Meteor.user() inside a source definition.

chSource.defineSource('packages', function(searchText, options) {
r options = {sort: {isoScore: -1}, limit: 20};

(searchText) {
var regExp = buildRegExp(searchText);
var selector = {packageName: regExp, description: regExp};
return Packages.find(selector, options).fetch();
else {
return Packages.find({}, options).fetch();



tion buildRegExp(searchText) {
r words = searchText.trim().split(/[ \-\:]+/);
r exps = _.map(words, function(word) {
return "(?=.*" + word + ")";
;
r fullExp = exps.join('') + ".+";
turn new RegExp(fullExp, "i");

Get the reactive data source

You can get the reactive data source with the PackageSearch.getData api. This is an example usage of that:

late.searchResult.helpers({
tPackages: function() {
return PackageSearch.getData({
  transform: function(matchText, regExp) {
    return matchText.replace(regExp, "<b>$&</b>")
  },
  sort: {isoScore: -1}
});


.getData() api accepts an object with options (and an optional argument to ask for a cursor instead of a fetched array; see example below). These are the options you can pass:

late.searchResult.helpers({
tPackages: function() {
return PackageSearch.getData({
  docTransform: function(doc) {
    return _.extend(doc, {
      owner: function() {
        return Meteor.users.find({_id: this.ownerId})
      }
    })
  },
  sort: {isoScore: -1}
}, true);


Searching

Finally we can invoke search queries by invoking following API.

ageSearch.search("the text to search");
Status

You can get the status of the search source by invoking following API. It's reactive too.

status = PackageSearch.getStatus();

Status has following fields depending on the status.

Metadata

With metadata, you get some useful information about search along with the search results. These metadata can be time it takes to process the search or the number of results for this search term.

You can get the metadata with following API. It's reactive too.

metadata = PackageSearch.getMetadata();

Now we need a way to send metadata to the client. This is how we can do it. You need to change the server side search source as follows

chSource.defineSource('packages', function(searchText, options) {
r data = getSearchResult(searchText);
r metadata = getMetadata();

turn {
data: data,
metadata: metadata


Passing Options with Search

We can also pass some options while searching. This is the way we can implement pagination and other extra functionality.

Let's pass some options to the server:

n the client
options = {page: 10};
ageSearch.search("the text to search", options);

Now you can get the options object from the server. See:

n the server
chSource.defineSource('packages', function(searchText, options) {
 do anything with options
nsole.log(options); // {"page": 10}

Get Current Search Query

You can get the current search query with following API. It's reactive too.

searchText = PackageSearch.getCurrentQuery();
Clean History

You can clear the stored history (if enabled the keepHistory option) via the following API.

ageSearch.cleanHistory();
Defining Data Source in the Client

Sometime, we don't need to fetch data from the server. We need to get it from a data source aleady available on the client. So, this is how we do it:

ageSearch.fetchData = function(searchText, options, success) {
meOtherDDPConnection.call('getPackages', searchText, options, function(err, data) {
success(err, data);
;


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.