matteodem / meteor-easy-search

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

defaultSearchOptions sortBy: not working #564

Closed ramsaymax closed 7 years ago

ramsaymax commented 7 years ago

Hey Matt,

I'm having a weird issue with sorting in easysearch that I've spent hours trying to fix.

I've basically copied the example "Add sorting to you app" line for line exactly:

http://matteodem.github.io/meteor-easy-search/docs/recipes/

it works great, the only issue is that when the app loads it does not load documents by default. Only when I select a sort from the drop down will it load documents.

The moment I remove the sort logic and add a basic return, default documents are there.

I believe it has something to do with this code:

sort: function (searchObject, options) {
      const sortBy = options.search.props.sortBy
      if ('relevance' === sortBy) {
        return {
          'establishment.visitCount': -1,
        }
      } else if ('followers' === sortBy) {
        return {
          'establishment.RatingsCount': -1,
        }
      } else if ('growth' === sortBy) {
        return {
          'establishment.DemoterCount': -1,
        }
      } else {
        throw new Meteor.Error('Invalid sort by prop passed')
      }
    }

I've tried passing in the defaultSearchOptions too:

import { PubData } from './WarmPub.js';
import { EasySearch } from 'meteor/easy:search';
import { _ } from 'meteor/underscore';

export const PubIndex = new EasySearch.Index({
  engine: new EasySearch.MongoDB({
    sort: function (searchObject, options) {
      const sortBy = options.search.props.sortBy
      // return a mongo sort specifier
      if ('relevance' === sortBy) {
        return {
          'establishment.visitCount': -1,
        }
      } else if ('positive' === sortBy) {
        return {
          'establishment.RatingsCount': -1,
        }
      } else if ('negative' === sortBy) {
        return {
          'establishment.DemoterCount': -1,
        }
      } else {
        throw new Meteor.Error('Invalid sort by prop passed')
      }
    },
    selector: function (searchObject, options, aggregation) {
      let selector = this.defaultConfiguration().selector(searchObject, options, aggregation),
        categoryFilter = options.search.props.categoryFilter;
      if (_.isString(categoryFilter) && !_.isEmpty(categoryFilter)) {
        selector.category = categoryFilter;
      }
      return selector;
    }
  }),
  collection: PubData,
  fields: ['establishment.name','establishment.visitCount'],
  // fields: ['name'],
    defaultSearchOptions: {
      sortBy: 'relevance',
    limit: 32
  },
permission: () => {
    //console.log(Meteor.userId());
    return true;
  }
});

and also wont work

I think the documentation might be lacking in an explanation for this

matteodem commented 7 years ago

Are you using easy search components? Also notice that the easy search leaderboard loads documents by default

matteodem commented 7 years ago

what was the problem? 😄

ramsaymax commented 7 years ago

Hey!

I changed:

sort: function (searchObject, options) {
      const sortBy = options.search.props.sortBy
      if ('relevance' === sortBy) {
        return {
          'establishment.visitCount': -1,
        }
      } else if ('followers' === sortBy) {
        return {
          'establishment.RatingsCount': -1,
        }
      } else if ('growth' === sortBy) {
        return {
          'establishment.DemoterCount': -1,
        }
      } else {
        throw new Meteor.Error('Invalid sort by prop passed')
      }
    }
sort: function (searchObject, options) {
      const sortBy = options.search.props.sortBy
      if ('relevance' === sortBy) {
        return {
          'establishment.visitCount': -1,
        }
      } else if ('followers' === sortBy) {
        return {
          'establishment.RatingsCount': -1,
        }
      } else if ('growth' === sortBy) {
        return {
          'establishment.DemoterCount': -1,
        }
      } else {
        throw new Meteor.Error('Invalid sort by prop passed')
      }
    }

to

    sort: function (searchObject, options) {
      const sortBy = options.search.props.sortBy

      // return a mongo sort specifier
      if ('relevance' === sortBy) {
        return {
          '_source.accounts.influencer_score': -1,
        }
      } else if ('followers' === sortBy) {
        return {
          '_source.total_followers': -1,
        }
      } else if ('growth' === sortBy) {
        return {
          '_source.total_followers_growth': -1,
        }
      } else {
        return {
          '_source.accounts.influencer_score': -1,
        }
      }
    },