aqueduct icon indicating copy to clipboard operation
aqueduct copied to clipboard

Case-insensitive search

Open devnasirali opened this issue 9 years ago • 4 comments

I am using Postgres as database and trying to search for records but it is currently case-sensitive. How can I do the case-insensitive search e.g "Allen" vs "allen" should return same results:

var query = new Query<Table>() ..matchOn.name = whereBeginsWith(params["q"]) ..fetchLimit = 10;

devnasirali avatar Mar 23 '17 02:03 devnasirali

You have a few options, you could store and search all of you data in lowercase which is what I do with email addresses. Or you could make a custom predicate, in your case the query would be: var query = new Query<Table>() ..predicate = new QueryPredicate("name ILIKE @q", {"q" : params["q"]}) ..fetchLimit = 10; Keep in mind that ILIKE is not a standard SQL operator and does not use indexes.

anachlas avatar Mar 23 '17 12:03 anachlas

Or you could do: var query = new Query<Table>() ..predicate = new QueryPredicate("LOWER(name) = @q", {"q" : params["q"].toLowerCase()}) ..fetchLimit = 10; I think that would be better actually.

anachlas avatar Mar 23 '17 13:03 anachlas

In 2.0, any matcher that can be used for a string has a caseSensitive optional parameter.

var query = new Query<Table>()
  ..where.name = whereBeginsWith(params["q"], caseSensitive: false);

If you're still using 1.0, I very much suggest moving to 2.0. Otherwise, Alex's advice should work.

itsjoeconway avatar Mar 23 '17 13:03 itsjoeconway

Oh I didn't know that was an option now, very cool!

anachlas avatar Mar 23 '17 13:03 anachlas