Upgrade Solr support with modified facet data structure.

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@504067 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Erik Hatcher 2007-02-06 10:34:44 +00:00
parent 32280db869
commit 42db0f20fe
3 changed files with 19 additions and 12 deletions

View File

@ -11,6 +11,7 @@
# limitations under the License. # limitations under the License.
class Solr::Response::Standard < Solr::Response::Ruby class Solr::Response::Standard < Solr::Response::Ruby
FacetValue = Struct.new(:name, :value)
include Enumerable include Enumerable
def initialize(ruby_code) def initialize(ruby_code)
@ -36,11 +37,19 @@ class Solr::Response::Standard < Solr::Response::Ruby
end end
def field_facets(field) def field_facets(field)
@data['facet_counts']['facet_fields'][field].sort {|a,b| b[1] <=> a[1]} facets = []
values = @data['facet_counts']['facet_fields'][field]
0.upto(values.size / 2 - 1) do |i|
n = i * 2
facets << FacetValue.new(values[n], values[n+1])
end
facets
end end
# supports enumeration of hits # supports enumeration of hits
# TODO revisit - should this iterate through *all* hits by re-requesting more?
def each def each
@response['docs'].each {|hit| yield hit} @response['docs'].each {|hit| yield hit}
end end

View File

@ -153,23 +153,21 @@ RUBY_CODE
'facet_counts'=>{ 'facet_counts'=>{
'facet_queries'=>{}, 'facet_queries'=>{},
'facet_fields'=>{ 'facet_fields'=>{
'subject_genre_facet'=>{ 'subject_genre_facet'=>[
'Biography.'=>2605, 'Biography.',2605,
'Congresses.'=>1837, 'Congresses.',1837,
'Bibliography.'=>672, 'Bibliography.',672,
'Exhibitions.'=>642, 'Exhibitions.',642,
'Periodicals.'=>615, 'Periodicals.',615,
'Sources.'=>485, 'Sources.',485]}}
}}}
} }
RUBY_CODE RUBY_CODE
set_post_return(ruby_code) set_post_return(ruby_code)
conn = Solr::Connection.new "http://localhost:9999" conn = Solr::Connection.new "http://localhost:9999"
response = conn.query('foo') response = conn.query('foo')
facets = response.field_facets('subject_genre_facet') facets = response.field_facets('subject_genre_facet')
assert_equal 2605, facets[0][1] assert_equal 2605, facets[0].value
assert_equal 485, facets[5][1] assert_equal 485, facets[5].value
end end
end end