module DatastaxRails::FacetMethods

Relation methods related to computing Solr facets. Results are available in the facets accessor. Facets include Field and Range (Date is not supported as it is depricated in Solr - use a range instead).

results = Article.field_facet(:author)
results.facets => {"author"=>["vonnegut", 2. "asimov", 3]}

Model.field_facet(:author)
Model.field_facet(:author, :sort => 'count', :limit => 10, :mincount => 1)
Model.range_facet(:price, 500, 1000, 10)
Model.range_facet(:price, 500, 1000, 10, :include => 'all')
Model.range_facet(:publication_date, "1968-01-01T00:00:00Z", "2000-01-01T00:00:00Z", "+1YEAR")

Range Gap syntax for dates: +1YEAR, +5YEAR, +5YEARS, +1MONTH, +1DAY

Useful constants:

DatastaxRails::FacetMethods::BY_YEAR  (+1YEAR)
DatastaxRails::FacetMethods::BY_MONTH (+1MONTH)
DatastaxRails::FacetMethods::BY_DAY   (+1DAY)

Model.range_facet(:publication_date,
                  "1968-01-01T00:00:00Z",
                  "2000-01-01T00:00:00Z",
                  DatastaxRails::FacetMethods::BY_YEAR)

These method can be called multiple times to facet on different fields

@param field [String, Symbol] the field to get facet counts for @param options [Hash] the facet options @return [DatastaxRails::Relation] a new Relation object

Field Facet Option Values:

Note: You can pass any valid facet option value, and it will be passed to Solr

Range Facet Option Values:

include / exclude upper and lower range values

Note: You can pass any valid facet option value, and it will be passed to Solr

Date Facet? - use Range Facet! They were depricated in Solr 3

Constants

BY_DAY
BY_MONTH
BY_YEAR

Public Instance Methods

facet_threads(thread_count = 0) click to toggle source

New option as of SOLR 4.5 limited to field_facets

# File lib/datastax_rails/relation/facet_methods.rb, line 80
def facet_threads(thread_count = 0)
  # thread_count == -1 will create a THREAD per field_facet up to Integer.MAX_VALUE
  thread_count = -1 if thread_count < 0

  clone.tap do |r|
    r.facet_threads_value = thread_count
  end
end
field_facet(field, options = {}) click to toggle source
# File lib/datastax_rails/relation/facet_methods.rb, line 59
def field_facet(field, options = {})
  return self if field.blank?
  clone.tap do |r|
    r.field_facet_values << { field: field.to_s, options: options }
  end
end
range_facet(field, start_range, end_range, gap, options = {}) click to toggle source
# File lib/datastax_rails/relation/facet_methods.rb, line 66
def range_facet(field, start_range, end_range, gap, options = {})
  return self if field.blank?
  clone.tap do |r|
    r.range_facet_values << { field:   field.to_s,
                              options: options.merge(start: start_range.to_s,
                                                     end:   end_range.to_s,
                                                     gap:   gap.to_s
                                                    )
                            }
  end
end