Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: dcarneiro/exsolr
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: heyorbit/exsolr
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Able to merge. These branches can be automatically merged.
  • 13 commits
  • 14 files changed
  • 1 contributor

Commits on Mar 13, 2018

  1. Use elixir format

    Adrián Quintás committed Mar 13, 2018
    Copy the full SHA
    a0185bd View commit details
  2. Update deps

    Adrián Quintás committed Mar 13, 2018
    Copy the full SHA
    c644b86 View commit details
  3. Code format

    Adrián Quintás committed Mar 13, 2018
    Copy the full SHA
    f4d2ead View commit details

Commits on Mar 14, 2018

  1. Copy the full SHA
    20ad984 View commit details
  2. Explicitly use an error tuple in match

    Adrián Quintás committed Mar 14, 2018
    Copy the full SHA
    b6c51d1 View commit details
  3. Encode query params

    Adrián Quintás committed Mar 14, 2018
    Copy the full SHA
    2153fd9 View commit details

Commits on Mar 15, 2018

  1. Return nextCursorMark

    Adrián Quintás committed Mar 15, 2018
    Copy the full SHA
    d55e52c View commit details

Commits on Mar 16, 2018

  1. Add tmp behaviour

    Adrián Quintás committed Mar 16, 2018
    Copy the full SHA
    fae5dfe View commit details

Commits on Apr 19, 2018

  1. Return error message in get query

    Adrián Quintás committed Apr 19, 2018
    Copy the full SHA
    72e4b32 View commit details

Commits on May 4, 2018

  1. Encode query params in HTTP requests

    Adrián Quintás committed May 4, 2018
    Copy the full SHA
    f274408 View commit details

Commits on May 15, 2018

  1. Update deps

    Adrián Quintás committed May 15, 2018
    Copy the full SHA
    a5d1b0a View commit details

Commits on May 16, 2018

  1. Add suggest

    Adrián Quintás committed May 16, 2018
    Copy the full SHA
    f339330 View commit details

Commits on May 30, 2018

  1. Add more like this

    Adrián Quintás committed May 30, 2018
    Copy the full SHA
    88ebd29 View commit details
Showing with 615 additions and 79 deletions.
  1. +135 −0 .credo.exs
  2. +7 −0 .formatter.exs
  3. +1 −1 config/config.exs
  4. +52 −8 lib/exsolr.ex
  5. +22 −1 lib/exsolr/config.ex
  6. +9 −6 lib/exsolr/http_response.ex
  7. +16 −14 lib/exsolr/indexer.ex
  8. +136 −0 lib/exsolr/more_like_this.ex
  9. 0 lib/exsolr/query.ex
  10. +62 −21 lib/exsolr/searcher.ex
  11. +141 −0 lib/exsolr/suggest.ex
  12. +17 −15 mix.exs
  13. +15 −11 mix.lock
  14. +2 −2 test/exsolr_test.exs
