NET中MongoDB中的全文本搜索 [英] Full text search in mongodb in .net

查看:64
本文介绍了NET中MongoDB中的全文本搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在.net mvc中的mongodb的特定集合中的所有文档中搜索内容.我已经尝试过像这里一样成功创建mongodb shell的索引.

I have to search contents in all documents in particular collection of mongodb in .net mvc . I have tried with mongodb shell by creating index successfully like here .

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

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

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

It works fine . but when i put it in .net that gives me error . I googled it and got following solution for indexing .

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

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

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

I am trying in .net as following way .

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

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

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

but I am getting error on first line "represents text as series of unicode ....syntax error "

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

2nd line error "There is no argument given that corresponds to required formal parameters " And "unexpected character $ ".

任何建议将不胜感激.

any suggestion will be appreciated .

推荐答案

我可以使用以下命令创建文本索引:

I could create text indexes with this command:

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

然后我可以这样查询索引:

And than i could query index this way:

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

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

searchFileByAuthor is just my fake class with subject field:

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

这篇关于NET中MongoDB中的全文本搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