Module: WeaviateRecord::Queries::Where

Included in:
Relation
Defined in:
lib/weaviate_record/queries/where.rb

Overview

This module contains function to perform where query on Weaviate

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.to_ruby_hash(string_condition) ⇒ Object

:enddoc:



29
30
31
32
33
34
35
36
# File 'lib/weaviate_record/queries/where.rb', line 29

def self.to_ruby_hash(string_condition)
  pattern = /(?<=\s)\w+:|(?<=operator:\s)\w+/
  keys_and_operator = string_condition.scan(pattern).uniq
  json_equivalent = keys_and_operator.map { |i| i.end_with?(':') ? "#{i[0...-1].inspect}:" : i.inspect }
  JSON.parse string_condition.gsub(pattern, keys_and_operator.zip(json_equivalent).to_h)
rescue StandardError
  raise WeaviateRecord::Errors::WhereQueryConversionError, 'invalid where query format'
end

Instance Method Details

#where(query = '', *values, **kw_args) ⇒ Object

Perform a where query on the collection. You can pass a string or keyword arguments as a query. It follows the same syntax as ActiveRecord where query. Chaining of where queries is also supported.

Example:

Article.where('title = ?', 'Hello World')
Article.where(title: 'Hello World')
Article.where('title = ? AND content = ?', 'Hello World', 'This is a content')
Article.where(title: 'Hello World').where(content: 'This is a content')


17
18
19
20
21
22
23
24
25
# File 'lib/weaviate_record/queries/where.rb', line 17

def where(query = '', *values, **kw_args)
  validate_arguments(query, values, kw_args)
  keyword_query = process_keyword_conditions(kw_args)
  string_query = process_string_conditions(query, *values)
  combined_query = combine_queries(keyword_query, string_query)
  @where_query = @where_query ? create_logical_condition(@where_query, 'And', combined_query) : combined_query
  @loaded = false
  self
end