mwmitchell-rsolr 0.6.8 → 0.6.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGES.txt +12 -0
- data/lib/rsolr/connection/base.rb +17 -1
- data/lib/rsolr/response/query.rb +45 -56
- data/lib/rsolr.rb +1 -1
- data/test/connection/test_methods.rb +11 -1
- metadata +1 -1
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
|
-
#
|
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
|
data/lib/rsolr/response/query.rb
CHANGED
@@ -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
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
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
|
-
|
56
|
-
|
57
|
-
|
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
|
-
#
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
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
|
-
|
75
|
-
end
|
79
|
+
)
|
76
80
|
end
|
77
|
-
|
78
|
-
#
|
79
|
-
def
|
80
|
-
|
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
|
-
|
97
|
-
|
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
|
102
|
-
def
|
103
|
-
|
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
|
107
|
-
def
|
108
|
-
|
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
@@ -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
|
-
|
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
|