135 changes: 135 additions & 0 deletions .credo.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# This file contains the configuration for Credo and you are probably reading
# this after creating it with `mix credo.gen.config`.
#
# If you find anything wrong or unclear in this file, please report an
# issue on GitHub: https://github.com/rrrene/credo/issues
#
%{
#
# You can have as many configs as you like in the `configs:` field.
configs: [
%{
#
# Run any config using `mix credo -C <name>`. If no config name is given
# "default" is used.
name: "default",
#
# These are the files included in the analysis:
files: %{
#
# You can give explicit globs or simply directories.
# In the latter case `**/*.{ex,exs}` will be used.
included: ["lib/", "src/", "web/", "apps/"],
excluded: [~r"/_build/", ~r"/deps/"]
},
#
# If you create your own checks, you must specify the source files for
# them here, so they can be loaded by Credo before running the analysis.
requires: [],
#
# Credo automatically checks for updates, like e.g. Hex does.
# You can disable this behaviour below:
check_for_updates: true,
#
# If you want to enforce a style guide and need a more traditional linting
# experience, you can change `strict` to `true` below:
strict: false,
#
# If you want to use uncolored output by default, you can change `color`
# to `false` below:
color: true,
#
# You can customize the parameters of any check by adding a second element
# to the tuple.
#
# To disable a check put `false` as second element:
#
# {Credo.Check.Design.DuplicatedCode, false}
#
checks: [
{Credo.Check.Readability.ModuleDoc, false},
{Credo.Check.Consistency.ExceptionNames},
{Credo.Check.Consistency.LineEndings},
{Credo.Check.Consistency.ParameterPatternMatching},
{Credo.Check.Consistency.SpaceAroundOperators},
{Credo.Check.Consistency.SpaceInParentheses},
{Credo.Check.Consistency.TabsOrSpaces},

# For some checks, like AliasUsage, you can only customize the priority
# Priority values are: `low, normal, high, higher`
{Credo.Check.Design.AliasUsage, priority: :low},

# For others you can set parameters

# If you don't want the `setup` and `test` macro calls in ExUnit tests
# or the `schema` macro in Ecto schemas to trigger DuplicatedCode, just
# set the `excluded_macros` parameter to `[:schema, :setup, :test]`.
{Credo.Check.Design.DuplicatedCode, excluded_macros: []},

# You can also customize the exit_status of each check.
# If you don't want TODO comments to cause `mix credo` to fail, just
# set this value to 0 (zero).
{Credo.Check.Design.TagTODO, exit_status: 2},
{Credo.Check.Design.TagFIXME},
{Credo.Check.Readability.FunctionNames},
{Credo.Check.Readability.LargeNumbers},
{Credo.Check.Readability.MaxLineLength, priority: :low, max_length: 150},
{Credo.Check.Readability.ModuleAttributeNames},
{Credo.Check.Readability.ModuleNames},
{Credo.Check.Readability.ParenthesesOnZeroArityDefs},
{Credo.Check.Readability.ParenthesesInCondition},
{Credo.Check.Readability.PredicateFunctionNames},
{Credo.Check.Readability.PreferImplicitTry},
{Credo.Check.Readability.RedundantBlankLines},
{Credo.Check.Readability.StringSigils},
{Credo.Check.Readability.TrailingBlankLine},
{Credo.Check.Readability.TrailingWhiteSpace},
{Credo.Check.Readability.VariableNames},
{Credo.Check.Readability.Semicolons},
{Credo.Check.Readability.SpaceAfterCommas},
{Credo.Check.Refactor.DoubleBooleanNegation},
{Credo.Check.Refactor.CondStatements},
{Credo.Check.Refactor.CyclomaticComplexity},
{Credo.Check.Refactor.FunctionArity},
{Credo.Check.Refactor.MatchInCondition},
{Credo.Check.Refactor.NegatedConditionsInUnless},
{Credo.Check.Refactor.NegatedConditionsWithElse},
{Credo.Check.Refactor.Nesting, max_nesting: 3},
{Credo.Check.Refactor.PipeChainStart},
{Credo.Check.Refactor.UnlessWithElse},
{Credo.Check.Warning.BoolOperationOnSameValues},
{Credo.Check.Warning.IExPry},
{Credo.Check.Warning.IoInspect},
{Credo.Check.Warning.LazyLogging},
{Credo.Check.Warning.OperationOnSameValues},
{Credo.Check.Warning.OperationWithConstantResult},
{Credo.Check.Warning.UnusedEnumOperation},
{Credo.Check.Warning.UnusedFileOperation},
{Credo.Check.Warning.UnusedKeywordOperation},
{Credo.Check.Warning.UnusedListOperation},
{Credo.Check.Warning.UnusedPathOperation},
{Credo.Check.Warning.UnusedRegexOperation},
{Credo.Check.Warning.UnusedStringOperation},
{Credo.Check.Warning.UnusedTupleOperation},

# Controversial and experimental checks (opt-in, just remove `, false`)
#
{Credo.Check.Refactor.ABCSize, false},
{Credo.Check.Refactor.AppendSingleItem, false},
{Credo.Check.Refactor.VariableRebinding, false},
{Credo.Check.Warning.MapGetUnsafePass, false},
{Credo.Check.Consistency.MultiAliasImportRequireUse, false},

# Deprecated checks (these will be deleted after a grace period)
{Credo.Check.Readability.Specs, false},
{Credo.Check.Warning.NameRedeclarationByAssignment, false},
{Credo.Check.Warning.NameRedeclarationByCase, false},
{Credo.Check.Warning.NameRedeclarationByDef, false},
{Credo.Check.Warning.NameRedeclarationByFn, false}

# Custom checks can be created using `mix credo.gen.check`.
#
]
}
]
}
7 changes: 7 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
inputs: [
"{lib,config,test}/**/*.{ex,exs}",
"mix.exs"
]
]

