Module: ResultsOrder

Included in:
DocsHelper
Defined in:
app/helpers/results_order.rb

Instance Method Summary collapse

Instance Method Details

#add_first_for_id(items, uid, item, item_fields) ⇒ Object

Adds item and first set of item fields


21
22
23
24
25
# File 'app/helpers/results_order.rb', line 21

def add_first_for_id(items, uid, item, item_fields)
  items[uid] = item
  items[uid]["item_fields"] = [list_item_info(item_fields, item)]
  return items
end

#gen_unique_results(doc, items, item_ids, uid, item_fields) ⇒ Object

Groups item fields by ID


9
10
11
12
13
14
15
16
17
18
# File 'app/helpers/results_order.rb', line 9

def gen_unique_results(doc, items, item_ids, uid, item_fields)
  if item_ids.include?(uid) # If item with same ID is included already
    items[uid]["item_fields"].to_a.push(list_item_info(item_fields, doc))
  else # If item with same ID has not been added yet
    items = add_first_for_id(items, uid, doc, item_fields)
    item_ids.to_a.push(uid)
  end

  return items, item_ids
end

#get_details(doc) ⇒ Object

Gets the dataspec, id, and item_fields


36
37
38
39
40
41
42
# File 'app/helpers/results_order.rb', line 36

def get_details(doc)
  dataspec = get_dataspec(doc)
  uid = get_id(doc, dataspec)
  item_fields = dataspec.item_fields

  return dataspec, uid, item_fields
end

#get_id(doc, dataspec) ⇒ Object

Gets the ID field for the item based on the dataspec


3
4
5
6
# File 'app/helpers/results_order.rb', line 3

def get_id(doc, dataspec)
  id_field = dataspec.id_field
  return getText(doc, id_field, doc)
end

#list_item_info(item_fields, item) ⇒ Object

Generates a list of item fields and vals


28
29
30
31
32
33
# File 'app/helpers/results_order.rb', line 28

def list_item_info(item_fields, item)
  return item_fields.inject({}) do |item_info, field|
    item_info[field] = item["_source"][field]
    item_info
  end
end

#order_results(docs) ⇒ Object

Puts results in appropriate order and handles item fields


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/helpers/results_order.rb', line 45

def order_results(docs)
  item_ids = []
  items = Hash.new
  
  docs.sort { |a, b| a["_score"] <=> b["_score"] }.reverse.each do |doc|
    # Get the dataspec, id, item_fields
    dataspec, uid, item_fields = get_details(doc)

    # Handle differently for item_fields vs none
    if item_fields
      items, item_ids = gen_unique_results(doc, items, item_ids, uid, item_fields)
    else
      items[uid] = doc
    end
  end

  return items
end