Class: WeaviateRecord::Relation

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable, Queries::Ask, Queries::Bm25, Queries::Count, Queries::Limit, Queries::NearObject, Queries::NearText, Queries::NearVector, Queries::Offset, Queries::Order, Queries::Select, Queries::Where, QueryBuilder
Defined in:
lib/weaviate_record/relation.rb,
lib/weaviate_record/relation/query_builder.rb

Overview

This class is used to build weaviate queries

Defined Under Namespace

Modules: QueryBuilder

Instance Method Summary collapse

Methods included from QueryBuilder

#to_query

Methods included from Queries::Ask

#ask

Methods included from Queries::Where

to_ruby_hash, #where

Methods included from Queries::Select

#select

Methods included from Queries::Order

#order

Methods included from Queries::Offset

#offset

Methods included from Queries::NearObject

#near_object

Methods included from Queries::NearVector

#near_vector

Methods included from Queries::NearText

#near_text

Methods included from Queries::Limit

#limit

Methods included from Queries::Count

#count

Methods included from Queries::Bm25

#bm25

Constructor Details

#initialize(klass) ⇒ Relation

:stopdoc:



26
27
28
29
30
31
32
33
34
35
# File 'lib/weaviate_record/relation.rb', line 26

def initialize(klass)
  @select_options = { attributes: [], nested_attributes: {} }
  @near_text_options = { concepts: [], distance: WeaviateRecord.config.similarity_search_threshold }
  @limit = ENV['QUERY_DEFAULTS_LIMIT'] || 25
  @offset = 0
  @klass = klass
  @records = []
  @loaded = false
  @connection = WeaviateRecord::Connection.new(@klass)
end

Instance Method Details

#allObject Also known as: inspect, to_a

Gets all the records from Weaviate matching the given conditions or search filters given in the query. This will return an array of WeaviateRecord objects.



45
46
47
48
49
# File 'lib/weaviate_record/relation.rb', line 45

def all
  records
rescue StandardError => e
  e
end

#destroy_allObject

Deletes all the records from Weaviate matching the given conditions or search filters given in the query. This will return the result of batch delete operation given by Weaviate.

Example:

Article.where(title: nil).destroy_all
# => {"failed"=>0, "limit"=>10000, "matches"=>3, "objects"=>nil, "successful"=>3}


57
58
59
60
61
62
63
64
65
66
67
# File 'lib/weaviate_record/relation.rb', line 57

def destroy_all
  unless @where_query
    raise WeaviateRecord::Errors::MissingWhereCondition, 'must specifiy atleast one where condition'
  end

  response = @connection.delete_where(Queries::Where.to_ruby_hash(@where_query))
  return response['results'] if response.is_a?(Hash) && response.key?('results')

  raise WeaviateRecord::Errors::ServerError,
        response == '' ? 'Unauthorized' : response.dig('error', 'message').presence
end

#each(&block) ⇒ Object

To enumerate over each record in the Weaviate relation



39
40
41
# File 'lib/weaviate_record/relation.rb', line 39

def each(&block)
  records.each(&block)
end