ecto_searcher v0.1.0 EctoSearcher.Searcher.Mapping behaviour View Source

Behaviour for search query, condition and field mappings

Usage

Either adopt EctoSearcher.Searcher.Mapping behaviour and implement callbacks or use EctoSearcher.Searcher.Mapping, which provides defaults.

defmodule CustomMapping do
  use EctoSearcher.Searcher.Mapping
  require Ecto.Query
  alias Ecto.Query

  def conditions
    %{
      "not_eq" => fn(field, value) -> Query.dynamic([q], ^field != ^value) end
    }
  end
end

Link to this section Summary

Callbacks

Should return map with search conditions

Should return map with field queries

Link to this section Callbacks

Link to this callback conditions() View Source
conditions() :: Map.t()

Should return map with search conditions

Search condition map should look like:

%{
  "not_eq" => fn(field, value) -> Query.dynamic([q], ^field != ^value) end
  "in" => %{
    query: fn field, value -> Query.dynamic([q], ^field in ^value) end,
    aggregation: :array
  }
}

Condition name will be matched as search field suffix.

Values should either be a query function or a map with query function as :query and value aggregate type as :aggregation.

Query function will be called with arguments field (atom) and value (casted to specific type) and should return Ecto.Query.DynamicExpr.

Should return map with field queries

Field queries map should look like:

%{
  id_alias: Query.dynamic([q], q.id),
  datetime_field_as_date: %{
    query: Query.dynamic([q], fragment("?::date", q.datetime_field)),
    type: :date
  }
}

Field name will be matched as search field prefix (from searchable_fields).

Values should either be a Ecto.Query.DynamicExpr or a map with Ecto.Query.DynamicExpr as :query and value type type as :type.