2 changes: 1 addition & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
@@ -31,4 +31,4 @@ config :exsolr,
# Configuration from the imported file will override the ones defined
# here (which is why it is important to import them last).
#
import_config "#{Mix.env}.exs"
import_config "#{Mix.env()}.exs"
60 changes: 52 additions & 8 deletions lib/exsolr.ex
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
defmodule Exsolr.Client do
@moduledoc """
Behaviour module for Exsolr client
"""

@callback info() :: Map.t()
@callback get(Keyword.t()) :: Map.t()
@callback suggest(Keyword.t()) :: Map.t()
@callback more_like_this(Keyword.t()) :: Map.t()
@callback add(Map.t()) :: Atom.t()
@callback commit() :: Atom.t()
@callback delete_by_id(any()) :: Atom.t()
@callback delete_all() :: Atom.t()
end

defmodule Exsolr do
@moduledoc """
Solr wrapper made in Elixir.
"""

alias Exsolr.Config
alias Exsolr.Indexer
alias Exsolr.Searcher
alias Exsolr.{Config, Indexer, Searcher, Suggest, MoreLikeThis}

@behaviour Exsolr.Client

@doc """
Returns a map containing the solr connection info
@@ -16,7 +31,7 @@ defmodule Exsolr do
%{hostname: "localhost", port: 8983, core: "elixir_test"}
"""
def info do
Config.info
Config.info()
end

@doc """
@@ -25,20 +40,49 @@ defmodule Exsolr do
## Example
iex> Exsolr.get(q: "roses", fq: ["blue", "violet"])
iex> Exsolr.get(q: "red roses", defType: "disMax")
:tbd
iex> Exsolr.get(q: "red roses", defType: "disMax")
:tbd
"""
def get(query_params) do
Searcher.get(query_params)
end

@doc """
Send a more like this (mlt) request to Solr.
## Example
iex> Exsolr.more_like_this(q: "roses", mlt.fl: ["color", "height"])
:tbd
iex> Exsolr.more_like_this(q: "red roses", rows: 10)
:tbd
"""
def more_like_this(query_params) do
MoreLikeThis.more_like_this(query_params)
end

@doc """
Send a search request to Solr.
## Example
iex> Exsolr.suggest("suggest.q": "roses", "suggest.dictionary": "suggestDictionary")
:tbd
"""
def suggest(query_params) do
Suggest.suggest(query_params)
end

@doc """
Adds the `document` to Solr.
## Example
iex> Exsolr.add(%{id: 1, price: 1.00})
:tbd
"""
def add(document) do
Indexer.add(document)
@@ -48,7 +92,7 @@ defmodule Exsolr do
Commits the pending changes to Solr
"""
def commit do
Indexer.commit
Indexer.commit()
end

@doc """
@@ -64,6 +108,6 @@ defmodule Exsolr do
https://wiki.apache.org/solr/FAQ#How_can_I_delete_all_documents_from_my_index.3F
"""
def delete_all do
Indexer.delete_all
Indexer.delete_all()
end
end
23 changes: 22 additions & 1 deletion lib/exsolr/config.ex
Original file line number Diff line number Diff line change
@@ -34,6 +34,28 @@ defmodule Exsolr.Config do
"""
def select_url, do: "#{base_url}/select"

@doc """
Returns the base url to do `suggest` queries to solr
## Examples
iex> Exsolr.Config.suggest_url
"http://localhost:8983/solr/elixir_test/suggest"
"""
def suggest_url, do: "#{base_url}/suggest"

