Class: Krikri::QASearchIndex

Inherits:
SearchIndex show all
Defined in:
lib/krikri/search_index.rb

Overview

Generates flattened Solr documents and manages indexing of DPLA MAP models.

Examples:


indexer = Krikri::QASearchIndex.new
agg = Krikri::Aggregation.new
doc = agg.to_jsonld['@graph'].first

indexer.add(doc)
indexer.commit

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ QASearchIndex

Returns a new instance of QASearchIndex.

Parameters:

  • opts (Hash) (defaults to: {})

    options to pass to RSolr

See Also:

  • RSolr.connect


157
158
159
160
161
162
# File 'lib/krikri/search_index.rb', line 157

def initialize(opts = {})
  # Override or append to default Solr options
  solr_opts = Krikri::Settings.solr.to_h.merge(opts)
  @solr = RSolr.connect(solr_opts)
  super(opts)
end

Instance Attribute Details

#solrObject (readonly)

Returns the value of attribute solr.



152
153
154
# File 'lib/krikri/search_index.rb', line 152

def solr
  @solr
end

Instance Method Details

#add(doc) ⇒ Object

Adds a single JSON document to Solr

Parameters:

  • doc (Hash)

    A hash that complies with the Solr schema



170
171
172
# File 'lib/krikri/search_index.rb', line 170

def add(doc)
  solr.add solr_doc(doc)
end

#bulk_add(docs) ⇒ Object

Add multiple documents to Solr

Parameters:

  • docs (Array)

    Array of hashes that comply with the Solr schema



187
188
189
# File 'lib/krikri/search_index.rb', line 187

def bulk_add(docs)
  solr.add(docs.map { |d| solr_doc(d) })
end

#commitObject

Commits changes to Solr, making them visible to new requests Should be run after self.add and self.delete Okay to add or delete multiple docs and commit them all with a single self.commit



210
211
212
# File 'lib/krikri/search_index.rb', line 210

def commit
  solr.commit
end

#delete_by_id(id) ⇒ Object

Deletes an item from Solr

Parameters:

  • String

    or Array



194
195
196
# File 'lib/krikri/search_index.rb', line 194

def delete_by_id(id)
  solr.delete_by_id id
end

#delete_by_query(query) ⇒ Object

Deletes items from Solr that match query

Parameters:

  • String

    or Array



201
202
203
# File 'lib/krikri/search_index.rb', line 201

def delete_by_query(query)
  solr.delete_by_query query
end

#schema_keysArray

Get field names from Solr schema in host application. Will raise exception if file not found.

Returns:

  • (Array)


226
227
228
229
230
231
232
# File 'lib/krikri/search_index.rb', line 226

def schema_keys
  schema_file = File.join(Rails.root, 'solr_conf', 'schema.xml')
  file = File.open(schema_file)
  doc = Nokogiri::XML(file)
  file.close
  doc.xpath('//fields/field').map { |f| f.attr('name') }
end

#solr_doc(doc) ⇒ Hash

Converts JSON document into a Hash that complies with Solr schema

Parameters:

  • (JSON)

Returns:

  • (Hash)


218
219
220
# File 'lib/krikri/search_index.rb', line 218

def solr_doc(doc)
  remove_invalid_keys(flat_hash(doc))
end

#update_from_activity(activity) ⇒ Object



176
177
178
179
180
181
182
# File 'lib/krikri/search_index.rb', line 176

def update_from_activity(activity)
  fail "#{activity} is not an Activity" unless 
    activity.class == Krikri::Activity
  result = bulk_update_from_activity(activity)
  solr.commit
  result
end