Skip to content
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

Cloudant Search for Array Values #89

Merged
merged 1 commit into from
Oct 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,7 @@ defmodule Artemis.Drivers.IBMCloudant.CreateSearch do
defp generate_search_index(fields) do
fields_without_default = List.delete(fields, :_id)

clauses =
Enum.map(fields_without_default, fn field ->
"""
if (typeof(doc.#{field}) !== 'undefined') {
index("#{field}", doc.#{field});
}
"""
end)
clauses = Enum.map(fields_without_default, &generate_search_index_clause/1)

"""
function (doc) {
Expand All @@ -90,6 +83,24 @@ defmodule Artemis.Drivers.IBMCloudant.CreateSearch do
"""
end

defp generate_search_index_clause({field, :array}) do
"""
if (typeof(doc.#{field}) !== 'undefined') {
for (var i in doc.#{field}) {
index("#{field}", doc.#{field}[i], {"store": true});
}
}
"""
end

defp generate_search_index_clause(field) do
"""
if (typeof(doc.#{field}) !== 'undefined') {
index("#{field}", doc.#{field});
}
"""
end

defp create_search_index(host, path, document, current_indexes, search_function, options) do
search_index = Map.put(%{}, options[:index], %{"analyzer" => "standard", "index" => search_function})
indexes = Map.merge(current_indexes, search_index)
Expand Down
2 changes: 2 additions & 0 deletions apps/artemis/lib/artemis/helpers/ibm_cloudant_search.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ defmodule Artemis.Helpers.IBMCloudantSearch do

key_sections =
Enum.map(keys_with_default, fn key ->
key = if is_tuple(key), do: elem(key, 0), else: key

tokens = Enum.map(words, &"#{key}:#{&1}")
joined = Enum.join(tokens, " AND ")

Expand Down