Skip to content
This repository has been archived by the owner on Jun 27, 2020. It is now read-only.

ActiveFedora Solr Cheatsheet

Jim Coble edited this page Jun 5, 2013 · 4 revisions

Solr Field Names

How to avoid hard-coding Solr field names.

> ActiveFedora::SolrService.solr_name(:field, type: :string)
 => "field_tesim" 
> ActiveFedora::SolrService.solr_name(:field, type: :text)
 => "field_tesim" 
> ActiveFedora::SolrService.solr_name(:field, type: :symbol)
 => "field_ssim" 
> ActiveFedora::SolrService.solr_name(:field, type: :date)
 => "field_dtsim" 
> ActiveFedora::SolrService.solr_name(:field, :sortable, type: :date)
 => "field_dti" 

Queries

To find results matching a given query: ActiveFedora::SolrService.query(query, :rows => limit, :sort => SORT)

Example query: last_fixity_check_on_dt:[* TO NOW-%s] AND (active_fedora_model_s:Component OR active_fedora_model_s:Target)

Example sort: last_fixity_check_on_dt asc

Example query for empty field: -is_governed_by_s:["" TO *]

Result of SolrService.query is an array. Iterate through this and create a Solr document for each hit: doc = SolrDocument.new(r)

To retrieve the ActiveFedora object for the object represented by the Solr document: ActiveFedora::Base.find(doc.id, :cast => true) (Not sure if need :cast parameter)

Finding and Fixing a descMetadata Field Value

The following commands were used to find and fix a situation where the 'creator' attribute was not set on a group of Components as they were ingested:

tbf = ActiveFedora::SolrService.query("-creator_tesim:['' TO *] AND active_fedora_model_ssi:Component", :rows=>999)
tbf.each do |r|
  doc = SolrDocument.new(r)
  c = ActiveFedora::Base.find(doc.id, :cast => true)
  c.creator = 'DPC'
  c.save
end
Clone this wiki locally