Search code examples
c#couchbasesql-likecase-insensitivecouchbase-lite

Couchbase Lite 2.0.0 C# case insensitive query


I'm using Couchbase Lite 2.0.0 in my app and am trying to do a case insensitive string search for terms which contain a given phrase e.g. if the value is hello, world! then the phrase o, w should return it but ow should not.

According to the couchbase docs is should be able to achieve this with the LIKE operator and wildcard matches:

Note that since like does a case insensitive match, the following query will return "landmark" type documents with name matching "Royal Engineers Museum", "royal engineers museum", "ROYAL ENGINEERS MUSEUM" and so on.

So my code is;

...
.Where(Expression.Property(myPropertyName).Like(Expression.String($"%{phrase}%"));
...

This works as expected EXCEPT it is still case sensitive.

I've tried using .Collate(Collation.ASCII().IgnoreCase(true)

I've also tried using the REGEX operator with (?i) mode but this caused a panic.

I'm thinking of using the Full Text Search functionality but this is probably overkill for this simple situation.

Is this a bug or is there something I'm missing?


Solution

  • They recently changed it to be case sensitive!

    To make it case insensitive they are now advising use of Function.Lower like this:

    ...
    .Where(Function.Lower(Expression.Property(myPropertyName))
        .Like(Expression.String($"%{phrase?.ToLower()}%"));
    ...
    

    It seems a bit hacky. Hopefully you don't have to deal with any Turkish i's!

    For more info see...

    Couchbase lite 2.0., DB22, like became case sensitive?

    Introducing the Query Interface in Couchbase Mobile 2.0

    Case Insensitive like