Skip to content

Commit

Permalink
add context to search results
Browse files Browse the repository at this point in the history
  • Loading branch information
elrayle committed Nov 15, 2017
1 parent 42a35de commit d33346d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/qa/authorities/linked_data/rdf_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ def extract_preds(graph, preds)
preds[:optional].each do |key, pred|
pattern([:uri, pred, key], optional: true)
end
preds[:context].each do |key, pred|
pattern([:uri, pred, key], optional: true)
end
end
end

Expand Down
29 changes: 27 additions & 2 deletions lib/qa/authorities/linked_data/search_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def parse_search_authority_response(graph, language)
end

def preds_for_search
{ required: required_search_preds, optional: optional_search_preds }
{ required: required_search_preds, optional: optional_search_preds, context: context_search_preds }
end

def required_search_preds
Expand All @@ -61,6 +61,10 @@ def optional_search_preds
preds
end

def context_search_preds
search_config.results_context || {}
end

def consolidate_search_results(results)
consolidated_results = {}
return consolidated_results if results.nil? || !results.count.positive?
Expand All @@ -72,25 +76,46 @@ def consolidate_search_results(results)
consolidated_hash[:label] = object_value(stmt_hash, consolidated_hash, :label, false)
consolidated_hash[:altlabel] = object_value(stmt_hash, consolidated_hash, :altlabel, false)
consolidated_hash[:sort] = object_value(stmt_hash, consolidated_hash, :sort, false)

# add context
context_search_preds.each_key do |k|
consolidated_hash[k] = object_value(stmt_hash, consolidated_hash, k, false)
end

consolidated_results[uri] = consolidated_hash
end
consolidated_results.each do |res|
consolidated_hash = res[1]
consolidated_hash[:label] = sort_string_by_language consolidated_hash[:label]
consolidated_hash[:altlabel] = sort_string_by_language consolidated_hash[:altlabel]
consolidated_hash[:sort] = sort_string_by_language consolidated_hash[:sort]

# consolidate context
context_search_preds.each_key do |k|
consolidated_hash[k] = sort_string_by_language consolidated_hash[k]
end
end
consolidated_results
end

def convert_search_to_json(consolidated_results)
json_results = []
consolidated_results.each do |uri, h|
json_results << { uri: uri, id: h[:id], label: full_label(h[:label], h[:altlabel]), sort: h[:sort] }
json_result = { uri: uri, id: h[:id], label: full_label(h[:label], h[:altlabel]), sort: h[:sort] }
json_result[:context] = context_json(h) if search_config.supports_context?
json_results << json_result
end
json_results
end

def context_json(consolidated_results_hash)
context_json = {}
context_search_preds.each_key do |k|
context_json[k] = consolidated_results_hash[k]
end
context_json
end

def full_label(label = [], altlabel = [])
lbl = wrap_labels(label)
lbl += " (#{altlabel.join(', ')})" unless altlabel.nil? || altlabel.length <= 0
Expand Down

0 comments on commit d33346d

Please sign in to comment.