[CSHARP-2566] How do I run a full text search on a property using the C# LINQ Driver? Created: 29/Mar/19  Updated: 27/Oct/23  Resolved: 30/May/19

Status: Closed
Project: C# Driver
Component/s: Linq
Affects Version/s: 2.8.0
Fix Version/s: None

Type: Task Priority: Critical - P2
Reporter: Vijay Devappa Assignee: Wan Bachtiar
Resolution: Works as Designed Votes: 0
Labels: question
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

I have a text index defined like this:

{ Builders<ApplicationLog>.IndexKeys.Ascending(k => k.ApplicationId).Text(k => k.LogMessage).Descending(k => k.LastModifiedTimeTicks), base.DefaultOptions("aid_lm_lmtt_nsprsc_index") },   //Can only have one text index.

How do I use LINQ to run a search which utilizes this text index?

I have searched everywhere, cannot figure it out.

 



 Comments   
Comment by Richard Collette [ 31/Jul/19 ]

@vijay1979 - How would you project MetaTextScore when using IMongoQueryable?

Comment by Wan Bachtiar [ 24/May/19 ]

How do I use LINQ to run a search which utilises this text index?

Hi Vijay,
Thanks for posting the workaround.

Currently the driver does not have LINQ support yet for text search. An alternative workaround is to use the builders instead of LINQ. Example:

    var cursor = collection.Find(Builders<BsonDocument>.Filter.Text("text to find"));

Regards,
Wan.

Comment by Vijay Devappa [ 01/Apr/19 ]

This is the workaround which works:

 

public static IMongoQueryable<T> WhereText<T>(this IMongoQueryable<T> query, string search)
{
    //REF: https://stackoverflow.com/questions/40915209/mongodb-net-driver-and-text-search/            
    var filter = Builders<T>.Filter.Text(search);
    return query.Where(_ => filter.Inject());
}
 
//Has to be used as the first where clause:
 
IMongoQueryable<ApplicationLog> query = (from p in collection.AsQueryable<ApplicationLog>()
                                              select p);            
 
if (!filter.IsNullOrEmpty())
{
    query = query.WhereText(filter);
}

Generated at Wed Feb 07 21:42:54 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.