Class: Tem::Mr::Search::WebClientQueryBuilder

Inherits:
MapReduceJob
  • Object
show all
Defined in:
lib/tem_mr_search/web_client_query_builder.rb

Instance Attribute Summary

Attributes inherited from MapReduceJob

#attributes, #finalizer, #id_attribute, #mapper, #reducer

Class Method Summary collapse

Methods inherited from MapReduceJob

#bind, #initialize, #to_hash, #unpack_decrypted_output

Constructor Details

This class inherits a constructor from Tem::Mr::Search::MapReduceJob

Class Method Details

.query(options) ⇒ Object

Builds a client query covering preferences expressed in the Web UI.

The supported (and required) options are:

layovers_cost:: the cost of each layover
start_time_cost:: the cost of the flight's departure time, in minutes
duration_cost:: the cost of each minute of flying


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/tem_mr_search/web_client_query_builder.rb', line 11

def self.query(options)
  QueryBuilder.query { |q|
    q.attributes :price => :tem_short, :start_time => :tem_short,
                 :end_time => :tem_short, :layovers => :tem_short
    q.id_attribute :flight
    
    # Score: 20000 - price - layover_cost * layovers -
    #                start_time * start_time_cost -
    #                (end_time - start_time) * duration_cost
    q.map { |s|
      s.ldwc 20000
      s.ldw :price
      s.sub
      s.ldw :end_time
      s.ldw :start_time
      s.sub
      s.ldwc options[:duration_cost]
      s.mul
      s.sub
      [:start_time, :layovers].each do |factor|
        s.ldw factor
        s.ldwc options[:"#{factor}_cost"]
        s.mul
        s.sub
      end
      s.stw :score
    }

    # The greater score wins.
    q.reduce { |s|
      s.ldw :score1
      s.ldw :score2
      s.cmp
      s.stw :comparison
    }
  }
end