Module: CheapSkate::Facet

Defined in:
lib/cheap_skate/models.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#facet_fieldsObject

Returns the value of attribute facet_fields.



61
62
63
# File 'lib/cheap_skate/models.rb', line 61

def facet_fields
  @facet_fields
end

#facet_limitObject

Returns the value of attribute facet_limit.



61
62
63
# File 'lib/cheap_skate/models.rb', line 61

def facet_limit
  @facet_limit
end

#facet_offsetObject

Returns the value of attribute facet_offset.



61
62
63
# File 'lib/cheap_skate/models.rb', line 61

def facet_offset
  @facet_offset
end

#facet_queriesObject

Returns the value of attribute facet_queries.



61
62
63
# File 'lib/cheap_skate/models.rb', line 61

def facet_queries
  @facet_queries
end

#facet_totalObject

Returns the value of attribute facet_total.



61
62
63
# File 'lib/cheap_skate/models.rb', line 61

def facet_total
  @facet_total
end

#facetsObject

Returns the value of attribute facets.



61
62
63
# File 'lib/cheap_skate/models.rb', line 61

def facets
  @facets
end

Instance Method Details

#add_facet_query(query, query_string) ⇒ Object



92
93
94
95
# File 'lib/cheap_skate/models.rb', line 92

def add_facet_query(query, query_string)
  @facet_queries ||= []
  @facet_queries << {:query=>query, :results=>0, :query_string=>query_string}
end

#add_facets_to_query(params) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/cheap_skate/models.rb', line 62

def add_facets_to_query(params)

  if params['facet.limit'] && !params['facet.limit'].empty?
    @facet_limit = params['facet.limit'].first.to_i
  else
    @facet_limit = 10
  end
  if params['facet.offset'] && !params['facet.offset'].empty?
    @facet_offset = params['facet.offset'].first.to_i
  else
    @facet_offset = 0
  end

  @facet_fields = {}
  params["facet.field"].each do | field |
    @facet_fields[field.to_sym] = {}
  end      

  @filter_proc = lambda do |doc,score,searcher|
    @facet_fields.keys.each do |field|
      [*searcher[doc][field]].each do |term|  
        next if term.nil?
        @facet_fields[field][term] ||=0
        @facet_fields[field][term] += 1
      end
    end
  end
  
end

#add_facets_to_results(query) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/cheap_skate/models.rb', line 97

def add_facets_to_results(query)

  @facet_fields = query.facet_fields
  @facet_limit = query.facet_limit
  @facet_queries = query.facet_queries
  @facet_offset = query.facet_offset
end

#flatten_facet_arrayObject



113
114
115
116
117
118
119
# File 'lib/cheap_skate/models.rb', line 113

def flatten_facet_array
  flat_arr = {}
  @facet_fields.each_pair do |k,v|
    flat_arr[k] = v.flatten
  end
  flat_arr
end

#format_facetsObject



105
106
107
108
109
110
111
# File 'lib/cheap_skate/models.rb', line 105

def format_facets      
  case self.nl_format
  when "flat" then flatten_facet_array
  when "map" then map_facet_array
  else @facet_fields
  end      
end

#map_facet_arrayObject



121
122
123
124
125
126
127
128
129
130
# File 'lib/cheap_skate/models.rb', line 121

def map_facet_array
  map_arr = {}
  @facet_fields.each_pair do |k,v|
    map_arr[k] ||= {}
    v.each do |val_pair|
     map_arr[k][val_pair[0]] = val_pair[1]
   end
  end
  map_arr      
end

#parse_facet_query(params) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/cheap_skate/models.rb', line 132

def parse_facet_query(params)     
 [*params['facet.query']].each do |q|
    next unless q
    bool = Ferret::Search::BooleanQuery.new
  
 
    (idx, term) = q.split(":")
    term.sub!(/^\"/,'').sub!(/\"$/,'')
    bool.add_query(Ferret::Search::TermQuery.new(idx.to_sym, term), :must)        
 
    unless bool.to_s.empty?
      if @filter
        bool = Ferret::Search::FilteredQuery(bool, @filter)
      end
      query.query = @query
      query.filter = Ferret::Search::QueryFilter.new(bool)
      add_facet_query(query, q)
    end
  end      

end

#to_hashObject



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/cheap_skate/models.rb', line 154

def to_hash
  r = super
  p = r["responseHeader"]["params"]
  p["facets"] = "true"
  p["facet.field"] = @fields
  p["facet.limit"] = @limit
  p["facet.offset"] = @offset
  p["facet.query"] = @query
  @total = 400;
  r["facet_counts"] = {"facet_fields"=>format_facets, "facet_queries"=>[]}
  if @facet_queries
    @facet_queries.each do | fq |
      r["facet_counts"]["facet_queries"] << [fq[:query_string], fq[:results]]
    end
  end
  r      
end