Cannot implement Search at Mongo on a Meteor application

I’m trying to build a search function for my app based on Meteor, following this tutorial

It all goes well, but where I’m indexing my collection and relevant fields in it, the server crashes with an error from MongoDB (v.2.6.7)

MongoError: Index with pattern: { _fts: "text", _ftsx: 1 } already exists with different options

When I completely erase the ensureIndex(), server runs and enables search but completely agnostic to the values I type. I mean no document in the collection I subscribed to appear regardless of what I searched.

What I’m doing wrong and where?

Thanks…

on Server:

Meteor.startup(function () {
        MyCollection._ensureIndex({
            "myField": "text"
        });
     });

Meteor.publish("publishedStuff", function(thesearch) {
    if (!thesearch) {
      return MyCollection.find({});
    }
    var cursor = MyCollection.find(
      { $text: {$search: thesearch} }
    );
    return cursor;
});

On Client:

Template.searchTemplate.events({
        "submit #searchform": function (e) {
          e.preventDefault();
          Session.set("thesearch", $("#thesearch").val());
        }
    });

Template.searchTemplate.helpers({
    myFunction: function() {
        Meteor.subscribe("publishedStuff", Session.get("thesearch"));
        if (Session.get("thesearch")) {
            return myCollection.find({});
      }
    }
});

does the app work for you the first time you run it, or is it a problem from the start?

Yes it’s works perfect otherwise.

well, why do you run the index code for every startup?

I also find it odd to subscribe in the helper? I think the subscribe should be in the onCreated of the template

Template.searchTemplate.onCreated(function(){
 var self = this;
 self.autorun(function(){
  self.subscribe("publishedStuff", Session.get("thesearch"));
 });
});
Template.searchTemplate.helpers({
 myFunction: function() {
  return myCollection.find({});
 }
});

You’re probably right with your points. But indeed I copied the code from the tutorial I linked in the first post. I’m only wondering why it works perfect with the search in his code, but not in mine… I only have changed the relevant parts of the code… The error makes me think that it is definitely a db issue. But don’t know how to fix…
Thanks for suggestions though. I’m only newbie here.

are you running the same versions as in the tutorial? (meteor 1.0.4+ , mongo 2.6)

I think you should also post at least a link to this discussion to the tutorial author’s blog so they can chime in.
My only other suggestion would be to try dropping the index in MongoDB, and then after the first time you Meteor app has started remove that _ensureIndex call from your code (e.g. put in somewhere in private so it doesn’t get loaded but you still have it for reference if you need it at some point, e.g. to use it through meteor shell).
Maybe _ensureIndex isn’t yet built to handle the case of a being called for a full-text search index.

Just to update: I have finally switched to use Arunoda’s Search Source solution. It worked perfect.

1 Like