Search code examples
lucenesolrsunspotsunspot-rails

Solr Sunspot non-indexed field


Solr (via Lucene) supports different ways to indicate the way a field is indexed in a document: indexed, tokenized, stored,...

I'm looking for a way to have fields that are stored in Solr but are not indexed. Is there a way to achieve that in Sunspot?


Solution

  • Sunspot's configuration DSL supports an option of :stored => true for many of its default types. For the example of the stored string, it would be much simpler than my first example:

    searchable do
      string :name, :stored => true
    end
    

    This generates a field name of name_ss corresponding to the following dynamicField already present in Sunspot's standard schema:

    <dynamicField name="*_ss" stored="true" type="string" multiValued="false" indexed="true"/>
    

    You can also create your own custom field or dynamicField in your schema.xml to be stored but not indexed, and then use the Sunspot 1.2 :as option to specify a corresponding field name.

    For example, a more verbose version of the above. In your schema:

    <dynamicField name="*_stored_string" type="string" indexed="false" stored="true" />
    

    And in your model:

    searchable do
      string :name, :as => 'name_stored_string'
    end