Class: S3arch::Searcher

Inherits:
Object
  • Object
show all
Defined in:
lib/s3arch/searcher.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: S3arch.configuration) ⇒ Searcher

Returns a new instance of Searcher.



21
22
23
24
25
26
# File 'lib/s3arch/searcher.rb', line 21

def initialize(config: S3arch.configuration)
  config.validate!
  @config = config
  @dynamodb = Aws::DynamoDB::Client.new
  @s3 = Aws::S3::Client.new
end

Class Attribute Details

.db_cacheObject

Returns the value of attribute db_cache.



13
14
15
# File 'lib/s3arch/searcher.rb', line 13

def db_cache
  @db_cache
end

.version_cacheObject

Returns the value of attribute version_cache.



13
14
15
# File 'lib/s3arch/searcher.rb', line 13

def version_cache
  @version_cache
end

Class Method Details

.reset!Object



15
16
17
18
# File 'lib/s3arch/searcher.rb', line 15

def reset!
  @version_cache = {}
  @db_cache = {}
end

Instance Method Details

#search(query:, owner_ids:, filters: {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/s3arch/searcher.rb', line 28

def search(query:, owner_ids:, filters: {})
  return nil if owner_ids.empty? || query.nil? || query.strip.empty?

  log(:info, 'Search started', query: query, owner_ids: owner_ids, filters: filters)

  results = []
  owner_ids.each do |owner_id|
    db = ensure_database(owner_id)
    unless db
      log(:info, 'No database available', owner_id: owner_id)
      next
    end
    hits = query_fts(db, query, filters: filters)
    log(:info, 'Owner search complete', owner_id: owner_id, hits: hits.size)
    results.concat(hits)
  end

  log(:info, 'Search complete', total_results: results.size)
  return nil if results.empty?

  results.sort_by! { |r| r[:rank] }
  { record_ids: results.first(@config.max_results).map { |r| r[:record_id] }, search_mode: 'fts5' }
end