Class: OKF::Bundle::Search::Corpus

Inherits:
Object
  • Object
show all
Defined in:
lib/okf/bundle/search.rb

Overview

A corpus prepared once and queried many times: the documents, the key → concept map, and each engine's built index.

This is the asymmetry the engine choice was always argued from. A CLI process loads a bundle, asks one question and exits, so an index build has exactly one query to amortize over and the scan wins. A server is the other case — the build is ~95% of the index path's cost, and paying it per request made every search re-read the whole corpus. Held once, it is paid once.

Pure: it holds concepts, never disk. Which is also the cost — the corpus is a snapshot, so a body edited after it was built is searchable only after the holder drops it. That matches the graph, which is memoized the same way and for the same reason.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bundles) ⇒ Corpus

Returns a new instance of Corpus.



219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/okf/bundle/search.rb', line 219

def initialize(bundles)
  @bundles = bundles
  @documents = []
  @sources = {}
  @indexes = {}
  bundles.each do |slug, bundle|
    bundle.concepts.each do |concept|
      key = "#{slug}#{KEY_SEPARATOR}#{concept.id}"
      @sources[key] = [ slug, concept ]
      @documents << Search.field_texts(concept).merge("key" => key)
    end
  end
end

Instance Attribute Details

#bundlesObject (readonly)

Returns the value of attribute bundles.



217
218
219
# File 'lib/okf/bundle/search.rb', line 217

def bundles
  @bundles
end

#documentsObject (readonly)

Returns the value of attribute documents.



217
218
219
# File 'lib/okf/bundle/search.rb', line 217

def documents
  @documents
end

#sourcesObject (readonly)

Returns the value of attribute sources.



217
218
219
# File 'lib/okf/bundle/search.rb', line 217

def sources
  @sources
end

Instance Method Details

#index_for(engine) ⇒ Object

nil for an engine with nothing to prebuild — the scan reads raw text and has no index to hold — so the option only ever reaches one that declared it can. Memoized per engine id: two engines over one corpus is legal.



236
237
238
239
240
# File 'lib/okf/bundle/search.rb', line 236

def index_for(engine)
  return nil unless engine.respond_to?(:prepare)

  @indexes[engine.id] ||= engine.prepare(@documents)
end