-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbombadil.ex
65 lines (50 loc) · 1.88 KB
/
bombadil.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
defmodule Bombadil do
@moduledoc """
Bombadil is a wrapper around some PostgreSQL search capabilities.
It supports:
* exact match through PostgreSQL tsvector(s)
* fuzzy search inside jsonb field
* indexing in the jsonb field
"""
@doc """
Search data with exact match of a string (or substring)
Assuming that you have indexed `%{"book" => "Lord of the Rings}`
## Example:
alias Bombadil.Ecto.Schema.SearchIndex
iex> YourEctoRepo.all(Bombadil.search(SearchIndex, "Lord of the Rings"))
[
%Bombadil.Ecto.Schema.SearchIndex{
__meta__: #Ecto.Schema.Metadata<:loaded, "search_index">,
payload: %{"book" => "Lord of the Rings"},
id: 1
}
]
"""
@spec search(Ecto.Schema.t(), String.t()) :: Ecto.Query.t()
defdelegate search(schema, query, opts \\ []), to: Bombadil.Search
@doc """
Fuzzy search data of a string (or substring)
Assuming that you have indexed `%{"book" => "Lord of the Rings}`
## Example:
alias Bombadil.Ecto.Schema.SearchIndex
iex> YourEctoRepo.all(Bombadil.fuzzy_search(SearchIndex, "lord of the ringz"))
[
%Bombadil.Ecto.Schema.SearchIndex{
__meta__: #Ecto.Schema.Metadata<:loaded, "search_index">,
payload: %{"book" => "Lord of the Rings"},
id: 1
}
]
"""
@spec fuzzy_search(Ecto.Schema.t(), String.t() | list(), Keyword.t()) :: Ecto.Query.t()
defdelegate fuzzy_search(schema, query, opts \\ []), to: Bombadil.Search
@doc """
Index a document payload as map
## Example:
alias Bombadil.Ecto.Schema.SearchIndex
YourEctoRepo.insert_or_update(Bombadil.index(SearchIndex, payload: %{"book" => "Lord of the Rings"}))
"""
@spec index(Ecto.Schema.t(), Keyword.t() | Ecto.Changeset.t(), Keyword.t()) ::
Ecto.Changeset.t()
defdelegate index(schema, payload, params \\ []), to: Bombadil.Index
end