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 a term in a given field of a queryable.
  • ilike: Searches for a term in a given field of a queryable, in a case insensitive fashion.
  • eq: Searches for a term to be equal to a given field of a queryable.
  • gt: Searches for a term to be greater than to a given field of a queryable.
  • lt: Searches for a term to be less than to a given field of a queryable.
  • gteq: Searches for a term to be greater than or equal to to a given field of a queryable.
  • lteq: Searches for a term to be less than or equal to a given field of a queryable.
  • is_nil: Searches for a null value when term is true, or not null when term is false.
  • in: Searches for a given field in a collection of terms in a queryable.

@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 an OR 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

Types

TODO: Finish

TODO: Finish

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

Link to this type search_expr() View Source
search_expr() :: :where | :or_where | :not_where

TODO: Finish

Link to this type search_type() View Source
search_type() ::
  :like |
  :ilike |
  :eq |
  :gt |
  :lt |
  :gteq |
  :lteq |
  :is_nil

TODO: Finish

Link to this section Functions

Link to this function handle_eq(queryable, field, search_term, atom) View Source

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_!">
Link to this function handle_gt(queryable, field, search_term, atom) View Source

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_!">
Link to this function handle_gteq(queryable, field, search_term, atom) View Source

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_!">
Link to this function handle_ilike(queryable, field, search_term, atom) View Source

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_!%"))>
Link to this function handle_in(queryable, field, list, atom) View Source

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"]>
Link to this function handle_is_nil(queryable, field, bool, atom) View Source

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)>
Link to this function handle_like(queryable, field, search_term, atom) View Source

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_!%"))>
Link to this function handle_lt(queryable, field, search_term, atom) View Source

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_!">
Link to this function handle_lteq(queryable, field, search_term, atom) View Source

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_!">

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]