简体   繁体   English

mongodb:如何使用C#驱动程序创建文本索引?

[英]mongodb : How do I create text index with C# driver?

For mongodb, how can I create the following index in C# ? 对于mongodb,如何在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 我没有在http://api.mongodb.org/csharp/current/?topic=html/7e62224e-33ab-098b-4e07-797c45494a63.htm看到IndexOptions的任何“文本”选项。

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". 您需要通过脚本或直接在MongoDB数据库上设置它,因为C#驱动程序不会公开文本索引创建功能,因为它仍处于“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): 如果您创建了IndexKeysBuilder类的副本( 此处 ),并添加了一个新方法(如下所示):

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). 您可以使用它而不是内置的东西,理论上,它应该工作(我没有测试过)。

the easiest way to create text indexes in c# is by using the driver wrapper library MongoDB.Entities . 在c#中创建文本索引的最简单方法是使用驱动程序包装器库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. 还没有看到任何让它变得简单的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM