Domanda

I am struggling with a little problem where I have to display relevant information about the resultset returned from SolR but can't figure out how to calculate it without iterating the results (bad).

Basically I am storing my documents with a state field and while the search is supposed to return all documents, the UI has to show "Found 15 entities, 5 are in state A, 3 in state B and 8 in C".

At the moment I am using a rather brittle approach of running the query 3 times with additional scoping by type, but I'd rather get that information from the one query I am displaying. (There have been some edge cases where the numbers don't add up and since SolR can return facets I guess there has to be a way to use that functionality in this case)

I am using SolR 3.5 from Rails with the sunspot gem

È stato utile?

Soluzione

As you mention yourself, you can use facets for this by setting

facet=true&facet.field=state

I'm not familiar with the sunspot gem, but by looking at the documentation you can use facets like this(Assuming Entity is your searchable):

Entity.search do:
  facet :state
end

This should return the states of all entities returned by your query with the number of entities in this state. The Sunspot documentation tells me you can read these facets in the following way:

search.facet(:state).rows.each do |facet|
  puts "State #{facet.value} has #{facet.count} entities"
end

Altri suggerimenti

Essentially there are three main sets of functions you can use to garner stats from solr.

The first is faceting:
http://wiki.apache.org/solr/SimpleFacetParameters

There is also grouping (field collapsing):
https://wiki.apache.org/solr/FieldCollapsing

And the stats package:
https://cwiki.apache.org/confluence/display/solr/The+Stats+Component

Although the stats, facet and group may be replaced by the analytic package known as olap which is aimed to be in solr V 5.0.0:
https://issues.apache.org/jira/browse/SOLR-5302

Good luck.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top