Skip to content

Commit

Permalink
configured_url/1 for retrieving additional endpoints / handlers from …
Browse files Browse the repository at this point in the history
…config
  • Loading branch information
boonious committed Sep 1, 2018
1 parent 9e93990 commit c67e06c
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 40 deletions.
34 changes: 19 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ which typically distributed across many servers.
Hui.search(q: "loch", rows: 5, fq: ["type:illustration", "format:image/jpeg"])
```

The above examples query a pre-configured default Solr endpoint - see `Configuration` below.
The query may involves search words (string) or a [keywords list](https://elixir-lang.org/getting-started/keywords-and-maps.html#keyword-lists)
The above queries the default Solr endpoint - see `Configuration` below.
A query may involves search words (string) or a [keywords list](https://elixir-lang.org/getting-started/keywords-and-maps.html#keyword-lists)
of Solr parameters, invoking the comprehensive and powerful features of Solr.

For any endpoint and request handlers defined in binary or struct format:
Queries may also be issued to other endpoints and request handlers, defined in binary or struct format:

```
Hui.search("http://localhost:8983/solr/collection", q: "loch")
Expand Down Expand Up @@ -89,25 +89,29 @@ be found at [https://hexdocs.pm/hui](https://hexdocs.pm/hui).

## Configuration

If your application only provides services to a single Solr core or collection.
A default URL may be specified in the application configuration as below:
A default Solr endpoint may be specified in the application configuration as below:

```
config hui, default_url,
url: "http://localhost:8983/solr/gettingstarted",
handler: "select" # optional
```
```
config :hui, :default_url,
url: "http://localhost:8983/solr/gettingstarted",
handler: "select" # optional
```

- `url`: Typical Solr endpoint including the core or collection name. This could also be a load balancer
endpoint fronting several upstream servers
- `handler`: name of a handler that processes requests (per endpoint).
See `Hui.URL.default_url!/0`.

Solr provides [a variety of request
Solr provides [various request
handlers](http://lucene.apache.org/solr/guide/7_4/overview-of-searching-in-solr.html#overview-of-searching-in-solr)
for many purposes (search, autosuggest, spellcheck, indexing etc.). The handlers are configured
in different custom or normative names in
[Solr configuration](http://lucene.apache.org/solr/guide/7_4/requesthandlers-and-searchcomponents-in-solrconfig.html#requesthandlers-and-searchcomponents-in-solrconfig),
e.g. "select" for search queries.

Hui sends queries to the default URL if it exists (when none is supplied programmtically).
Multiple endpoints with different Solr request handlers can be configured in Hui with an arbitrary config key (e.g. `:suggester`):

```
config :hui, :suggester,
url: "http://localhost:8983/solr/collection",
handler: "suggest"
```

Use the config key in `Hui.search/2` to send queries to the endpoint or retrieve from configuration e.g. `Hui.URL.config_url(:suggester)`.
9 changes: 8 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,12 @@ use Mix.Config
# A default Solr endpoint may be configured via the 'default_url' property
#
config :hui, :default_url, # default endpoint
url: "http://localhost:8983/solr/gettingstarted",
url: "http://localhost:8983/solr/gettingstarted", # core or collection endpoint
handler: "select" # optional

# Additional Solr endpoints may be configured using any config key, e.g. :suggester.
# Use Hui.URL.config_url(:suggester) function to retrieve the corresponding URL struct
#
config :hui, :suggester,
url: "http://localhost:8983/solr/collection",
handler: "suggest"
53 changes: 38 additions & 15 deletions lib/hui/url.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,58 @@ defmodule Hui.URL do
@type t :: %__MODULE__{url: nil | binary, handler: nil | binary}

@doc """
Returns any configured Solr url as `t:Hui.URL.t/0` struct.
Returns a configured default Solr endpoint as `t:Hui.URL.t/0` struct.
```
iex> Hui.URL.default_url!
%Hui.URL{handler: "select", url: "http://localhost:8983/solr/gettingstarted"}
```
A default URL may be specified in the application configuration as below:
The default endpoint can be specified in application configuration as below:
```
config hui, default_url,
config :hui, :default_url,
url: "http://localhost:8983/solr/gettingstarted",
handler: "select" # optional
```
- `url`: Typical Solr endpoint including the core or collection name. This could also be a load balancer
endpoint fronting several upstream servers
- `handler`: name of a handler that processes requests (per endpoint).
- `url`: typical endpoint including the core or collection name. This may also be a load balancer
endpoint fronting several Solr upstreams.
- `handler`: name of a request handler that processes requests.
"""
@spec default_url! :: t | nil
def default_url! do
{x, y} = {Application.get_env(:hui, :default_url)[:url], Application.get_env(:hui, :default_url)[:handler]}
{status, default_url} = config_url(:default_url)
case status do
:ok -> default_url
:error -> nil
end
end

@doc """
Retrieve url configuration as `t:Hui.URL.t/0` struct.
## Example
iex> Hui.URL.config_url(:suggester)
{:ok, %Hui.URL{handler: "suggest", url: "http://localhost:8983/solr/collection"}}
The above retrieves the following endpoint configuration e.g. from `config.exs`:
```
config :hui, :suggester,
url: "http://localhost:8983/solr/collection",
handler: "suggest"
```
"""
@spec config_url(atom) :: {:ok, t} | {:error, binary} | nil
def config_url(config_key) do
{x, y} = {Application.get_env(:hui, config_key)[:url], Application.get_env(:hui, config_key)[:handler]}
case {x,y} do
{nil, nil} -> nil
{nil, _} -> nil
{_, nil} -> %Hui.URL{url: x}
{_, _} -> %Hui.URL{url: x, handler: y}
{nil, nil} -> {:error, "URL not found in configuration"}
{nil, _} -> {:error, "URL not found in configuration"}
{_, nil} -> {:ok, %Hui.URL{url: x}}
{_, _} -> {:ok, %Hui.URL{url: x, handler: y}}
end
end

Expand Down Expand Up @@ -70,7 +93,7 @@ defmodule Hui.URL do
def encode_query(enumerable) when is_list(enumerable), do: Enum.map_join(enumerable, "&", &encode/1)
def encode_query(_), do: ""

@doc "Returns the string representation of the given `t:Hui.URL.t/0` struct."
@doc "Returns the string representation (URL path) of the given `t:Hui.URL.t/0` struct."
@spec to_string(t) :: binary
def to_string(%__MODULE__{url: url, handler: handler}), do: "#{url}/#{handler}"

Expand Down
28 changes: 19 additions & 9 deletions test/hui_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,26 @@ defmodule HuiTest do
use ExUnit.Case, async: true
doctest Hui.URL

# Using the config :hui, :default_url example
test "default_url! should return a %Hui.URL stuct" do
assert %Hui.URL{url: "http://localhost:8983/solr/gettingstarted", handler: "select"} = Hui.URL.default_url!
end
describe "Hui.URL" do

# Using the config :hui, :default_url example in config.exs
test "default_url! should return %Hui.URL stuct" do
assert %Hui.URL{url: "http://localhost:8983/solr/gettingstarted", handler: "select"} = Hui.URL.default_url!
end

# Using the config :hui, :suggester example in config.exs
test "url function should return %Hui.URL stuct for a given config" do
assert {:ok, %Hui.URL{url: "http://localhost:8983/solr/collection", handler: "suggest"}} = Hui.URL.config_url(:suggester)
assert {:error, "URL not found in configuration"} = Hui.URL.config_url(:random_url_not_in_config)
end

test "to_string should return a URL" do
x = %Hui.URL{url: "http://localhost:8983/solr/newspapers", handler: "suggest"}
y = %Hui.URL{url: "http://localhost:8983/solr/newspapers"}
assert "http://localhost:8983/solr/newspapers/suggest" = x |> Hui.URL.to_string
assert "http://localhost:8983/solr/newspapers/select" = y |> Hui.URL.to_string
end

test "Hui.URL.to_string should return a URL" do
x = %Hui.URL{url: "http://localhost:8983/solr/newspapers", handler: "suggest"}
y = %Hui.URL{url: "http://localhost:8983/solr/newspapers"}
assert "http://localhost:8983/solr/newspapers/suggest" = x |> Hui.URL.to_string
assert "http://localhost:8983/solr/newspapers/select" = y |> Hui.URL.to_string
end

end

0 comments on commit c67e06c

Please sign in to comment.