Search code examples
javascriptsearchmeteorcollectionfs

How to search over an FS Collection?


Anybody can help me out on this? I have a group of PDF documents that I would like to be able to search. I've attempted use matteodem:easy-search and meteorhacks:search-source, but both of them only handle Meteor Collections and not FS. I can get both of these packages working with regular text. I just want to return the results of the search query based off the name field in the FS Collection.

Any help would be greatly appreciated!


Solution

    1. Install if you haven't already the reactive-var and the reactive-dict packages if you havent done that already
    2. Create a reactive variable (it could also be a Session variable) searchString
    3. Wrap your item display in an {{#each chosenFiles}} where chosenFiles is your helper name.
    4. Create an helper chosenFiles where you call your search function and provide it a cursor from your file collection.

      "chosenFiles": function() {
          return filterFiles(yourFileCollection.find());
      }
      
    5. Create the filterFiles function where, basically, you get back your searchString and use it to filter your results, returning everything if your searchString is empty. Here is an example using reactive-dict:

      var pageSession = new ReactiveDict();
      
       filterFiles= function(cursor) {
          if(!cursor) {
              return [];
          }
          var searchString = pageSession.get("SearchString");
          var raw = cursor.fetch();
      
          // filter
          var filtered = [];
          if(!searchString || searchString == "") {
              filtered = raw;
          } else {
              searchString = searchString.replace(".", "\\.");
              var regEx = new RegExp(searchString, "i");
              var searchFields = ["original.name", "original.type"];
              filtered = _.filter(raw, function(item) {
                  var match = false;
                  _.each(searchFields, function(field) {
                      var value = (getPropertyValue(field, item) || "") + "";
                      match = match || (value && value.match(regEx));
                      if(match) {
                          return false;
                      }
                  })
                  return match;
              });
          }
          return filtered;
      };
      
    6. Attach the searchString definition to, for instance, your keyUp event so that you get a reactive search.

      "keyup .yourTextBox": function(e, t) {
          var searchString = e.currentTarget.value;
          switch (e.which) {
              //pressing esc clear the textbox
              case 27:
              e.currentTarget.value = "";
              pageSession.set("SearchString", "");
              break;
      
              default:
              pageSession.set("SearchString", searchString);
      }
      

    I am not 100% sure that "original.name" is the right way to access the name field since I don't use collectionFS but it seems it is, according to the doc