Rumamge.Ecto v2.0.0-rc.0 Rummage.Ecto.Services.BuildSearchQuery View Source
Rummage.Ecto.Services.BuildSearchQuery
is a service module which serves the
default search hook, Rummage.Ecto.Hook.Search
that comes shipped with Rummage.Ecto
.
Module Attributes
@search_types ~w{like ilike eq gt lt gteq lteq is_nil}a
@search_exprs ~w{where or_where not_where}a
@search_types
is a collection of all the 8 valid search_types
that come shipped with
Rummage.Ecto
’s default search hook. The types are:
like
: Searches for aterm
in a givenfield
of aqueryable
.ilike
: Searches for aterm
in a givenfield
of aqueryable
, in a case insensitive fashion.eq
: Searches for aterm
to be equal to a givenfield
of aqueryable
.gt
: Searches for aterm
to be greater than to a givenfield
of aqueryable
.lt
: Searches for aterm
to be less than to a givenfield
of aqueryable
.gteq
: Searches for aterm
to be greater than or equal to to a givenfield
of aqueryable
.lteq
: Searches for aterm
to be less than or equal to a givenfield
of aqueryable
.is_nil
: Searches for a null value whenterm
is true, or not null whenterm
is false.in
: Searches for a givenfield
in a collection ofterms
in aqueryable
.
@search_exprs
is a collection of 3 valid search_exprs
that are used to
apply a search_type
to a Ecto.Queryable
. Those expressions are:
where
(DEFAULT): An AND where query expression.or_where
: An OR where query expression. Behaves exactly like where but combines the previous expression with anOR
operation. Useful for optional searches.not_where
: A NOT where query expression. This can be used while excluding a list of entries based on where query.
Feel free to use this module on a custom search hook that you write.
Link to this section Summary
Functions
Builds a searched queryable
on top of the given queryable
using
field
, search_term
and search_expr
when the search_type
is eq
Builds a searched queryable
on top of the given queryable
using
field
, search_term
and search_expr
when the search_type
is gt
Builds a searched queryable
on top of the given queryable
using
field
, search_term
and search_expr
when the search_type
is gteq
Builds a searched queryable
on top of the given queryable
using
field
, search_term
and search_expr
when the search_type
is ilike
Builds a searched queryable
on field
based on whether it exists in
a given collection, based on search_expr
given
Builds a searched queryable
on field
is_nil (when term
is true),
or not is_nil (when term
is false), based on search_expr
given
Builds a searched queryable
on top of the given queryable
using
field
, search_term
and search_expr
when the search_type
is like
Builds a searched queryable
on top of the given queryable
using
field
, search_term
and search_expr
when the search_type
is lt
Builds a searched queryable
on top of the given queryable
using
field
, search_term
and search_expr
when the search_type
is lteq
Builds a searched queryable
on top of the given queryable
using field
, search_type
and search_term
Link to this section Types
TODO: Finish
search_type() :: :like | :ilike | :eq | :gt | :lt | :gteq | :lteq | :is_nil
TODO: Finish
Link to this section Functions
handle_eq(Ecto.Query.t(), atom(), term(), Rummage.Ecto.Services.BuildSearchQuery.search_expr()) :: Ecto.Query.t()
Builds a searched queryable
on top of the given queryable
using
field
, search_term
and search_expr
when the search_type
is eq
.
Assumes that search_expr
is in [:where, :or_where, :not_where].
Examples
When search_expr
is :where
iex> alias Rummage.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_eq(queryable, :field_1, "field_!", :where)
#Ecto.Query<from p in "parents", where: p.field_1 == ^"field_!">
When search_expr
is :or_where
iex> alias Rummage.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_eq(queryable, :field_1, "field_!", :or_where)
#Ecto.Query<from p in "parents", or_where: p.field_1 == ^"field_!">
When search_expr
is :not_where
iex> alias Rummage.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_eq(queryable, :field_1, "field_!", :not_where)
#Ecto.Query<from p in "parents", where: p.field_1 != ^"field_!">
handle_gt(Ecto.Query.t(), atom(), term(), Rummage.Ecto.Services.BuildSearchQuery.search_expr()) :: Ecto.Query.t()
Builds a searched queryable
on top of the given queryable
using
field
, search_term
and search_expr
when the search_type
is gt
.
Assumes that search_expr
is in [:where, :or_where, :not_where].
Examples
When search_expr
is :where
iex> alias Rummage.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_gt(queryable, :field_1, "field_!", :where)
#Ecto.Query<from p in "parents", where: p.field_1 > ^"field_!">
When search_expr
is :or_where
iex> alias Rummage.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_gt(queryable, :field_1, "field_!", :or_where)
#Ecto.Query<from p in "parents", or_where: p.field_1 > ^"field_!">
When search_expr
is :not_where
iex> alias Rummage.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_gt(queryable, :field_1, "field_!", :not_where)
#Ecto.Query<from p in "parents", where: p.field_1 <= ^"field_!">
handle_gteq(Ecto.Query.t(), atom(), term(), Rummage.Ecto.Services.BuildSearchQuery.search_expr()) :: Ecto.Query.t()
Builds a searched queryable
on top of the given queryable
using
field
, search_term
and search_expr
when the search_type
is gteq
.
Assumes that search_expr
is in [:where, :or_where, :not_where].
Examples
When search_expr
is :where
iex> alias Rummage.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_gteq(queryable, :field_1, "field_!", :where)
#Ecto.Query<from p in "parents", where: p.field_1 >= ^"field_!">
When search_expr
is :or_where
iex> alias Rummage.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_gteq(queryable, :field_1, "field_!", :or_where)
#Ecto.Query<from p in "parents", or_where: p.field_1 >= ^"field_!">
When search_expr
is :not_where
iex> alias Rummage.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_gteq(queryable, :field_1, "field_!", :not_where)
#Ecto.Query<from p in "parents", where: p.field_1 < ^"field_!">
handle_ilike(Ecto.Query.t(), atom(), String.t(), Rummage.Ecto.Services.BuildSearchQuery.search_expr()) :: Ecto.Query.t()
Builds a searched queryable
on top of the given queryable
using
field
, search_term
and search_expr
when the search_type
is ilike
.
Checkout Ecto.Query.API.ilike/2 for more info.
Assumes that search_expr
is in [:where, :or_where, :not_where].
Examples
When search_expr
is :where
iex> alias Rummage.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_ilike(queryable, :field_1, "field_!", :where)
#Ecto.Query<from p in "parents", where: ilike(p.field_1, ^"%field_!%")>
When search_expr
is :or_where
iex> alias Rummage.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_ilike(queryable, :field_1, "field_!", :or_where)
#Ecto.Query<from p in "parents", or_where: ilike(p.field_1, ^"%field_!%")>
When search_expr
is :not_where
iex> alias Rummage.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_ilike(queryable, :field_1, "field_!", :not_where)
#Ecto.Query<from p in "parents", where: not(ilike(p.field_1, ^"%field_!%"))>
Builds a searched queryable
on field
based on whether it exists in
a given collection, based on search_expr
given.
Checkout Ecto.Query.API.in/2 for more info.
Assumes that search_expr
is in [:where, :or_where, :not_where].
Examples
When search_expr
is :where
iex> alias Rummage.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_in(queryable, :field_1, ["a", "b"], :where)
#Ecto.Query<from p in "parents", where: p.field_1 in ^["a", "b"]>
When search_expr
is :or_where
iex> alias Rummage.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_in(queryable, :field_1, ["a", "b"], :or_where)
#Ecto.Query<from p in "parents", or_where: p.field_1 in ^["a", "b"]>
When search_expr
is :not_where
iex> alias Rummage.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_in(queryable, :field_1, ["a", "b"], :not_where)
#Ecto.Query<from p in "parents", where: p.field_1 not in ^["a", "b"]>
handle_is_nil(Ecto.Query.t(), atom(), boolean(), Rummage.Ecto.Services.BuildSearchQuery.search_expr()) :: Ecto.Query.t()
handle_is_nil(Ecto.Query.t(), atom(), boolean(), Rummage.Ecto.Services.BuildSearchQuery.search_expr()) :: Ecto.Query.t()
Builds a searched queryable
on field
is_nil (when term
is true),
or not is_nil (when term
is false), based on search_expr
given.
Checkout Ecto.Query.API.like/2 for more info.
Assumes that search_expr
is in [:where, :or_where, :not_where].
Examples
When search_expr
is :where
iex> alias Rummage.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_is_nil(queryable, :field_1, true, :where)
#Ecto.Query<from p in "parents", where: is_nil(p.field_1)>
iex> BuildSearchQuery.handle_is_nil(queryable, :field_1, false, :where)
#Ecto.Query<from p in "parents", where: not(is_nil(p.field_1))>
When search_expr
is :or_where
iex> alias Rummage.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_is_nil(queryable, :field_1, true, :or_where)
#Ecto.Query<from p in "parents", or_where: is_nil(p.field_1)>
iex> BuildSearchQuery.handle_is_nil(queryable, :field_1, false, :or_where)
#Ecto.Query<from p in "parents", or_where: not(is_nil(p.field_1))>
When search_expr
is :not_where
iex> alias Rummage.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_is_nil(queryable, :field_1, true, :not_where)
#Ecto.Query<from p in "parents", where: not(is_nil(p.field_1))>
iex> BuildSearchQuery.handle_is_nil(queryable, :field_1, false, :not_where)
#Ecto.Query<from p in "parents", where: is_nil(p.field_1)>
handle_like(Ecto.Query.t(), atom() | tuple(), String.t(), Rummage.Ecto.Services.BuildSearchQuery.search_expr()) :: Ecto.Query.t()
Builds a searched queryable
on top of the given queryable
using
field
, search_term
and search_expr
when the search_type
is like
.
Checkout Ecto.Query.API.like/2 for more info.
NOTE: Be careful of Like Injections
Assumes that search_expr
is in [:where, :or_where, :not_where].
Examples
When search_expr
is :where
iex> alias Rummage.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_like(queryable, :field_1, "field_!", :where)
#Ecto.Query<from p in "parents", where: like(p.field_1, ^"%field_!%")>
When search_expr
is :or_where
iex> alias Rummage.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_like(queryable, :field_1, "field_!", :or_where)
#Ecto.Query<from p in "parents", or_where: like(p.field_1, ^"%field_!%")>
When search_expr
is :not_where
iex> alias Rummage.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_like(queryable, :field_1, "field_!", :not_where)
#Ecto.Query<from p in "parents", where: not(like(p.field_1, ^"%field_!%"))>
handle_lt(Ecto.Query.t(), atom(), term(), Rummage.Ecto.Services.BuildSearchQuery.search_expr()) :: Ecto.Query.t()
Builds a searched queryable
on top of the given queryable
using
field
, search_term
and search_expr
when the search_type
is lt
.
Assumes that search_expr
is in [:where, :or_where, :not_where].
Examples
When search_expr
is :where
iex> alias Rummage.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_lt(queryable, :field_1, "field_!", :where)
#Ecto.Query<from p in "parents", where: p.field_1 < ^"field_!">
When search_expr
is :or_where
iex> alias Rummage.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_lt(queryable, :field_1, "field_!", :or_where)
#Ecto.Query<from p in "parents", or_where: p.field_1 < ^"field_!">
When search_expr
is :not_where
iex> alias Rummage.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_lt(queryable, :field_1, "field_!", :not_where)
#Ecto.Query<from p in "parents", where: p.field_1 >= ^"field_!">
handle_lteq(Ecto.Query.t(), atom(), term(), Rummage.Ecto.Services.BuildSearchQuery.search_expr()) :: Ecto.Query.t()
Builds a searched queryable
on top of the given queryable
using
field
, search_term
and search_expr
when the search_type
is lteq
.
Assumes that search_expr
is in [:where, :or_where, :not_where].
Examples
When search_expr
is :where
iex> alias Rummage.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_lteq(queryable, :field_1, "field_!", :where)
#Ecto.Query<from p in "parents", where: p.field_1 <= ^"field_!">
When search_expr
is :or_where
iex> alias Rummage.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_lteq(queryable, :field_1, "field_!", :or_where)
#Ecto.Query<from p in "parents", or_where: p.field_1 <= ^"field_!">
When search_expr
is :not_where
iex> alias Rummage.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_lteq(queryable, :field_1, "field_!", :not_where)
#Ecto.Query<from p in "parents", where: p.field_1 > ^"field_!">
run(Ecto.Query.t(), {Rummage.Ecto.Services.BuildSearchQuery.search_expr(), Rummage.Ecto.Services.BuildSearchQuery.search_type()}, String.t(), term()) :: {Ecto.Query.t()}
Builds a searched queryable
on top of the given queryable
using field
, search_type
and search_term
.
Examples
When search_type
is where
:
When field
, search_type
and queryable
are passed with search_type
of like
:
iex> alias Rummage.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.run(queryable, :field_1, {:where, :like}, "field_!")
#Ecto.Query<from p in "parents", where: like(p.field_1, ^"%field_!%")>
When field
, search_type
and queryable
are passed with search_type
of ilike
:
iex> alias Rummage.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.run(queryable, :field_1, {:where, :ilike}, "field_!")
#Ecto.Query<from p in "parents", where: ilike(p.field_1, ^"%field_!%")>
When field
, search_type
and queryable
are passed with search_type
of eq
:
iex> alias Rummage.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.run(queryable, :field_1, {:where, :eq}, "field_!")
#Ecto.Query<from p in "parents", where: p.field_1 == ^"field_!">
When field
, search_type
and queryable
are passed with search_type
of gt
:
iex> alias Rummage.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.run(queryable, :field_1, {:where, :gt}, "field_!")
#Ecto.Query<from p in "parents", where: p.field_1 > ^"field_!">
When field
, search_type
and queryable
are passed with search_type
of lt
:
iex> alias Rummage.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.run(queryable, :field_1, {:where, :lt}, "field_!")
#Ecto.Query<from p in "parents", where: p.field_1 < ^"field_!">
When field
, search_type
and queryable
are passed with search_type
of gteq
:
iex> alias Rummage.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.run(queryable, :field_1, {:where, :gteq}, "field_!")
#Ecto.Query<from p in "parents", where: p.field_1 >= ^"field_!">
When field
, search_type
and queryable
are passed with search_type
of lteq
:
iex> alias Rummage.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.run(queryable, :field_1, {:where, :lteq}, "field_!")
#Ecto.Query<from p in "parents", where: p.field_1 <= ^"field_!">
When search_type
is or_where
:
When field
, search_type
and queryable
are passed with search_type
of like
:
iex> alias Rummage.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.run(queryable, :field_1, {:or_where, :like}, "field_!")
#Ecto.Query<from p in "parents", or_where: like(p.field_1, ^"%field_!%")>
When field
, search_type
and queryable
are passed with search_type
of ilike
:
iex> alias Rummage.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.run(queryable, :field_1, {:or_where, :ilike}, "field_!")
#Ecto.Query<from p in "parents", or_where: ilike(p.field_1, ^"%field_!%")>
When field
, search_type
and queryable
are passed with search_type
of eq
:
iex> alias Rummage.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.run(queryable, :field_1, {:or_where, :eq}, "field_!")
#Ecto.Query<from p in "parents", or_where: p.field_1 == ^"field_!">
When field
, search_type
and queryable
are passed with search_type
of gt
:
iex> alias Rummage.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.run(queryable, :field_1, {:or_where, :gt}, "field_!")
#Ecto.Query<from p in "parents", or_where: p.field_1 > ^"field_!">
When field
, search_type
and queryable
are passed with search_type
of lt
:
iex> alias Rummage.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.run(queryable, :field_1, {:or_where, :lt}, "field_!")
#Ecto.Query<from p in "parents", or_where: p.field_1 < ^"field_!">
When field
, search_type
and queryable
are passed with search_type
of gteq
:
iex> alias Rummage.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.run(queryable, :field_1, {:or_where, :gteq}, "field_!")
#Ecto.Query<from p in "parents", or_where: p.field_1 >= ^"field_!">
When field
, search_type
and queryable
are passed with search_type
of lteq
:
iex> alias Rummage.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.run(queryable, :field_1, {:or_where, :lteq}, "field_!")
#Ecto.Query<from p in "parents", or_where: p.field_1 <= ^"field_!">
When field
, search_type
and queryable
are passed with an invalid search_type
and search_expr
:
iex> alias Rummage.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.run(queryable, :field_1, {:pizza, :cheese}, "field_!")
** (RuntimeError) Unknown {search_expr, search_type}, {:pizza, :cheese}
search_type should be one of [:like, :ilike, :eq, :gt, :lt, :gteq, :lteq, :is_nil, :in]
search_expr should be one of [:where, :or_where, :not_where]