Our application provides a macro to help with full text search. The macro, as it exists currently looks like this:
defmodule MyApp.Search
defmacro tsquery(column, terms, language \\ "english") do
quote do
fragment("to_tsvector(?, ?) @@ to_tsquery(?, ?)",
unquote(language),
unquote(column),
unquote(language),
unquote(terms))
end
end
end
The Search
module also implements a function for sanitizing and joining the search terms. We’re adding the ability to control whether words are “anded” or “ored” together, and we’d like to do that within the tsquery
macro directly. Functionally, what we’re looking to do looks like this:
defmodule MyApp.Search
defmacro tsquery(column, terms, operator, language \\ "english") do
quote do
fragment("to_tsvector(?, ?) @@ to_tsquery(?, ?)",
unquote(language),
unquote(column),
unquote(language),
unquote(ts_query_text(terms, operator)))
end
end
end
This fails because ts_query_text
is called at compile time. None of my various attempts at restructuring the macro to call the function within the quote
block and the fragment
macro have worked.
Is it possible to call a function from inside the quoted fragment macro?