MongoDB .NET驱动程序和文本搜索 [英] MongoDB .NET driver and text search
问题描述
我正在使用此MongoDB驱动程序: https://mongodb.github.io/mongo -csharp-driver/ 并且我想使用一个文本索引进行搜索,该索引(我认为)是在所有文本字段上创建的,如下所示:
I am using this MongoDB driver: https://mongodb.github.io/mongo-csharp-driver/ and I would like to search using a text index, which (I think) is created on all text fields like so:
{
"_fts" : "text",
"_ftsx" : 1
}
我正在使用linq查询来过滤数据,例如:
I am using linq queries to filter the data, example:
MongoClient client = new MongoClient(_mongoConnectionString);
IMongoDatabase mongoDatabase = client.GetDatabase(DatabaseName);
var aCollection = mongoDatabase.GetCollection<MyTypeSerializable>(CollectionName);
IMongoQueryable<MyTypeSerializable> queryable = aCollection.AsQueryable()
.Where(e=> e.Field == 1);
var result = queryable.ToList();
如何使用这种方法来进行文本搜索?
How do I utilize the text search using this method?
推荐答案
在C#MongoDB驱动程序中的PredicateTranslator
中,没有任何表达式可以转换为text
查询.因此,您将无法使用linq查询来实现text
查询.
Looking at the PredicateTranslator
within the C# MongoDB driver there isn't any expression that gets converted in to a text
query. So you won't be able to achive a text
query using a linq query.
- PredicateTranslator.cs
- PredicateTranslatorTests.cs
不过,您可以尝试使用Builder<>
进行文本搜索:
However you could try just doing a text search with the Builder<>
:
MongoClient client = new MongoClient(_mongoConnectionString);
IMongoDatabase mongoDatabase = client.GetDatabase(DatabaseName);
var aCollection = mongoDatabase.GetCollection<MyTypeSerializable>(CollectionName);
var cursor = await aCollection.FindAsync(Builders<MyTypeSerializable>.Filter.Text("search"));
var results = await cursor.ToListAsync();
有关文本过滤器的详细信息,请参见 https://docs.mongodb .com/manual/reference/operator/query/text/
Details about the text filter is here https://docs.mongodb.com/manual/reference/operator/query/text/
这篇关于MongoDB .NET驱动程序和文本搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!