Rumamge.Ecto v1.2.0 Rummage.Ecto.CustomHooks.SimpleSearch

Rummage.Ecto.CustomHooks.SimpleSearch is a custom search hook that comes shipped with Rummage.Ecto.

Usage: For a regular search:

This returns a queryable which upon running will give a list of Parent(s) searched by ascending field_1

alias Rummage.Ecto.CustomHooks.SimpleSearch

searched_queryable = SimpleSearch.run(Parent, %{"search" => %{"field_1" => "field_!"}})

For a case-insensitive search:

This returns a queryable which upon running will give a list of Parent(s) searched by ascending case insensitive field_1.

Keep in mind that case_insensitive can only be called for text fields

alias Rummage.Ecto.CustomHooks.SimpleSearch

searched_queryable = SimpleSearch.run(Parent, %{"search" => %{"field_1.ci" => "field_!"}})

This module can be used by overriding the default search module. This can be done in the following ways:

In the Rummage.Ecto call:

Rummage.Ecto.rummage(queryable, rummage, search: Rummage.Ecto.CustomHooks.SimpleSearch)

OR

Globally for all models in config.exs:

config :rummage_ecto,
  Rummage.Ecto,
  default_search: Rummage.Ecto.CustomHooks.SimpleSearch

Summary

Functions

Implementation of before_hook for Rummage.Ecto.CustomHooks.SimpleSearch. This just returns back rummage at this point. It doesn’t matter what queryable or opts are, it just returns back rummage

Builds a search queryable on top of the given queryable from the rummage parameters from the given rummage struct

Functions

before_hook(queryable, rummage, opts)
before_hook(Ecto.Query.t, map, map) :: map

Implementation of before_hook for Rummage.Ecto.CustomHooks.SimpleSearch. This just returns back rummage at this point. It doesn’t matter what queryable or opts are, it just returns back rummage.

Examples

iex> alias Rummage.Ecto.CustomHooks.SimpleSearch
iex> SimpleSearch.before_hook(Parent, %{}, %{})
%{}
run(queryable, rummage)
run(Ecto.Query.t, map) :: {Ecto.Query.t, map}

Builds a search queryable on top of the given queryable from the rummage parameters from the given rummage struct.

Examples

When rummage struct passed doesn’t have the key “search”, it simply returns the queryable itself:

iex> alias Rummage.Ecto.CustomHooks.SimpleSearch
iex> import Ecto.Query
iex> SimpleSearch.run(Parent, %{})
Parent

When the queryable passed is not just a struct:

iex> alias Rummage.Ecto.CustomHooks.SimpleSearch
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex>  SimpleSearch.run(queryable, %{})
#Ecto.Query<from p in "parents">

When rummage struct passed has the key "search", but with a value of %{}, "" or [] it simply returns the queryable itself:

iex> alias Rummage.Ecto.CustomHooks.SimpleSearch
iex> import Ecto.Query
iex> SimpleSearch.run(Parent, %{"search" => %{}})
Parent

iex> alias Rummage.Ecto.CustomHooks.SimpleSearch
iex> import Ecto.Query
iex> SimpleSearch.run(Parent, %{"search" => ""})
Parent

iex> alias Rummage.Ecto.CustomHooks.SimpleSearch
iex> import Ecto.Query
iex> SimpleSearch.run(Parent, %{"search" => []})
Parent

When rummage struct passed has the key “search”, with “field” and “term” it returns a searched version of the queryable passed in as the argument:

iex> alias Rummage.Ecto.CustomHooks.SimpleSearch
iex> import Ecto.Query
iex> rummage = %{"search" => %{"field_1" => "field_!"}}
%{"search" => %{"field_1" => "field_!"}}
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> SimpleSearch.run(queryable, rummage)
#Ecto.Query<from p in "parents", where: like(p.field_1, ^"%field_!%")>

When rummage struct passed has case-insensitive search, it returns a searched version of the queryable with case_insensitive arguments:

iex> alias Rummage.Ecto.CustomHooks.SimpleSearch
iex> import Ecto.Query
iex> rummage = %{"search" => %{"field_1.ci" => "field_!"}}
%{"search" => %{"field_1.ci" => "field_!"}}
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> SimpleSearch.run(queryable, rummage)
#Ecto.Query<from p in "parents", where: ilike(p.field_1, ^"%field_!%")>