3

For mongodb, how can I create the following index in C# ?

db.reviews.ensureIndex( { comments: "text" } )

I don't see any "Text" option for IndexOptions at http://api.mongodb.org/csharp/current/?topic=html/7e62224e-33ab-098b-4e07-797c45494a63.htm

Jardalu
  • 231
  • 3
  • 9

2 Answers2

1

You'll need to set this up through a script or directly on the MongoDB database as the C# driver doesn't expose the text index creation feature as it's still in "beta".

Unfortunately, you can't easily override the behavior either ... as the classes that control the behavior aren't easily overriden/extensible.

If you created a copy of the IndexKeysBuilder class (here), and added a new method (something like below):

public IndexKeysBuilder Text(string name)
{
    _document.Add(name, "text");
    return this;
}

You could use that instead of the built in stuff and in theory, it should work (I've not tested this).

WiredPrairie
  • 58,954
  • 17
  • 116
  • 143
  • Adding the new method didn't work for me. I will add script to add the index to the collection. – Jardalu Oct 24 '13 at 00:31
  • It threw the following exception. `MongoDB.Driver.WriteConcernException : WriteConcern detected an error 'Unknown index plugin 'Description' in index { text: "Description" }'. (Response was { "err" : "Unknown index plugin 'Description' in index { text: \"Description\" }", "code" : 16734, "n" : 0, "connectionId" : 130, "ok" : 1.0 }).` From the documentation (http://docs.mongodb.org/manual/release-notes/2.4-index-types/#index-type-validation) it seems, that happen when the index is not supported. – Jardalu Oct 24 '13 at 02:56
  • Interesting. The error looks like it has it backwards, as I'm guessing the field you want to index is called `description`? – WiredPrairie Oct 24 '13 at 10:39
0

the easiest way to create text indexes in c# is by using the driver wrapper library MongoDB.Entities. here's an example of creating a text index:

    DB.Index<Review>()
      .Key(a => a.Comment, Type.Text)
      .Create();

haven't seen anything else that makes it simpler than that.

Dĵ ΝιΓΞΗΛψΚ
  • 5,068
  • 3
  • 13
  • 26