【问题标题】:Full text search in mongodb in .net.net 中的 mongodb 中的全文搜索
【发布时间】:2017-05-12 09:53:02
【问题描述】:

我必须搜索所有文档中的内容,特别是 .net mvc 中的 mongodb 集合。我已经通过像这里一样成功创建索引来尝试使用 mongodb shell。

db.collection_name.createIndex( { subject: "text" } )

db.collection_name.find( { $text: { $search: "search_word" } } )

它工作正常。但是当我把它放在 .net 中时,这给了我错误。我用谷歌搜索并得到了以下索引解决方案。

 collection.EnsureIndex(new IndexKeysBuilder().Ascending("subject"));

现在我该如何运行这个查询db.collection_name.find( { $text: { $search: "coffee" } } )

我正在 .net 中尝试以下方式。

collection.CreateIndex("subject":"text");

var query = collection.Find({ $text: { $search: "coffe" }}); 

但我在第一行出现错误“将文本表示为一系列 unicode ....语法错误”

第二行错误“没有给出与所需形式参数相对应的参数”和“意外字符$”。

任何建议将不胜感激。

【问题讨论】:

  • 您需要显示 .NET 代码,当您说它“给出错误”时 - 什么错误?
  • var collection = Database.GetCollection("articles"); collection.EnsureIndex(new IndexKeysBuilder().Ascending("subject")); var query = collection.find( { $text: { $search: "coffee" } } );
  • 最后一行的错误我认为我们不能像在 mongodb shell 中那样直接编写查询。所以我必须得到.net 的解决方案才能在 mongodb 中进行全文搜索

标签: c# .net mongodb mongodb-query mongodb-.net-driver


【解决方案1】:

我可以用这个命令创建文本索引:

collection.Indexes.CreateOne(Builders<searchFileByAuthor>.IndexKeys.Text(x=>x.subject));

而且我可以这样查询索引:

collection.Find(Builders<searchFileByAuthor>.Filter.Text("coffe")).ToList();

searchFileByAuthor 只是我的带有主题字段的假课程:

public class searchFileByAuthor
{
    public int Id { get; set; } 
    public string subject { get; set; } 
}

【讨论】:

  • 我试过了,但是出错了。 "参数 1:无法从 'MongoDb.Driver.Filter.Definition' 转换为 'MongoDb.Driver.IMongoQuery' 。
  • 您要执行的命令到底是什么?我已经更新了我的答案。
  • 谢谢。我只想在文件或文档中搜索。这个查询我想执行.. collection.CreateIndex(subject:"text"); var query = collection.Find({ $text: { $search: "coffe" }});
  • 我已运行此查询,但索引有错误。我确信我的模型类中有主题字段(例如 searchFileByAuthor),正如您在此处描述的那样。但是索引上的错误光标说“MongoCollection”不包含“索引”的定义,并且找不到接受 MongoCollection 的第一个参数的扩展方法“索引”。
  • 您使用什么版本的 mongo c# 驱动程序以及您的 mongodb 版本是什么?我使用 mongo 驱动程序 2.4.0 和 mongodb 版本 3.4
【解决方案2】:
public List<T> FindSearch<T>(string collectionName, string searchWord) {
    IMongoQuery query = Query.Text(searchWord);
    List<T> find = getCollection<T>(collectionName).Find(query).ToList();
    return find;
}

【讨论】:

    【解决方案3】:

    Maksim Simkin 的答案是正确的,尽管它已经过时了。 更新后的版本是:

    collection.Indexes.CreateOne(new CreateIndexModel<YourClass>(Builders<YourClass>.IndexKeys.Text(x => x.something)));
    

    或者,如果您想使用通配符索引(索引整个文档),您可以这样做:

    collection.Indexes.CreateOne(new CreateIndexModel<YourClass>(Builders<YourClass>.IndexKeys.Text("$**")));
    

    或者您可能出于某种原因想要/拥有更多索引,而不是这样做:

    var indexWildcardTextSearch = new CreateIndexModel<YourClass>(Builders<YourClass>.IndexKeys.Text("$**"));
    
    List<CreateIndexModel<YourClass>> indexes = new List<CreateIndexModel<YourClass>>();
    indexes.Add(indexWildcardTextSearch);
    
    collection.Indexes.CreateMany(indexes);
    

    而查询,它保持不变:

    collection.Find(Builders<YourClass>.Filter.Text("something")).ToList();
    

    【讨论】:

      猜你喜欢
      • 2013-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-12
      • 2018-10-26
      • 1970-01-01
      相关资源
      最近更新 更多