Class: Leann::Rails::Index

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/leann/rails/active_record/index.rb

Overview

ActiveRecord model for storing LEANN indexes

Examples:

index = Leann::Rails::Index.find_by(name: "products")
index.search("running shoes")

Instance Method Summary collapse

Instance Method Details

#document_countInteger

Get number of documents

Returns:

  • (Integer)


39
40
41
# File 'lib/leann/rails/active_record/index.rb', line 39

def document_count
  passages.count
end

#embedding_provider_symSymbol

Get embedding provider as symbol

Returns:

  • (Symbol)


45
46
47
# File 'lib/leann/rails/active_record/index.rb', line 45

def embedding_provider_sym
  embedding_provider.to_sym
end

#inspectString

Detailed inspection

Returns:

  • (String)


65
66
67
# File 'lib/leann/rails/active_record/index.rb', line 65

def inspect
  "#<Leann::Rails::Index id=#{id} name=#{name.inspect} documents=#{document_count}>"
end

#search(query, limit: 5, threshold: nil, filters: nil) ⇒ Leann::SearchResults

Search this index

Parameters:

  • query (String)

    Search query

  • limit (Integer) (defaults to: 5)

    Maximum results

  • threshold (Float) (defaults to: nil)

    Minimum similarity score

  • filters (Hash) (defaults to: nil)

    Metadata filters

Returns:



32
33
34
35
# File 'lib/leann/rails/active_record/index.rb', line 32

def search(query, limit: 5, threshold: nil, filters: nil)
  searcher = Searcher.new(self)
  searcher.search(query, limit: limit, threshold: threshold, filters: filters)
end

#to_sString

Index info as string

Returns:

  • (String)


51
52
53
54
55
56
57
58
59
60
61
# File 'lib/leann/rails/active_record/index.rb', line 51

def to_s
  lines = [
    "Index: #{name}",
    "  Documents: #{document_count}",
    "  Embedding: #{embedding_provider}/#{embedding_model}",
    "  Dimensions: #{dimensions}",
    "  Backend: active_record",
    "  Created: #{created_at&.strftime('%Y-%m-%d %H:%M:%S') || 'unknown'}"
  ]
  lines.join("\n")
end