msonawane

msonawane

How to do a similarity search with Ash?

Hello,

I am trying to get search results with postgres trgm like below

SELECT t, similarity(t, 'word') AS sml
  FROM test_trgm
  WHERE t % 'word'
  ORDER BY sml DESC, t;

with following read action and calculation.

    read :search do
      argument :term, :string, allow_nil?: false

      prepare build(
                select: [:name, :id, :country_code, :state_code],
                sort: [similarity: {%{term: arg(:term)}, :desc}],
                limit: 10
              )

      filter expr(fragment("name % (?)", ^arg(:term)))
    end


    calculate :similarity, :float do
      argument :term, :string, allow_nil?: false
      calculation expr(fragment("similarity(name,?)", ^arg(:term)))
    end

i get sql but similarity function is called twice, once in select clause and again in order by clause.

iex(4)> City |>  Ash.Query.for_read(:search, %{term: "york"}) |> Ash.read
[debug] QUERY OK source="cities" db=29.8ms queue=4.4ms idle=590.6ms
SELECT c0."name", c0."id", c0."country_code", c0."state_code", ((similarity(name,$1)))::float::float FROM "cities" AS c0 WHERE (((name % ($2)))) ORDER BY ((similarity(name,$3)))::float::float DESC LIMIT $4 ["york", "york", "york", 10]
↳ anonymous fn/3 in AshPostgres.DataLayer.run_query/2, at: lib/data_layer.ex:767

Questions

  1. can this be improved ?
  2. any recipies for calling raw sql queries in ash way?

Thanks

Most Liked

msonawane

msonawane

sorry for delay. few reasons

  • we have few custom sql functions which require escape hatches for any orm
  • to learn idiomatic ways to do tasks without fighting framework. so far ash seems really well thought out and i havent had to fight it much after initial learning.
    Thank you for your work.

Where Next?

Popular in Questions Top

New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 43487 311
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41989 114
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39467 209
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement