matteodem / meteor-easy-search

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

Does this still work with collection-helpers for composite fields? #641

Closed jamauro closed 5 years ago

jamauro commented 5 years ago

I'm following the Usage with collection-helpers example in the guide: http://matteodem.github.io/meteor-easy-search/docs/recipes/

Does anyone have an example of how they've gotten easy-search to work with collection-helpers? I can't seem to return any search results when searching on a composite field created with collection-helpers.

Here's what my setup looks like:

Relevant versions

Meteor 1.7.0.3

easy:search@2.2.1
easysearch:components@2.2.1
easysearch:core@2.2.0

dburles:collection-helpers@1.1.0

Code in my /lib/collections folder

import { Index, MongoDBEngine } from 'meteor/easy:search'

Channels = new Mongo.Collection("channels");

// using collection-helpers
Channels.helpers({
  convertedName() {
    if (this.name) {
      return this.name
    } else {
      var memberIdsWithoutCurrentUser = _.without(this.members, Meteor.userId());
      var userNamesNotIncludingCurrentUser = Meteor.users.find({_id: {$in: memberIdsWithoutCurrentUser}}).map(function(user) {
        return user.username
      });
      return userNamesNotIncludingCurrentUser.join(", ")
    }
  }
});

// want to search by convertedName created with collection-helpers
ChannelsIndex = new Index({
  collection: Channels,
  fields: ['convertedName'],
  engine: new MongoDBEngine({
    transform: (doc) => Channels._transform(doc)
  })
});
matteodem commented 5 years ago

It should still work. On what environment is the code run? Server and / or client?

jamauro commented 5 years ago

Cool. I created the Collection, Index, and the collection helper in the lib/collections folder so those should be running on both server and client. Anything that jumps out at you with the code above or is there any other detail / logs that I can provide that might help diagnose?

matteodem commented 5 years ago

okay so now I see it. You can't search for the convertedName (see fields configured as convertedName), because easy-search uses a mongo selector which does a search on the mongo documents.

I'd recommend having a denormalization process before inserting or updating a doc or some other way to have that field available in the actual document.

jamauro commented 5 years ago

I see. Can you help me understand how my use of convertedName differs from the fullName in the example in the docs?

screen shot 2019-01-23 at 10 22 47 am

matteodem commented 5 years ago

I guess I should add docs to explain that the "fullName" is an actual field on the mongodb document and that the transform is only useful for reading the data.

jamauro commented 5 years ago

Got it. Thanks for your help!