@doc """
Returns the base url to do `mlt` queries to solr
## Examples
iex> Exsolr.Config.more_like_this_url
"http://localhost:8983/solr/elixir_test/mlt"
"""
def more_like_this_url, do: "#{base_url}/mlt"

@doc """
Returns the base url to do `update` queries to solr
@@ -46,4 +68,3 @@ defmodule Exsolr.Config do

defp base_url, do: "http://#{hostname}:#{port}/solr/#{core}"
end

15 changes: 9 additions & 6 deletions lib/exsolr/http_response.ex
Original file line number Diff line number Diff line change
@@ -8,14 +8,17 @@ defmodule Exsolr.HttpResponse do
def body({status, response}) do
case {status, response} do
{:ok, %HTTPoison.Response{status_code: 200, body: response_body}} ->
_ = Logger.debug response_body
response_body
Logger.debug(fn -> response_body end)
{:ok, response_body}

{:ok, %HTTPoison.Response{status_code: status_code, body: response_body}} ->
_ = Logger.warn "status_code: #{status_code} - #{response_body}"
nil
Logger.warn(fn -> "status_code: #{status_code} - #{response_body}" end)
error_message = response_body |> Poison.decode!() |> get_in(["error", "msg"])
{:error, status_code, error_message}

{_, %HTTPoison.Error{id: _, reason: error_reason}} ->
_ = Logger.error error_reason
nil
Logger.error(fn -> error_reason end)
{:error, error_reason}
end
end
end
30 changes: 16 additions & 14 deletions lib/exsolr/indexer.ex
Original file line number Diff line number Diff line change
@@ -7,9 +7,9 @@ defmodule Exsolr.Indexer do
alias Exsolr.HttpResponse

def add(document) do
json_docs_update_url
|> HTTPoison.post(encode(document), json_headers)
|> HttpResponse.body
json_docs_update_url()
|> HTTPoison.post(encode(document), json_headers())
|> HttpResponse.body()
end

@doc """
@@ -26,7 +26,7 @@ defmodule Exsolr.Indexer do
https://cwiki.apache.org/confluence/display/solr/Uploading+Data+with+Index+Handlers#UploadingDatawithIndexHandlers-JSONFormattedIndexUpdates
"""
def delete_by_id(id) do
update_request(json_headers, delete_by_id_json_body(id))
update_request(json_headers(), delete_by_id_json_body(id))
end

@doc """
@@ -35,21 +35,21 @@ defmodule Exsolr.Indexer do
https://wiki.apache.org/solr/FAQ#How_can_I_delete_all_documents_from_my_index.3F
"""
def delete_all do
update_request(xml_headers, delete_all_xml_body)
update_request(xml_headers(), delete_all_xml_body())
commit
end

@doc """
Commit changes into Solr
"""
def commit do
update_request(xml_headers, commit_xml_body)
update_request(xml_headers(), commit_xml_body())
end

defp update_request(headers, body) do
Config.update_url
Config.update_url()
|> HTTPoison.post(body, headers)
|> HttpResponse.body
|> HttpResponse.body()
end

defp json_headers, do: [{"Content-Type", "application/json"}]
@@ -67,25 +67,27 @@ defmodule Exsolr.Indexer do
~s({"delete":{"id":"42"}})
"""
def delete_by_id_json_body(id) when is_integer(id) do
def delete_by_id_json_body(id) when is_integer(id) do
id
|> Integer.to_string()
|> delete_by_id_json_body
|> delete_by_id_json_body()
end

def delete_by_id_json_body(id) do
{:ok, body} = %{delete: %{id: id}}
|> Poison.encode
{:ok, body} =
%{delete: %{id: id}}
|> Poison.encode()

body
end

defp delete_all_xml_body, do: "<delete><query>*:*</query></delete>"
defp commit_xml_body, do: "<commit/>"

defp json_docs_update_url, do: "#{Config.update_url}/json/docs"
defp json_docs_update_url, do: "#{Config.update_url()}/json/docs"

defp encode(document) do
{:ok, body} = Poison.encode(document)
body
end
end
end
Loading