Class: Picky::Query::Allocation

Inherits:
Object
  • Object
show all
Defined in:
lib/picky/query/allocation.rb

Overview

An Allocation contains an ordered list of tuples (Combinations). The Combinations are ordered according to the order of the words in the query.

It offers convenience methods to calculate the #ids etc.

An Allocation is normally contained in an Allocations container.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index, combinations) ⇒ Allocation

An allocation is defined by:

* An index where the allocation was found, e.g. documents.
* A number of combinations, e.g. 1. author:eloy, 2. name:bla.


24
25
26
27
# File 'lib/picky/query/allocation.rb', line 24

def initialize index, combinations
  @index = index
  @combinations = combinations
end

Instance Attribute Details

#combinationsObject (readonly)

Returns the value of attribute combinations.



16
17
18
# File 'lib/picky/query/allocation.rb', line 16

def combinations
  @combinations
end

#countObject (readonly)

Returns the value of attribute count.



16
17
18
# File 'lib/picky/query/allocation.rb', line 16

def count
  @count
end

#scoreObject (readonly)

Returns the value of attribute score.



16
17
18
# File 'lib/picky/query/allocation.rb', line 16

def score
  @score
end

Instance Method Details

#<=>(other_allocation) ⇒ Object

Sort highest score first.



116
117
118
# File 'lib/picky/query/allocation.rb', line 116

def <=> other_allocation
   other_allocation.score <=> self.score
end

#backendObject



37
38
39
# File 'lib/picky/query/allocation.rb', line 37

def backend
  @index.backend
end

#calculate_ids(amount, offset) ⇒ Object

Asks the backend for the (intersected) ids.

Note: Combinations can be empty on eg. query “alan history” and category :title is ignored (ie. removed).



71
72
73
74
75
76
77
# File 'lib/picky/query/allocation.rb', line 71

def calculate_ids amount, offset
  return [] if @combinations.empty? # Checked here to avoid checking in each backend.

  # TODO Redesign such that ids is only created (and cached) if requested.
  #
  backend.ids @combinations, amount, offset
end

#calculate_score(boosts) ⇒ Object

Asks the backend for the total score and adds the boosts to it.

Note: Combinations can be empty on eg. query “alan history” and category :title is ignored (ie. removed).



48
49
50
51
52
53
54
55
# File 'lib/picky/query/allocation.rb', line 48

def calculate_score boosts
  @score ||= (if @combinations.empty?
    0 # Optimization.
  else
    # Note: Was @backend.score(@combinations) - indirection for maximum flexibility.
    @combinations.score + boosts.boost_for(@combinations)
  end)
end

#each(&block) ⇒ Object

TODO



31
32
33
# File 'lib/picky/query/allocation.rb', line 31

def each &block
  @combinations.each &block
end

#idsObject

Ids return by default [].

TODO Calculate ids here.



61
62
63
# File 'lib/picky/query/allocation.rb', line 61

def ids
  @ids ||= []
end

#process!(amount, offset, sorting = nil) ⇒ Object

This starts the searching process.

Returns the calculated ids (from the offset).

Parameters:

* amount: The amount of ids to calculate.
* offset: The offset to calculate them from.


87
88
89
90
91
92
# File 'lib/picky/query/allocation.rb', line 87

def process! amount, offset, sorting = nil
  calculated_ids = calculate_ids amount, offset
  calculated_ids.sort_by! &sorting if sorting
  @count = calculated_ids.size                         # cache the count before throwing away the ids
  @ids   = calculated_ids.slice!(offset, amount) || [] # slice out the relevant part
end

#process_with_illegals!(amount, offset, illegal_ids, sorting = nil) ⇒ Object

Same as the above, but with illegal ids. Parameter added:

* illegal_ids: ids to ignore.


96
97
98
99
100
101
102
103
104
105
106
# File 'lib/picky/query/allocation.rb', line 96

def process_with_illegals! amount, offset, illegal_ids, sorting = nil
  # Note: Fairly inefficient calculation since it
  # assumes the worst case that the ids contain
  # all illegal ids.
  #
  calculated_ids = calculate_ids amount + illegal_ids.size, offset
  calculated_ids = calculated_ids - illegal_ids
  calculated_ids.sort_by! &sorting if sorting
  @count = calculated_ids.size                         # cache the count before throwing away the ids
  @ids   = calculated_ids.slice!(offset, amount) || [] # slice out the relevant part
end

#remove(categories = []) ⇒ Object



110
111
112
# File 'lib/picky/query/allocation.rb', line 110

def remove categories = []
  @combinations.remove categories
end

#to_qualifiersObject



128
129
130
# File 'lib/picky/query/allocation.rb', line 128

def to_qualifiers
  @combinations.to_qualifiers
end

#to_resultObject

Transform the allocation into result form.



122
123
124
# File 'lib/picky/query/allocation.rb', line 122

def to_result
  [@index.result_identifier, self.score, self.count, @combinations.to_result, self.ids] if self.count && self.count > 0
end

#to_sObject



134
135
136
# File 'lib/picky/query/allocation.rb', line 134

def to_s
  "Allocation(#{to_result})"
end