简体   繁体   中英

Case-insensitive string search in CosmosDB C# SDK

I'm trying to perform a case insensitive search using the C# Cosmos SDK, v3.11.0. From reading the CosmosDB blog post the SQL API now supports this - but I can't get it to work using the SDK. The release notes for 3.10.0 would suggest this is supported but I can't find any documentation on how to use it.

I've tried the following code but it does not perform a case-insensitive search:

query.Where(d => d.Name.Contains(name, StringComparison.InvariantCultureIgnoreCase));

Using the following code results in an exception being thrown:

query.Where(d => d.Name.StartsWith(name, StringComparison.InvariantCultureIgnoreCase))
Microsoft.Azure.Cosmos.Linq.DocumentQueryException: Method 'StartsWith' is not supported., Windows/10.0.18363 cosmos-netstandard-sdk/3.11.2
   at Microsoft.Azure.Cosmos.Linq.BuiltinFunctionVisitor.Visit(MethodCallExpression methodCallExpression, TranslationContext context)
   at Microsoft.Azure.Cosmos.Linq.StringBuiltinFunctions.Visit(MethodCallExpression methodCallExpression, TranslationContext context)
   at Microsoft.Azure.Cosmos.Linq.BuiltinFunctionVisitor.VisitBuiltinFunctionCall(MethodCallExpression methodCallExpression, TranslationContext context)
   at Microsoft.Azure.Cosmos.Linq.ExpressionToSql.VisitNonSubqueryScalarExpression(Expression inputExpression, TranslationContext context)
   at Microsoft.Azure.Cosmos.Linq.ExpressionToSql.VisitNonSubqueryScalarExpression(Expression expression, ReadOnlyCollection`1 parameters, TranslationContext context)
   at Microsoft.Azure.Cosmos.Linq.ExpressionToSql.VisitScalarExpression(Expression expression, ReadOnlyCollection`1 parameters, TranslationContext context)
   at Microsoft.Azure.Cosmos.Linq.ExpressionToSql.VisitScalarExpression(LambdaExpression lambda, TranslationContext context)
   at Microsoft.Azure.Cosmos.Linq.ExpressionToSql.VisitWhere(ReadOnlyCollection`1 arguments, TranslationContext context)

StringEquals is not yet supported in the SDK (yet). I found a message from someone in the Cosmos DB team confirming this (and noting the communication could be better).

I've also found it is not supported in the emulator either.

Support for the syntax you wrote is currently in the works . A pull request was started just a couple of hours ago .

Until then, the queries below work with the current SDK version (3.11), but they also should work with older versions. It should be considered a workaround for case insensitivity. The RU cost is higher than the native case insensitive search. I did a quick test, and the cost of ToLower().StartsWith(...) was almost twice the cost of the SQL equivalent.

string mySearchStringLowered = name.ToLower();
//...
query.Where(d => d.Name.ToLower() == mySearchStringLowered);
query.Where(d => d.Name.ToLower().StartsWith(mySearchStringLowered));

That is not the correct syntax. Please see the documentation here

Case Insensitive support is definitely in SDK 3.12.0:

string searchTerm = "do something";
var query = container.GetItemLinqQueryable<ToDoItem>();

query.Where(t => t.Description.StartsWith(searhTerm, StringComparison.InvariantCultureIgnoreCase));

Will run successfully, and return results where Description starts with things like "Do Something" or "do something".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM