-
Notifications
You must be signed in to change notification settings - Fork 3
Improve API #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Improve API #11
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ccd7b38
This feels better, but wip
f1e31f3
Better handling of bulk imports (still broken tests though)
a9635b2
Make sure documents get converted
3a886e8
Whoops! Thats a mistake
b22e24f
Load to the index
29799a6
Missing config key
3468556
Add tests for ElasticSync.Index
247f4f6
Lazily evaluate the config
7472717
Fixed tests
4310476
Refactor Mix.Tasks.Reindex
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
---|---|---|
@@ -1,8 +1,2 @@ | ||
defmodule ElasticSync do | ||
import Tirexs.HTTP | ||
|
||
def version do | ||
{:ok, 200, info} = get("/") | ||
info.version | ||
end | ||
end |
This file contains hidden or 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 hidden or 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 |
---|---|---|
@@ -1,122 +1,87 @@ | ||
defmodule ElasticSync.Repo do | ||
import Tirexs.Bulk | ||
|
||
alias ElasticSync.{Schema, Index} | ||
alias ElasticSync.Index | ||
alias Tirexs.HTTP | ||
alias Tirexs.Resources.APIs, as: API | ||
|
||
def search(schema, query, opts \\ []) | ||
def search(schema, query, opts) when is_binary(query) do | ||
def search(schema, query) when is_binary(query) do | ||
schema | ||
|> to_search_url(opts) | ||
|> to_search_url() | ||
|> HTTP.get(%{q: query}) | ||
end | ||
def search(schema, [search: query] = dsl, opts) do | ||
opts = | ||
dsl | ||
|> Keyword.take([:index, :type]) | ||
|> Keyword.merge(opts) | ||
|
||
search(schema, query, opts) | ||
def search(schema, [search: query]) do | ||
search(schema, query) | ||
end | ||
def search(schema, query, opts) do | ||
def search(schema, query) do | ||
schema | ||
|> to_search_url(opts) | ||
|> to_search_url() | ||
|> HTTP.post(query) | ||
end | ||
|
||
def insert(record, opts \\ []) do | ||
def insert(record) do | ||
record.__struct__ | ||
|> to_index_url(opts) | ||
|> to_index_url() | ||
|> HTTP.post(%{id: record.id}, to_document(record)) | ||
end | ||
|
||
def insert!(record, opts \\ []) do | ||
def insert!(record) do | ||
record.__struct__ | ||
|> to_index_url(opts) | ||
|> to_index_url() | ||
|> HTTP.post!(%{id: record.id}, to_document(record)) | ||
end | ||
|
||
def update(record, opts \\ []) do | ||
def update(record) do | ||
record | ||
|> to_document_url(opts) | ||
|> to_document_url() | ||
|> HTTP.put(to_document(record)) | ||
end | ||
|
||
def update!(record, opts \\ []) do | ||
def update!(record) do | ||
record | ||
|> to_document_url(opts) | ||
|> to_document_url() | ||
|> HTTP.put!(to_document(record)) | ||
end | ||
|
||
def delete(record, opts \\ []) do | ||
def delete(record) do | ||
record | ||
|> to_document_url(opts) | ||
|> HTTP.delete! | ||
|> to_document_url() | ||
|> HTTP.delete | ||
end | ||
|
||
def delete!(record, opts \\ []) do | ||
def delete!(record) do | ||
record | ||
|> to_document_url(opts) | ||
|> to_document_url() | ||
|> HTTP.delete! | ||
end | ||
|
||
def insert_all(schema, records, opts \\ []) when is_list(records) do | ||
index_name = | ||
schema | ||
|> Schema.merge(opts) | ||
|> Schema.get(:index) | ||
|
||
with {:ok, 200, response} <- bulk_index(schema, records, opts), | ||
{:ok, 200, _} <- Index.refresh(index_name), | ||
def insert_all(schema, records) when is_list(records) do | ||
with {:ok, 200, response} <- load(schema.__elastic_sync__, records), | ||
{:ok, 200, _} <- Index.refresh(schema.__elastic_sync__), | ||
do: {:ok, 200, response} | ||
end | ||
|
||
def bulk_index(schema, records, opts \\ []) when is_list(records) do | ||
data = Enum.map(records, &to_reindex_document/1) | ||
|
||
payload = | ||
schema | ||
|> Schema.merge(opts) | ||
|> Map.take([:index, :type]) | ||
|> Map.to_list() | ||
|> bulk(do: index(data)) | ||
@doc false | ||
def load(index, records) do | ||
Index.load(index, Enum.map(records, &to_document/1)) | ||
end | ||
|
||
Tirexs.bump!(payload)._bulk() | ||
def to_search_url(schema) do | ||
url_for(:_search, schema) | ||
end | ||
|
||
def to_search_url(schema, opts \\ []) do | ||
url_for(:_search, schema, opts) | ||
def to_index_url(schema) do | ||
url_for(:index, schema) | ||
end | ||
|
||
def to_index_url(schema, opts \\ []) do | ||
url_for(:index, schema, opts) | ||
def to_document_url(record) do | ||
url_for(:doc, record.__struct__, [record.id]) | ||
end | ||
|
||
def to_document_url(record, opts \\ []) do | ||
url_for(:doc, record.__struct__, opts, [record.id]) | ||
defp url_for(fun_name, schema, paths \\ []) do | ||
index = schema.__elastic_sync__ | ||
apply(API, fun_name, [index.name, index.type] ++ paths) | ||
end | ||
|
||
def to_document(record) do | ||
record.__struct__.to_search_document(record) | ||
end | ||
|
||
defp url_for(fun_name, schema, opts, paths \\ []) do | ||
schema = Schema.merge(schema, opts) | ||
index = Schema.get(schema, :index) | ||
type = Schema.get(schema, :type) | ||
apply(API, fun_name, [index, type] ++ paths) | ||
end | ||
|
||
# Tirexs only accepts a list for bulk | ||
defp to_reindex_document(record) do | ||
document = to_document(record) | ||
|
||
cond do | ||
is_list(document) -> | ||
document | ||
true -> | ||
Enum.into(document, []) | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you mention the new config feature in The README. Sorry I missed that.