Skip to content

Commit

Permalink
add search with Repo result
Browse files Browse the repository at this point in the history
  • Loading branch information
jaeyson committed Jan 20, 2023
1 parent 847f4dc commit 9e6fb9c
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.2.0 (2023.01.20)

* Add search function which returns a list of structs or empty.

## 0.1.0 (2023.01.20)

* Initial release
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ by adding `ex_typesense` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:ex_typesense, "~> 0.1.0"}
{:ex_typesense, "~> 0.2.0"}
]
end
```
Expand All @@ -44,6 +44,12 @@ config :ex_typesense,
scheme: "http" # "https"
```

Then:

1. create collection (`lib/ex_typesense/collection.ex`)
2. index a document (`lib/ex_typesense/document.ex`)
3. try to search (`lib/ex_typesense/document.ex`)

Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at <https://hexdocs.pm/ex_typesense>.
60 changes: 60 additions & 0 deletions lib/ex_typesense/document.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule ExTypesense.Document do
"""

alias ExTypesense.HttpClient
import Ecto.Query, warn: false

@collections_path "collections"
@documents_path "documents"
Expand Down Expand Up @@ -73,6 +74,65 @@ defmodule ExTypesense.Document do
HttpClient.run(:get, path, nil, query)
end

@doc """
Search from a document. Returns a list of structs or empty.
## Examples
iex> Document.search(Something, "umbrella", "title,description")
{:ok,
%{
"facet_counts" => [],
"found" => 20,
"hits" => [...],
"out_of" => 111,
"page" => 1,
"request_params" => %{
"collection_name" => "something",
"per_page" => 10,
"q" => "umbrella"
},
"search_cutoff" => false,
"search_time_ms" => 5
}
}
"""
@doc since: "0.1.0"
@spec ecto_search(module(), module(), String.t(), String.t(), String.t()) :: [] | [struct()]
def ecto_search(module_name, repo, search_term, search_field, query_by) do
query = %{
q: search_term,
query_by: query_by
}

path =
Path.join([
@collections_path,
module_name.__schema__(:source),
@documents_path,
@search_path
])

{:ok, result} = HttpClient.run(:get, path, nil, query)

case Enum.empty?(result["hits"]) do
true ->
[]

false ->
values =
Enum.map(result["hits"], fn %{"document" => document} ->
get_in(document, ["slug"])
end)

search_field = String.to_existing_atom(search_field)

module_name
|> where([i], field(i, ^search_field) in ^values)
|> repo.all()
end
end

@doc """
Indexes multiple documents.
Expand Down
5 changes: 3 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule ExTypesense.MixProject do
use Mix.Project

@source_url "https://github.com/jaeyson/ex_typesense"
@version "0.1.0"
@version "0.2.0"

def project do
[
Expand Down Expand Up @@ -31,7 +31,8 @@ defmodule ExTypesense.MixProject do
[
{:ex_doc, "~> 0.29.1", only: [:dev, :test], runtime: false},
{:credo, "~> 1.6.7", only: [:dev, :test], runtime: false},
{:req, "~> 0.3.3"}
{:req, "~> 0.3.3"},
{:ecto, "~> 3.9"}
]
end

Expand Down
2 changes: 2 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"bunt": {:hex, :bunt, "0.2.1", "e2d4792f7bc0ced7583ab54922808919518d0e57ee162901a16a1b6664ef3b14", [:mix], [], "hexpm", "a330bfb4245239787b15005e66ae6845c9cd524a288f0d141c148b02603777a5"},
"castore": {:hex, :castore, "0.1.22", "4127549e411bedd012ca3a308dede574f43819fe9394254ca55ab4895abfa1a2", [:mix], [], "hexpm", "c17576df47eb5aa1ee40cc4134316a99f5cad3e215d5c77b8dd3cfef12a22cac"},
"credo": {:hex, :credo, "1.6.7", "323f5734350fd23a456f2688b9430e7d517afb313fbd38671b8a4449798a7854", [:mix], [{:bunt, "~> 0.2.1", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2.8", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "41e110bfb007f7eda7f897c10bf019ceab9a0b269ce79f015d54b0dcf4fc7dd3"},
"decimal": {:hex, :decimal, "2.0.0", "a78296e617b0f5dd4c6caf57c714431347912ffb1d0842e998e9792b5642d697", [:mix], [], "hexpm", "34666e9c55dea81013e77d9d87370fe6cb6291d1ef32f46a1600230b1d44f577"},
"earmark_parser": {:hex, :earmark_parser, "1.4.29", "149d50dcb3a93d9f3d6f3ecf18c918fb5a2d3c001b5d3305c926cddfbd33355b", [:mix], [], "hexpm", "4902af1b3eb139016aed210888748db8070b8125c2342ce3dcae4f38dcc63503"},
"ecto": {:hex, :ecto, "3.9.4", "3ee68e25dbe0c36f980f1ba5dd41ee0d3eb0873bccae8aeaf1a2647242bffa35", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "de5f988c142a3aa4ec18b85a4ec34a2390b65b24f02385c1144252ff6ff8ee75"},
"ex_doc": {:hex, :ex_doc, "0.29.1", "b1c652fa5f92ee9cf15c75271168027f92039b3877094290a75abcaac82a9f77", [:mix], [{:earmark_parser, "~> 1.4.19", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "b7745fa6374a36daf484e2a2012274950e084815b936b1319aeebcf7809574f6"},
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
"finch": {:hex, :finch, "0.14.0", "619bfdee18fc135190bf590356c4bf5d5f71f916adb12aec94caa3fa9267a4bc", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: false]}, {:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.3", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.4", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 0.2.6", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "5459acaf18c4fdb47a8c22fb3baff5d8173106217c8e56c5ba0b93e66501a8dd"},
Expand Down

0 comments on commit 9e6fb9c

Please sign in to comment.