EasySearch sorting


#1

Before implementing EasySearch i used to display my items sorted by createdAt like this:

return Items.find({}, { sort: { createdAt: -1 } })

  {{#each items}}
          {{> Item}}
  {{/each}}

Now, using EasySearch i have something like this

  {{#EasySearch.Each index=itemsIndex }}
          {{> Item}}
  {{/EasySearch.Each}}-->

, but the sorting is not working anymore.

Is there any way i could work with EasySearch and also display my items in a desired order?


#2

You can specify your sorting when you create your EasySearch.Index. See the Adding sorting to your app section of the docs:

const carSearchIndex = new EasySearch.Index({
  collection: carCollection,
  fields: ['name', 'companyName'],
  defaultSearchOptions: {
    sortBy: 'relevance'
  },
  engine: new EasySearch.MongoDB({
    sort: function (searchObject, options) {
      const sortBy = options.search.props.sortBy;

      // return a mongo sort specifier
      if ('relevance' === sortBy) {
        return {
          relevance: -1
        };
      } else if ('newest' === sortBy) {
        return {
          createdAt: -1
        };
      } else if ('bestScore' === sortBy) {
        return {
          averageScore: -1
        };
      } else {
        throw new Meteor.Error('Invalid sort by prop passed');
      }
    }
  })
});

#4

@hwillson hanks again! It works :grin:


#5

Is there any way i could do this with : engine: new EasySearch.Minimongo() ?

By using your example

engine: new EasySearch.MongoDB({})

i notice in MeteorToys that Meteor creates a new collection named items/easySearch, and that makes all the other features i have already implemented that use the items colelction stop working :neutral_face: