Full Text MongoDB Search

Create Index

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

Matching

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

Result 1. for a field that has the value " myWord here "

If I perform a regular search on the selected fields as follows, I get two records, one record has a name = " myWord here " and the second record has " myWord in the Details filed as" something here, and myWord here "

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

How can I recreate an index so that it searches all fields like SQL, where any field, like% searchText%

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

Update:


I also looked at him. it finds all results with a search key with prefix and suffix spaces, but is not part of a line in a word.

Example: it returns an entry for Hello myWord here ", but does not return" HellomyWord "

But according to this document, it should support pattern matching. https://docs.mongodb.com/v3.0/reference/operator/query/text/

+7
c # mongodb
source share
2 answers

Since I did not find much help in finding wildcards / Full-text search using Mongo, I came up with a job 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 a list of custom fields for each group of documents, and then create a concatenated Mongo Field, and then use the Regex query in that field.

Then, to improve the performance of a query added by index

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

Note that wildcards can be included in regular expression searches, but not in full-text searches.

For this, SERVER-10227 already has a ticket requesting a function. If this is an important feature for you, please skip the ticket.

Further adding to your comment:

I also looked at him. it finds all results with a search key with prefix and suffix spaces, but is not part of a line in a word.

Example: it returns an entry for the value "Hello myWord here", but does not return "HellomyWord"

The $ text operator matches a full phrase. There are currently no tools for handling index positions in strings that return a meaningful value for a projection. In your use case, searching for the term "myWord" will not match "HellomyWord". However, it matches a document containing, for example, โ€œhi myWord hereโ€ or โ€œmyWordโ€, etc.

See the Match Operation page for more information.

+2
source share

All Articles