mwmitchell-rsolr 0.6.8 → 0.6.9

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES.txt CHANGED
@@ -1,3 +1,15 @@
1
+ 0.6.9 - January 29, 2009
2
+ Simplified facet response methods
3
+ Main facet method is called #facets
4
+ - returns an array of Response::Facet instances
5
+ - each Facet instance has field and values attributes
6
+ -- the values attribute is an array with FacetValue instances which have @value and @hits
7
+ Added ability to set Connection::Base @param_adapters using :dismax or :standard
8
+ instead of full class constant
9
+ updated comments for #search method
10
+ Updated tests
11
+ Bumped up version
12
+
1
13
  0.6.8 - January 28, 2009
2
14
  New method added to RSolr::Connection::Base - #find_values_for_facet
3
15
  This method searches for facet values only, and sets the :rows param to 0
@@ -45,10 +45,26 @@ class RSolr::Connection::Base
45
45
  p[:wt]==:ruby ? RSolr::Response::Query::Base.new(response) : response
46
46
  end
47
47
 
48
- # register your own mapper if you want?
48
+ # The #search method uses a param mapper to prepare the request for solr.
49
+ # For example, instead of doing your fq params by hand,
50
+ # you can use the simplified :filters param instead.
51
+ # The 2 built in mappers are for dismax and standard: RSolr::Connection::ParamMapping::*
52
+ # The default is :dismax
53
+ # If you create your own request handler in solrconfig.xml,
54
+ # you can use it by setting the :qt=>:my_handler
55
+ # You'll need to set the correct param mapper class (when using the search method)
56
+ # To take advantage of the param mapping
57
+ # If your request handler uses the solr dismax class, then do nothing
58
+ # if it uses the standard, you'll need to set it like:
59
+ # solr.param_mappers[:my_search_handler] = :standard
60
+ # The value can also be a custom class constant that must have a #map method
61
+ # The initialize method must accept a hash of input params
62
+ # The #map method must handle a block being passed in and return a new hash of raw solr params
49
63
  def search(params,&blk)
50
64
  qt = params[:qt] ? params[:qt].to_sym : :dismax
51
65
  mapper_class = @param_mappers[qt]
66
+ mapper_class = RSolr::Connection::ParamMapping::Dismax if mapper_class==:dismax
67
+ mapper_class = RSolr::Connection::ParamMapping::Standard if mapper_class==:standard
52
68
  mapper = mapper_class.new(params)
53
69
  query(mapper.map(&blk))
54
70
  end
@@ -43,70 +43,59 @@ module RSolr::Response::Query
43
43
  # from the delsolr project -> http://github.com/avvo/delsolr/tree/master/lib/delsolr/response.rb
44
44
  module Facets
45
45
 
46
- def facets
47
- @facets ||= data['facet_counts'] || {}
48
- end
49
-
50
- # Returns the hash of all the facet_fields (ie: {'instock_b' => ['true', 123, 'false', 20]}
51
- def facet_fields
52
- @facet_fields ||= facets['facet_fields'] || {}
46
+ class FacetValue
47
+ attr_reader :value,:hits
48
+ def initialize(value,hits)
49
+ @value,@hits=value,hits
50
+ end
53
51
  end
54
-
55
- # Returns all of the facet queries
56
- def facet_queries
57
- @facet_queries ||= facets['facet_queries'] || {}
52
+
53
+ class Facet
54
+ attr_reader :field
55
+ attr_accessor :values
56
+ def initialize(field)
57
+ @field=field
58
+ @values=[]
59
+ end
58
60
  end
