matteodem / meteor-easy-search

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

Meteor.users collection #576

Closed zyonnetworks closed 7 years ago

zyonnetworks commented 7 years ago

Why doesn't this work? I know it's in http://matteodem.github.io/meteor-easy-search/docs/recipes/ and that's what i followed, but it always returns empty array []

I have this defined on the Client

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

const UsersIndex = new Index({
    collection: Meteor.users,
    fields: ['username', 'createdAt'],
    engine: new MongoDBEngine({
selectorPerField: function (field, searchString) {
      if ('emails' === field) {
        // return this selector if the email field is being searched
        return {
          emails: {
            $elemMatch: {
              address: { '$regex' : '.*' + searchString + '.*', '$options' : 'i' }
            },
          },
        }
      }

      // use the default otherwise
      return this.defaultConfiguration().selectorPerField(field, searchString)
    }
});
console.log(UsersIndex.search('').fetch());
matteodem commented 7 years ago

maybe your users don't have any emails?

zyonnetworks commented 7 years ago

There are emails, i even tried without searching for emails and still no luck. It should work out the box meaning no special subscriptions or publications, correct?

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

const UsersIndex = new Index({
    collection: Meteor.users,
    fields: ['username', 'createdAt'],
    engine: new MongoDBEngine({
    selectorPerField: function (field, searchString) {
      return this.defaultConfiguration().selectorPerField(field, searchString)
    }
});
console.log(UsersIndex.search('').fetch());
daorren commented 7 years ago

same here!

// verisons
easy:search@2.1.6
easysearch:components@2.1.5
easysearch:core@2.1.6
// indexes.js
export const MessagesIndex = new Index({
  collection: Messages,
  engine: new MongoDBEngine({
    //
  }),
  fields: ['content'],
})

// search.html
<template name="messagesSearch">
  {{> EasySearch.Input index=MessagesIndex }}
  <ul>
    {{#EasySearch.Each index=MessagesIndex}}
      <li>Content: {{content}}</li>
    {{/EasySearch.Each}}
  </ul>

  {{> EasySearch.LoadMore index=MessagesIndex}}

  {{#EasySearch.IfNoResults index=MessagesIndex}}
    <div class="no-results">No results found!</div>
  {{/EasySearch.IfNoResults}}
</template>

// search.js
Template.messagesSearch.helpers({
  MessagesIndex: function () {
    console.log(Messages.find({}).fetch());
    console.log(MessagesIndex.search('').fetch());
    return MessagesIndex;
  },
})

console.log(Messages.find({}).fetch()); returns something like [Document, Document, Document] console.log(MessagesIndex.search('').fetch()); returns an empty array [] all the time.

daorren commented 7 years ago

Well, switching to MinimongoEngine solved the problem.

It's weird, because no matter which engine I use, console.log(Messages.find({}).fetch()); shows the same results in my console.

And I wanna use beforePublish function in MongoDBEngine actually.

matteodem commented 7 years ago

how about searching for a letter? does that return users? It might be that the mongodb selector returned by selectorPerField is outdated.