How can I do a 'like' query or similarity search?

Hi, I am looking to do fuzzy search. So, for example , if the user types in 'dan'. I'd like to return all users whose names or emails include 'dan'. I read through the predicate list here: https://hexdocs.pm/ash/Ash.Filter.html hoping for something like 'like' in SQL, but I didnt see naything. What is the best approach? Here is how my code looks currently: App.EmailHandler.User |> Ash.Query.filter( [or: [ [email: search_slug], [name: search_slug] ]])
6 Replies
Alan Heywood
Alan Heywood2y ago
You could use an expression for this:
App.EmailHandler.User
|> Ash.Query.filter(
expr(
ilike(email, "%" <> ^search_slug <> "%") ||
ilike(name, "%" <> ^search_slug <> "%")
)
)
App.EmailHandler.User
|> Ash.Query.filter(
expr(
ilike(email, "%" <> ^search_slug <> "%") ||
ilike(name, "%" <> ^search_slug <> "%")
)
)
(this assumes you're using ash_postgres as your datalayer)
ZachDaniel
ZachDaniel2y ago
like and ilike are provided by the ash_postgres data layer
ZachDaniel
ZachDaniel2y ago
You can also use the builtin contains function
Ash.Query.filter(expr(contains(foo, ^search)))
Ash.Query.filter(expr(contains(foo, ^search)))
barnabasj
barnabasj2y ago
ash_hq also has search built upon postgres full text search depending on your needs. This transformer adds the necessary search stuff to the resource, I used as a reference to implement search in my own application

Did you find this page helpful?