59
-
60
- # Returns a hash of hashs rather than a hash of arrays (ie: {'instock_b' => {'true' => 123', 'false', => 20} })
61
- def facet_fields_by_hash
62
- @facet_fields_by_hash ||= begin
63
- f = {}
64
- if facet_fields
65
- facet_fields.each do |field,value_and_counts|
66
- f[field] = {}
67
- value_and_counts.each_with_index do |v, i|
68
- if i % 2 == 0
69
- f[field][v] = value_and_counts[i+1]
70
- end
71
- end
61
+
62
+ # @response.facet_fields.each do |facet|
63
+ # facet.field
64
+ # end
65
+ # "caches" the result in the @facets instance var
66
+ def facets
67
+ @facets ||= (
68
+ facet_fields.inject([]) do |acc,(facet_field_name,values_and_hits_list)|
69
+ acc << facet = Facet.new(facet_field_name)
70
+ # the values_and_hits_list is an array where a value is immediately followed by it's hit count
71
+ # so we shift off an item (the value)
72
+ while value = values_and_hits_list.shift
73
+ # and then shift off the next to get the hit value
74
+ facet.values << FacetValue.new(value, values_and_hits_list.shift)
75
+ # repeat until there are no more pairs in the values_and_hits_list array
72
76
  end
77
+ acc
73
78
  end
74
- f
75
- end
79
+ )
76
80
  end
77
-
78
- # Returns an array of value/counts for a given field (ie: ['true', 123, 'false', 20]
79
- def facet_field(field)
80
- facet_fields[field.to_s]
81
- end
82
-
83
- # Returns the array of field values for the given field in the order they were returned from solr
84
- def facet_field_values(field)
85
- facet_field_values ||= {}
86
- facet_field_values[field.to_s] ||= begin
87
- a = []
88
- return unless facet_field(field)
89
- facet_field(field).each_with_index do |val_or_count, i|
90
- a << val_or_count if i % 2 == 0 && facet_field(field)[i+1] > 0
91
- end
92
- a
93
- end
81
+
82
+ # pass in a facet field name and get back a Facet instance
83
+ def facet_by_field_name(name)
84
+ facets.detect{|facet|facet.field.to_s == name.to_s}
94
85
  end
95
-
96
- # Returns a hash of value/counts for a given field (ie: {'true' => 123, 'false' => 20}
97
- def facet_field_by_hash(field)
98
- facet_fields_by_hash[field.to_s]
86
+
87
+ def facet_counts
88
+ @facets ||= data['facet_counts'] || {}
99
89
  end
100
-
101
- # Returns the count for the given field/value pair
102
- def facet_field_count(field, value)
103
- facet_fields_by_hash[field.to_s][value.to_s] if facet_fields_by_hash[field.to_s]
90
+
91
+ # Returns the hash of all the facet_fields (ie: {'instock_b' => ['true', 123, 'false', 20]}
92
+ def facet_fields
93
+ @facet_fields ||= facet_counts['facet_fields'] || {}
104
94
  end
105
-
106
- # Returns the counts for a given facet_query_name
107
- def facet_query_count_by_name(facet_query_name)
108
- query_string = query_builder.facet_query_by_name(facet_query_name)
109
- facet_queries[query_string] if query_string
95
+
96
+ # Returns all of the facet queries
97
+ def facet_queries
98
+ @facet_queries ||= facet_counts['facet_queries'] || {}
110
99
  end
111
100
 
112
101
  end
data/lib/rsolr.rb CHANGED
@@ -7,7 +7,7 @@ proc {|base, files|
7
7
 
8
8
  module RSolr
9
9
 
10
- VERSION = '0.6.8'
10
+ VERSION = '0.6.9'
11
11
 
12
12
  autoload :Message, 'rsolr/message'
13
13
  autoload :Response, 'rsolr/response'
@@ -119,7 +119,17 @@ module ConnectionTestMethods
119
119
  def test_search_facet_by_name
120
120
  @solr.add([{:id=>1, :cat=>'eletronics'}, {:id=>2, :cat=>'software'}]) and @solr.commit
121
121
  response = @solr.search_facet_by_name('cat', {:q=>'*:*'})
122
- assert_equal 2, response.facet_field_values(:cat).size
122
+
123
+ response.facets.each do |facet|
124
+ puts facet.field
125
+ puts facet.values.inspect
126
+ facet.values.each do |value|
127
+ puts value.value
128
+ puts value.hits
129
+ end
130
+ end
131
+
132
+ assert_equal 2, response.facet_by_field_name(:cat).values.size
123
133
  #
124
134
  response = @solr.search_facet_by_name('cat', {:q=>'*:*'})
125
135
  assert_equal 0, response.docs.size
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mwmitchell-rsolr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.8
4
+ version: 0.6.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Mitchell