Help with search packages

Hello, I’m using meteor add matteodem:easy-search for my searches and I’ve run into some customization problems.
I have set up Posts.initEasySearch(‘tags’) to seacrh thru posts with certain tags

1.) I would like to have all posts listed while the input field is empty, now when it’s empty their is no data displayed but when i start typing posts I want they get rendered

2.) I want to get the data from my helper into the search since Im filtering it there thru 'posts': return Posts.find("some filter").
It seems easy search access all documents in the collection and not just the ones i return in my helper.
Do I need to set up publish with these filters or something or there is a way to get the data thru the helper?

Not sure how to do it with easy-search. I’ve no experience using it.

Try to look at Search Source too. With that, you’ve a lot control.

Disclaimer: I am the author of this package.

+1 to @arunoda’s Search Source package. It will solve both your problems out of the box.

1.) The first one is easy. Just pass

allDocsOnEmpty=true

to your {{> esInput }}, so it becomes:

{{> esInput index="Posts" placeholder="Search using tags..." allDocsOnEmpty=true }}

2.) If I get you correctly, you want to customize your search query, so that while searching, you don’t return all the Posts-docs in minimongo but only a special selection of them, correct? If so, have a look at the javascript API ( http://matteodem.github.io/meteor-easy-search/docs/javascript-api/ ), you can customize properties to filter you data.

1 Like

Got the first problem to work, but having issues with the second one.
Any idea what im doing wrong here:

Posts.initEasySearch(['tags', 'posts'],
	{
		'limit': 1000,
		'query': {date:{$gte: startDate, $lt:storeDate}},
		'sort': {date: - 1}
	}

	);

@arunoda I couldn’t get your package to work, I copy-pasted your example, changed the code to match mine collection and filter but the data wouldnt get rendered. Only after I would type something in the input field and then delete it it would appear. But i still couldnt filter it, I probably messed up somewhere but couldn’t find where.

forgot to add the error im getting with easy-search:
Exception from Tracker recompute function: object is not a function
TypeError: object is not a function

here is an example that i copied from the EasySearch Leaderboard example on github: https://github.com/matteodem/easy-search-leaderboard/blob/master/leaderboard.js.

// Search Index for the main players search
EasySearch.createSearchIndex('players', {
  'collection': Players, // instanceof Meteor.Collection
  'field': ['name', 'score'], // array of fields to be searchable
  'limit': 10,
  'use' : 'mongo-db',
  'convertNumbers': true,
  'props': {
    'filteredCategory': 'All',
    'sortBy': 'score'
  },
  'sort': function() {
    if (this.props.sortBy === 'name') {
      return { 'name': 1 };
    }  else if (this.props.sortBy === 'lowest-score') {
      return { 'score': 1 };
    }

    // default by highest score
    return { 'score': -1 };
  },
  'query': function(searchString, opts) {
    // Default query that will be used for the mongo-db selector
    var query = EasySearch.getSearcher(this.use).defaultQuery(this, searchString);

    // filter for categories if set
    if (this.props.filteredCategory.toLowerCase() !== 'all') {
      query.category = this.props.filteredCategory;
    }

    return query;
  }
});

hope that helps.

1 Like

Thanks for the help but I don’t know to set it up with easy-search, I’ll give arunoda’s search-source a try now.

A general question for all:
How does this search work anyway, does it make a call to the DB every time I search?
If so, isn’t something like a simple filter faster since I already got all my data displayed and now I just wanna filter it with the keywords thru the desired fields? Similar to ng-repeat in Angular and the filter option.

@arunoda could you help me setting up your package

I get this error when I start typing in the search box:

Undefined is not a function at this line : return matchText.replace(regExp, "<b>$&</b>")

from here:

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

Could you post an issues here with some produceable app.

Then, I can help you quickly.

Found out what the problem was finally. My matchText is an array of strings and I can’t call .replace() on array.
I’ve tried using toString() before .replace() but it doesn’t work. And from what I understood it just basically returns the matched string as bold text (highlighted) and I can live without it but figured maybe you want to look into that.
If i comment out / delete that part it all works.

Now I have trouble getting new data appear when I update my collection. It only appears after some time has passed.
I’ve tried changing keepHistory to a low number and removing it completely but it doesn’t work.
Any idea whats the problem?

edit: actually i have to restart server to get the new data in

Found out why I had those issues, look in this thread:

https://forums.meteor.com/t/changes-arent-reactive/1611

Search works perfectly now.

I know it’s been awhile since you posted this, but this solution lead me in the right direction. Thank you for your help!

2 Likes