matteodem / meteor-easy-search

Easy-to-use search for Meteor with Blaze Components
MIT License
436 stars 68 forks source link

Searches automatically at every page reload, ignores timeout, event and allDocsOnEmpty #551

Closed cosmin-novac closed 7 years ago

cosmin-novac commented 7 years ago

Hello everyone,

I have a relatively simple search setup. I want to search through a characters Collection - and I need only the characters with a certain "storyId".

The problem is, the {{> EasySearch.Input }} is always active, always searching on page reload, even though it's set up like this: "timeout=500 event=enter allDocsOnEmpty=false". I would expect it to not search anything if the input field is empty and/or if the user hasn't pressed enter.

This is my index:

   'collection': Collections.Characters,
   'fields': ['name', 'description'],
   'name' : 'CharacterIndex',
   'engine': new EasySearch.MongoDB({
      selector: function(searchObject, options, aggregation) {
         var selector = this.defaultConfiguration().selector(searchObject, options, aggregation);
         selector.storyId = User.get(options.search.userId).getCurrentStoryId();
         return selector;
      }
   }),
  permission(options) {
    return options.userId;
  }
});

And this is the template:

        <div class="search-input panel-body col-md-11" autofocus>
            {{> EasySearch.Input index=CharacterIndex timeout=500 event=enter allDocsOnEmpty=false}}
        </div>
        <div>
            {{#EasySearch.IfNoResults index=CharacterIndex}}
                <div class="search-results">No characters found</div>
            {{else}}
                <div class="search-results">Characters</div>
            {{/EasySearch.IfNoResults}}
            <ul>
                {{#EasySearch.Each index=CharacterIndex}}
                    <li class="searchResultLink"><a href="/myApp/character/{{__originalId}}">{{name}}</a></li>
                {{/EasySearch.Each}}
            </ul>
            {{> EasySearch.LoadMore index=CharacterIndex attributes=loadMoreAttributes}}
        </div>

The permanent searching is slowing down my app considerably. How can I solve this? I was considering skipping the default template elements and manually triggering the search, but maybe there's a better solution I'm not seeing?

cosmin-novac commented 7 years ago

This is even worse if I try to add multiple collections, then there is a lot of clipping and there are tens of unnecessary searches started on each page load and each search - each collection is searched at least a few times.

matteodem commented 7 years ago

can you provide a simple reproduction repository?

cosmin-novac commented 7 years ago

Hm, sorry, the app is quite complex and I would have to build an entirely new app from scratch for a repo. Perhaps there is anything out of the ordinary in the code above that I could provide more info on and/or more code snippets?

matteodem commented 7 years ago

try removing the {{else}} block in your example code? Does that help?

cosmin-novac commented 7 years ago

I'm afraid removing the {{else}} block didn't change the behaviour. I solved the problem with a workaround now, by handling the search helper manually, and replacing the EasySearch.Input field with a normal input field.

searchResultsCharacter: function() {
        var searchTerm = Session.get("searchTerm");
        if (searchTerm==null || searchTerm=="") 
            return null
        else
            return CharacterIndex.search(searchTerm).fetch();
    },