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


161
162
163
164
165
166
# File 'lib/krikri/search_index.rb', line 161

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.



156
157
158
# File 'lib/krikri/search_index.rb', line 156

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



174
175
176
# File 'lib/krikri/search_index.rb', line 174

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



191
192
193
# File 'lib/krikri/search_index.rb', line 191

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



214
215
216
# File 'lib/krikri/search_index.rb', line 214

def commit
  solr.commit
end

#delete_by_id(id) ⇒ Object

Deletes an item from Solr

Parameters:

  • String

    or Array



198
199
200
# File 'lib/krikri/search_index.rb', line 198

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



205
206
207
# File 'lib/krikri/search_index.rb', line 205

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)


230
231
232
233
234
235
236
# File 'lib/krikri/search_index.rb', line 230

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)


222
223
224
# File 'lib/krikri/search_index.rb', line 222

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

#update_from_activity(activity) ⇒ Object



180
181
182
183
184
185
186
# File 'lib/krikri/search_index.rb', line 180

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