Skip to content

Commit

Permalink
Add "key_column" and "merge_columns" options for search reducers
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Dec 4, 2013
1 parent c841af5 commit 6cef124
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/droonga/plugin/distributor/search.rb
Expand Up @@ -66,6 +66,7 @@ def search(envelope)
final_attributes = collect_output_attributes(output["attributes"])
output["attributes"] = format_attributes_to_array_style(output["attributes"])
output["attributes"] += collect_sort_attributes(output["attributes"], query["sortBy"])
output["attributes"] << "_key" unless output["attributes"].include?("_key")

elements[element] = sort_reducer(output["attributes"], query["sortBy"])
# On the reducing phase, we apply only "limit". We cannot apply
Expand Down Expand Up @@ -246,12 +247,21 @@ def collect_sort_attributes(attributes, sort_keys)

ASCENDING_OPERATOR = "<".freeze
DESCENDING_OPERATOR = ">".freeze
MERGE_ATTRIBUTES = ["_nsubrecs", "_subrecs"]

def sort_reducer(attributes, sort_keys)
attributes ||= []
sort_keys ||= []
sort_keys = sort_keys["keys"] || [] if sort_keys.is_a?(Hash)

key_column_index = attributes.index("_key")
merge_columns = []
attributes.each_with_index do |attribute, index|
source = attribute
source = attribute["source"] if attribute.is_a?(Hash)
merge_colums << index if MERGE_ATTRIBUTES.include?(source)
end

operators = sort_keys.collect do |sort_key|
operator = ASCENDING_OPERATOR
if sort_key[0] == "-"
Expand All @@ -267,6 +277,8 @@ def sort_reducer(attributes, sort_keys)
{
"type" => "sort",
"operators" => operators,
"key_column" => key_column_index,
"merge_columns" => merge_columns,
}
end
end
Expand Down

0 comments on commit 6cef124

Please sign in to comment.