matteodem / meteor-easy-search

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

How to search from the start of the searchString? #661

Closed jamauro closed 2 years ago

jamauro commented 2 years ago

This is a follow up to this older issue: https://github.com/matteodem/meteor-easy-search/issues/95

I'm using selectorPerField to search from the start of the searchString. It gets the job done but is there a better way to do it?

...
fields: ['symbol', 'name'],
engine: new MongoDBEngine({
    selectorPerField: function (field, searchString) {
      if (field === 'name') {
        return {
          name: { '$regex' : '^' + searchString + '.*', '$options' : 'i' }
        }
      }

      if (field === 'symbol') {
        return {
          symbol: { '$regex' : '^' + searchString + '.*', '$options' : 'i' }
        }
      }

      // use the default otherwise
      return this.defaultConfiguration().selectorPerField(field, searchString)
    }
  })
matteodem commented 2 years ago

Hi there, if you want to use MongoDB than that seems to be the preferred way to go. It's even better to use ElasticSearch, if performance is a key factor for your app. Here's the library for that: https://github.com/matteodem/meteor-easy-search/tree/master/packages/easysearch_elasticsearch

Cheers!

jamauro commented 2 years ago

Thanks! Will check it out.