 <p>You can't use <code>u.name</code> like that. <code>u</code> is just a name that exists in Elixir when compiling the query to SQL. It gets renamed while it is compiled. You need to add another <code>?</code> to <code>fragment</code> and pass <code>u.name</code> for that:</p> <pre><code>def search(query, search_term) do from u in query, where: fragment("to_tsvector(?) @@ plainto_tsquery(?)", u.name, ^search_term), order_by: fragment("ts_rank(to_tsvector(?), plainto_tsquery(?)) DESC", u.name, ^search_term) end </code></pre>