c# – MongoDB全文搜索
发布时间:2020-12-15 07:44:34 所属栏目:百科 来源:网络整理
导读:创建索引 db.MyCollection.createIndex({'$**': 'text'},{name: 'FullTextIndex'}) 搜索匹配 db.MyCollection.find({$text: {$search: 'myWord'}}).count() 对于具有“myWord is here”值的字段,结果为1. 如果我经常搜索所选字段如下,我得到两条记录,一条记
创建索引
db.MyCollection.createIndex({'$**': 'text'},{name: 'FullTextIndex'}) 搜索匹配 db.MyCollection.find({$text: {$search: 'myWord'}}).count() 对于具有“myWord is here”值的字段,结果为1. 如果我经常搜索所选字段如下,我得到两条记录,一条记录名称=“myWord就在这里”,第二条记录中的“myWord”在详细信息中归档为“这里的东西,myWord就在这里” db.getCollection('MyCollection').find({ "$or":[{"Name":/myWord/i},{"Details":/myWord/i}] }).sort({"Name": 1}) 如何重新创建索引,以便在所有字段中搜索SQL,其中任何字段如%searchText% 最后,我如何在C#Driver中编写此搜索查询 更新: 我进一步调查了它.它找到所有带有前缀和后缀空格的搜索键的结果,但不是单词中字符串的一部分. 示例它返回值“Hello myWord is here”的记录,但不返回“HellomyWord” 但根据这份文件,它必须支持通配符搜索. 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" }); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |