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
- can this be improved ?
- any recipies for calling raw sql queries in ash way?
Thanks