Module: OKF::Bundle::Search::Index

Defined in:
lib/okf/bundle/search/index.rb

Overview

The default engine: a MiniFTS full-text index — the same engine, and the same BM25+ arithmetic, the browser page already runs as MiniSearch, so a Ruby-built index and the page's rank identically.

Matching is by token: a term matches a whole word or a word it prefixes ("dedup" reaches "deduplication"), and fuzzy: opts into typo tolerance. The index is built per call — see .okf/capabilities/search.md for why that ceiling stands and what lifts it.

Constant Summary collapse

CAPABILITIES =
%i[fuzzy prefix].freeze

Class Method Summary collapse

Class Method Details

.available?Boolean

minifts is a hard runtime dependency with no native extension, so it is here whenever the gem is. An addon backed by a native build is the case this predicate exists for.

Returns:

  • (Boolean)


32
33
34
# File 'lib/okf/bundle/search/index.rb', line 32

def available?
  true
end

.call(documents, terms, fields:, fuzzy: false, **_options) ⇒ Object

fields: narrows where a term may hit, so a field the caller excluded can neither match nor be credited. The hit's terms are MiniFTS's matched document terms — already lowercased, and present in the text verbatim even when the query only prefixed them.



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/okf/bundle/search/index.rb', line 40

def call(documents, terms, fields:, fuzzy: false, **_options)
  index = MiniFTS.new(fields: FIELDS, id_field: "key")
  index.add_all(documents)

  options = { combine_with: "AND", prefix: true, boost: WEIGHTS, fields: fields }
  options[:fuzzy] = FUZZY_DISTANCE if fuzzy

  index.search(terms.join(" "), options).map do |hit|
    { key: hit[:id], matched: matched_in(hit), score: hit[:score], terms: hit[:terms] }
  end
end

.capabilitiesObject



25
26
27
# File 'lib/okf/bundle/search/index.rb', line 25

def capabilities
  CAPABILITIES
end

.idObject



21
22
23
# File 'lib/okf/bundle/search/index.rb', line 21

def id
  :index
end