Best way to create text index in MongoDB collection using C # driver

Using C # driver v1.9.0 (latest at time of writing) with MongoDB 2.6.0

What is the best way to create a text index in a collection using a C # driver?

From what I could say is this impossible through MongoCollection.CreateIndex? So, currently creating it using MongoDatabase.Eval as follows:

Database.Eval(new EvalArgs { Code = "function(){db.dummycollection.ensureIndex({\"$**\" : \"text\"},{name:\"TextIndex\"});}"

Am I missing something / is there a better way?

+4
source share
3 answers

This should work:

collection.EnsureIndex(IndexKeys.Text("a", "b").Ascending("c"), IndexOptions.SetTextLanguageOverride("idioma").SetName("custom").SetTextDefaultLanguage("spanish"));

https://jira.mongodb.org/browse/CSHARP-874

https://github.com/mongodb/mongo-csharp-driver/commit/1e7db3bedb3bee1b0ccecdb5f8ff39854526213a

+2

, 1.9.0 # MongoDB 2.6.0-rc2:

MongoCollection.CreateIndex(new IndexKeysDocument("Markdown", "text"));

( )

djch 1.9, , :

MongoCollection.CreateIndex(IndexKeys<MyClass>.Text(p => p.Markdown));
+4

The easiest way to create indices in C # is to use the MongoDB.Entities driver wrapper library . Here is an example of creating a text index:

    DB.Index<Author>()
      .Key(a => a.Name, Type.Text)
      .Key(a => a.Surname, Type.Text)
      .Create();

and for full-text search you just do:

    DB.SearchText<Author>("search term");

It doesn't get any easier than this :-)

0
source

Source: https://habr.com/ru/post/1538694/


All Articles