MongoDB的全文检索 [英] MongoDB full text search

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

问题描述

CREATE INDEX

Create index

db.MyCollection.createIndex({'$**': 'text'}, {name: 'FullTextIndex'})

搜索匹配

db.MyCollection.find({$text: {$search: 'myWord'}}).count()

结果为1的申请有值 myWord是这里

如果我的如下所选字段做常规的搜索,我得到两个记录,一个记录有NAME = myWord是这里和第二条记录有 myWord 的详细信息这里的东西,myWord是这里作为归档

If I do regular search for selected fields as following, I get two records, one record has Name = "myWord is here" and second record has "myWord" in Details filed as "something here and myWord is here"

db.getCollection('MyCollection').find({  
     "$or":[{"Name":/myWord/i}, {"Details":/myWord/i}]
    }).sort({"Name": 1})

我怎样才能重新创建索引,使其在各个领域的搜索如SQL,其中任何领域一样%SEARCHTEXT%

How can I recreate the index so that it search in all fields as SQL where any field like %searchText%

和最后,我怎么能写在C#驱动这个搜索查询

And Finally how can I write this search query in C# Driver

更新:

我进一步看着它。它被发现具有前缀和后缀的空间的搜索键,但不是在一个字中的字符串的一部分的所有结果。

I further looked in to it. it is finding all results that has a search key with prefix and suffix spaces, but not part of the string in a word.

实施例它返回一个记录一个值您好myWord是这里,但不会返回 HellomyWord

Example it is returning a record for a value "Hello myWord is here", but doesn't return "HellomyWord"

但是,根据这个文件,它支持通配符搜索。 https://docs.mongodb.com/v3.0/reference/operator/query/text /

But according to this document, it has to support wildcard search. https://docs.mongodb.com/v3.0/reference/operator/query/text/

推荐答案

由于我还没有发现,使用蒙戈通配符搜索/全文搜索太大的帮助,我想出了一个变通为我的要求。

Since I haven't found, much help with wildcard search/Full text search using Mongo, I have come up with a work around for my requirement.

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++;
 }



我读自定义字段列表每个组的文件,然后创建一个级联蒙戈字段,然后使用正则表达式查询该字段。

I read list of custom fields for each group of documents, then create a concatenated Mongo Field, then use Regex query on that field.

然后以提高查询性能添加了以下指标

Then to improve the performance of the query added following index

  _mongoConnect.Database?.GetCollection<BsonDocument>("MyCollectionName")
                .Indexes.CreateOneAsync(new BsonDocument("CustomFieldsConcatenated", "hashed"), new CreateIndexOptions { Name = "CollectionName_FieldName_Index" });

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

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