Class: Picky::Query::Boosts

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

Overview

Calculates boosts for combinations.

Example:

Someone searches for peter fish.
Picky might match this to categories as follows:
  [:name, :food]
and
  [:name, :surname]

This class is concerned with calculating boosts for the category combinations.

Implement either

#boost_for(combinations)

or

#boost_for_categories(category_names) # Subclass this class for this.

And return a boost (float).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(boosts = {}) ⇒ Boosts

Needs a Hash of

[:category_name1, :category_name2] => +3

(some positive or negative weight)



34
35
36
# File 'lib/picky/query/boosts.rb', line 34

def initialize boosts = {}
  @boosts = boosts
end

Instance Attribute Details

#boostsObject (readonly)

Returns the value of attribute boosts.



26
27
28
# File 'lib/picky/query/boosts.rb', line 26

def boosts
  @boosts
end

Instance Method Details

#==(other) ⇒ Object

A Weights instance is == to another if the weights are the same.



71
72
73
# File 'lib/picky/query/boosts.rb', line 71

def == other
  @boosts == other.boosts
end

#boost_for(combinations) ⇒ Object

API.

Calculates a score for the combinations. Implement #weight_for(category_names) if you don’t need the actual combinations, just the category names.

Note: Cache this if more complicated weighings become necessary. Note: Maybe make combinations comparable to Symbols?

TODO Push into categories? Store boosts in categories?



64
65
66
# File 'lib/picky/query/boosts.rb', line 64

def boost_for combinations
  boost_for_categories combinations.map { |combination| combination.category_name }
end

#boost_for_categories(names) ⇒ Object

API.

Get the boost for an array of category names.

Example:

[:name, :height, :color] returns +3, but
[:name, :height, :street] returns -1.

Note: Use Array#clustered_uniq to make

[:a, :a, :b, :a] => [:a, :b, :a]


49
50
51
# File 'lib/picky/query/boosts.rb', line 49

def boost_for_categories names
  @boosts[names.clustered_uniq] || 0
end

#to_sObject

Prints out a nice representation of the configured weights.



78
79
80
# File 'lib/picky/query/boosts.rb', line 78

def to_s
  "#{self.class}(#@boosts)"
end