matteodem / meteor-easy-search

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

only return some of the fields from collection with MongoDB engine #507

Closed DylanKojiCheslin closed 7 years ago

DylanKojiCheslin commented 8 years ago

Hi I'm trying to only return the portions of the data needed from a collection. But I can't figure out how to do this in easy search.

This is a comparable Collection find. note that data is only return data for some fields, using the options fields property to limit

Agenda.find(
//selector
      {agendaState : {$eq:'published'},
      createdBy : {$ne: this.userId}
      },
//options
      {fields:{
          agendaState: 1 ,
          createdBy: 1 ,
          questions: 1 ,
          publishedDate: 1 ,
          requestorAvailabilities: 1
        }
      }
    );

here is the index I have now. it works fine other than returning all the fields

AnwserIndex = new EasySearch.Index({
  collection: Agenda,
  fields: ['questions.question.context'],
  name: 'AnwserIndex',
  engine: new EasySearch.MongoDB(
    {
    sort: function () {
      return {
        publishedDate: 1
      }
    },
    selector: function (searchObject, options, aggregation) {
      // retrieve the default selector
      selector = this.defaultConfiguration().selector(searchObject, options, aggregation);
     //filter
      selector.agendaState = {$eq:'published'};
      selector.createdBy = {$ne: this.userId};
    return selector;
    },
    permission: function (options) {
      return (!!Meteor.userId) // type converted to boolean
    },
    defaultSearchOptions: {
      sortBy: 'relevance'
    },
  }),
});
tarunbatra commented 8 years ago

:+1: facing similar issue. I think there's engine.fields method just like engine.sort, but not sure how to use it. Anyone?

matteodem commented 8 years ago

http://matteodem.github.io/meteor-easy-search/docs/engines/ Here's the docs. Can you let me know what is unclear to you?

tarunbatra commented 8 years ago

@matteodem I checked the docs, but it's unclear how the return value of fields method in MongoDB engine configuration should look like. screenshot from 2016-08-17 18-14-49

Appreciate your reply!

matteodem commented 8 years ago

It should be the same as here http://docs.meteor.com/api/collections.html#fieldspecifiers. does that work?

tarunbatra commented 8 years ago

Great! I was confused because the fields property in Index expects an array. Thanx @matteodem, I think that resolves the issue!

matteodem commented 8 years ago

Cool, I'll add docs to clarify that.