moxley

moxley

I’m building a module that builds up an Ash.Query from parameters. I want it to be able to “OR” two queries together, like this:

left_query = build_query(Member2, left_params)
right_query = build_query(Member2, right_params)

query = Ash.Query.filter(Member2, ^left_query or ^right_query)

But this doesn’t work. The resulting query is:

#Ash.Query<resource: Member2, filter: #Ash.Filter<true>>

This will always return all records.

How do I correctly compose a query from other queries?


Additionally, I don’t know what an Ash.Filter is. The full description in the documentation is “The representation of a filter in Ash.” I don’t know how Ash.Filter relates to Ash.Query. They seem to be related, but the documentation doesn’t appear to mention how they relate. There is no section in the Ash HQ Tutorials for Ash.Filter or Ash.Query.

The intro documentation for Ash.Filter includes this example:

Ash.Query.filter(resource, name == "Zardoz")
Ash.Query.filter(resource, first_name == "Zar" and last_name == "Doz")
Ash.Query.filter(resource, first_name == "Zar" and last_name in ["Doz", "Daz"] and high_score > 10)
Ash.Query.filter(resource, first_name == "Zar" or last_name == "Doz" or (high_score > 10 and high_score < -10))

There is another example of Ash.Query.filter() further down, but no explanation of the relation between Ash.Query and Ash.Filter.

Ash.Query.filter() returns an %Ash.Query{} struct, not an Ash.Filter. The documentation for Ash.Query.filter() contains the sentence “The filter is applied as an “and” to any filters currently on the query. For more information on writing filters, see: Ash.Filter.”. So the documentation says there’s is a link between the two modules, but doesn’t explain how, or what that link is.

Showing Posts 1 to 2

zachdaniel

zachdaniel

Creator of Ash

Hey there! Sorry it took a while to get back to you. In Ash, we compose “expressions” to do this kind of building, as opposed to composing full queries. For example:

import Ash.Expr

defp build_filter(map) do
  Enum.reduce(map, true, fn {key, value}, acc -> 
    expr(^acc and ^ref(:field) == ^value)
  end)
end

Then you could or these together with

left_filter = build_filter(left_params)
right_filter = build_filter(right_params)
filter = expr(^left_query or ^right_query)

Ash.Query.filter(query, ^filter)

With that said, Ash also has a thing for this. It is missing some docs on the exact format accepted, but that is just elbow grease to be applied:

{:ok, filter} = Ash.Filter.parse(Resource, %{or: [%{a: 1}, %{b: 2}]})
# or even more succinctly
Ash.Query.filter_input(query, %{or: [%{a: 1}, %{b: 2}]})

So if you can use our format for filters, then you should do that, but if not then you’d build your own with expressions.

zachdaniel

zachdaniel

Creator of Ash

As for the documentation for that input format, I intend to document it very soon. It needs to have a json schema because it is the same format as the one use in AshJsonApi’s filter query parameter.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
rlipscombe
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
rlipscombe
When I use Mix.install with a git dependency, using git@github.com:... how do I change the SSH identity used? I’ve got two Github accoun...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews