forked from breakroom/snap
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request breakroom#18 from jshmrtn/search_suggest_response
Support suggest in SearchReponse
- Loading branch information
Showing
6 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
defmodule Snap.Suggest do | ||
@moduledoc """ | ||
Represents an individual suggest dictionary from a [Search API](https://www.elastic.co/guide/en/elasticsearch/reference/current/search.html) response. | ||
""" | ||
defstruct ~w[ | ||
length | ||
offset | ||
options | ||
text | ||
]a | ||
|
||
def new(response) do | ||
%__MODULE__{ | ||
length: response["length"], | ||
offset: response["offset"], | ||
options: Snap.Suggest.Options.new(response["options"]), | ||
text: response["text"] | ||
} | ||
end | ||
|
||
@type t :: %__MODULE__{ | ||
length: non_neg_integer(), | ||
offset: non_neg_integer(), | ||
options: [Snap.Suggest.Option.t()], | ||
text: String.t() | ||
} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
defmodule Snap.Suggest.Option do | ||
@moduledoc """ | ||
Represents an individual `suggest` / `options` dictionary from a [Search API](https://www.elastic.co/guide/en/elasticsearch/reference/current/search.html) response. | ||
""" | ||
defstruct ~w[ | ||
freq | ||
score | ||
text | ||
]a | ||
|
||
def new(response) do | ||
%__MODULE__{ | ||
freq: response["freq"], | ||
score: response["score"], | ||
text: response["text"] | ||
} | ||
end | ||
|
||
@type t :: %__MODULE__{ | ||
freq: integer(), | ||
score: float(), | ||
text: String.t() | ||
} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
defmodule Snap.Suggest.Options do | ||
@moduledoc """ | ||
Represents the `suggest` / `options` dictionary returned from an ElasticSearch [Search API](https://www.elastic.co/guide/en/elasticsearch/reference/current/search.html) response. | ||
""" | ||
|
||
def new(response), do: Enum.map(response, &Snap.Suggest.Option.new/1) | ||
|
||
@type t :: [Snap.Suggest.Option.t()] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
defmodule Snap.Suggests do | ||
@moduledoc """ | ||
Represents the `suggest` dictionary returned from an ElasticSearch [Search API](https://www.elastic.co/guide/en/elasticsearch/reference/current/search.html) response. | ||
""" | ||
|
||
def new(response) | ||
def new(nil), do: nil | ||
|
||
def new(response), | ||
do: | ||
Map.new(response, fn {name, suggest} -> {name, Enum.map(suggest, &Snap.Suggest.new(&1))} end) | ||
|
||
@type t :: %{String.t() => [Snap.Suggest.t()]} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters