rsolr-ext
rsolr-ext copied to clipboard
Facet items not working properly
Using Rails 2.3.2, Ruby 1.8.7, windows Vista, Solr 1.4
Playing with rsolr ext and have a query like this:
solr = RSolr.connect :url => 'http://localhost:8982/solr'
solr_params = { :per_page=>2, :queries=>':', :facets=>{:fields=>['project_id_facet', 'source_type_facet']}, :echoParams => 'EXPLICIT' }
@response = solr.find solr_params
In my view I have the code to output the facet values and counts:
<% @response.facets.each do |facet| %>
<%= facet.name %>
<% facet.items.each do |item| %> <%= "#{facet.name}::#{item.value} (#{item.hits})" %> <% end %>
<% end %>
With the stock code I get a result like: source_type_facet source_type_facet:: () source_type_facet:: () source_type_facet:: () source_type_facet:: () source_type_facet:: () project_id_facet project_id_facet:: ()
The problem is in the facets method in RSolr::Ext::Response::Facets.
If I change this code: (values_and_hits.size/2).times do |index|
items << FacetItem.new(values_and_hits[index_2], values_and_hits[index_2+1])
end
to:
values_and_hits.each do |key, value|
items << FacetItem.new(key, value)
end
I get the proper results: source_type_facet source_type_facet::ticket (4324) source_type_facet::commit (1092) source_type_facet::portal (34) source_type_facet::native (43) source_type_facet::build (211) source_type_facet::timesheet (7) source_type_facet::wiki (1127) source_type_facet::code_review (714) source_type_facet::general (266) source_type_facet::email (189) project_id_facet project_id_facet::1 (4637) project_id_facet::2 (1192) project_id_facet::3 (2178)
Anyone else see a similar issue?
If I inspect the value_and_hits variable its a simple hash like this: {"1"=>4637, "2"=>1192, "3"=>2178}
So, I can't see how the original code can work at all.
I figured out what the problem is. I have the following in the requestHandler section of my solrconfig.xml:
<str name="json.nl">map</str>
This forces json (and Ruby because it extends the json output) format to use maps instead of flat arrays. Personally I like the map format better but if rsolr-ext doesn't support it then it should be called out in the docs somewhere (if it isn't already)
Hi. The reason the arrays are supported and not hashes, is because the hashes are not guaranteed to be ordered by hit value. The return array from Solr might be, but Ruby < 1.9 will immediately mess the ordering up.