Skip to content

Dynamic fields

CJ edited this page Jan 22, 2016 · 9 revisions

Overview

from the documentation: http://outoftime.github.com/sunspot/docs/classes/Sunspot.html#M000001

Let’s take this problem : "We have a category named ‘Fruit’ with values of ‘Banana’, ‘Apple’, ‘Orange’, and ‘Grape’.
On the search results page, ‘Fruit’ should be displayed as a facet category with
counts for matches for ‘Banana’, ‘Apple’, ‘Orange’, and ‘Grape’ on any text field of the document. "

class MyClass 
  searchable do 
    dynamic_integer :custom_category_ids, :multiple => true do 
      custom_categories.inject(Hash.new { |h, k| h[k] = [] }) do |map, custom_category| 
        map[custom_category.name] << custom_category_values_for(custom_category)
        map
      end 
    end 
  end 
end 
search = MyClass.search do 
  dynamic(:custom_categories) do 
    facet(some_custom_category.id) 
  end 
end 
facet = search.facet(:custom_categories, some_custom_category.name)

Do not use “integer id key” as it doesn’t work well with exclude you will get an error no adapter configured for fix null

Excerpted from this conversation

Sunspot modifies field names in a regular way while indexing and searching from ruby hash-parameters.

Were you to query a Sunspot-indexed record using a lower-level library like RSolr, you might see:

rsolr.select(:q=>"*:*")['response']['docs'].first.keys
# => ["updated_at_ds", "highest_score_is", "title_texts", "id"]

Notes

  • you can’t define a dynamic_text field. Hence, it is not possible to do a fulltext search on dynamic fields.
  • id is magic… solr needs
  • do not change primary key field

How to

  • allowing OPTION of non-dynamic indexing by indexing through request_handlers in solrconfig.xml
Something went wrong with that request. Please try again.