创建索引
db.MyCollection.createIndex({'$**': 'text'}, {name: 'FullTextIndex'})
搜索匹配项
db.MyCollection.find({$text: {$search: 'myWord'}}).count()
结果为 1. 对于具有值“的字段我的话语在这里"
如果我定期搜索选定字段,如下所示,我会得到两条记录,其中一条记录的 Name = "我的话语在这里“第二条记录有”myWord“在详细信息中归档为”这里有东西,myWord 也在这里"
db.getCollection('MyCollection').find({
"$or":[{"Name":/myWord/i}, {"Details":/myWord/i}]
}).sort({"Name": 1})
如何重新创建索引,以便它在所有字段中作为 SQL 进行搜索,其中任何字段如 %searchText%
最后,如何在 C# 驱动程序中编写此搜索查询
Update:
我进一步查看了它。它正在查找具有带有前缀和后缀空格的搜索键但不属于单词中的字符串一部分的所有结果。
例如,它返回一个值的记录“你好,我的Word在这里“,但不返回”你好我的文字"
但根据这个文档,它必须支持通配符搜索。https://docs.mongodb.com/v3.0/reference/operator/query/text/ https://docs.mongodb.com/v3.0/reference/operator/query/text/
由于我还没有找到使用 Mongo 进行通配符搜索/全文搜索的太多帮助,因此我想出了一个解决方案来满足我的要求。
foreach (var doc in batch)
{
if (custDictionary.ContainsKey(projectId))
{
string concatenatedCustomFields = custFieldsList.Aggregate(string.Empty,
(current, custField) =>
current +
(ds.Tables[0].Columns.Contains(custField)
? (ds.Tables[0].Rows[i][custField].GetType().Name == typeof(DBNull).Name
? string.Empty
: ((string) ds.Tables[0].Rows[i][custField]).StripHtml())
: string.Empty));
doc.Add("CustomFieldsConcatenated", concatenatedCustomFields);
}
i++;
}
我读取每组文档的自定义字段列表,然后创建一个串联的 Mongo 字段,然后对该字段使用正则表达式查询。
然后为了提高查询的性能添加以下索引
_mongoConnect.Database?.GetCollection<BsonDocument>("MyCollectionName")
.Indexes.CreateOneAsync(new BsonDocument("CustomFieldsConcatenated", "hashed"), new CreateIndexOptions { Name = "CollectionName_FieldName_Index" });
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)