Class: Multiwoven::Integrations::Destination::Weaviate::Client

Inherits:
DestinationConnector
  • Object
show all
Defined in:
lib/multiwoven/integrations/destination/weaviate/client.rb

Constant Summary collapse

WEAVIATE_TYPE_MAP =
{
  "text" => "string",
  "int" => "integer",
  "number" => "number",
  "boolean" => "boolean",
  "date" => "string"
}.freeze

Instance Method Summary collapse

Instance Method Details

#check_connection(connection_config) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/multiwoven/integrations/destination/weaviate/client.rb', line 16

def check_connection(connection_config)
  client = build_client(connection_config)
  client.schema.list
  success_status
rescue StandardError => e
  handle_exception(e, {
                     context: "WEAVIATE:CHECK_CONNECTION:EXCEPTION",
                     type: "error"
                   })
  failure_status(e)
end

#discover(connection_config) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/multiwoven/integrations/destination/weaviate/client.rb', line 28

def discover(connection_config)
  client = build_client(connection_config)
  schema = client.schema.list
  classes = schema["classes"] || []
  streams = classes.map { |class_data| build_stream(class_data) }
  catalog = build_catalog(streams)
  catalog.to_multiwoven_message
rescue StandardError => e
  handle_exception(e, {
                     context: "WEAVIATE:DISCOVER:EXCEPTION",
                     type: "error"
                   })
end

#write(sync_config, records, _action = "destination_insert") ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/multiwoven/integrations/destination/weaviate/client.rb', line 42

def write(sync_config, records, _action = "destination_insert")
  write_success = 0
  write_failure = 0
  log_message_array = []
  # Passing in the id handles upsert
  objects = records.map do |record|
    {
      class: sync_config.stream.name,
      vector: record["vector"],
      id: record["id"].present? ? generate_uuid(record["id"]) : SecureRandom.uuid,
      properties: coerce_properties(record["properties"], sync_config.stream)
    }
  end

  client = build_client(sync_config.destination.connection_specification)
  responses = client.objects.batch_create(objects: objects)
  responses.each do |response|
    if response["result"]["status"] == "SUCCESS"
      write_success += 1
      log_message_array << log_request_response("info", { object: response }, response)
    else
      write_failure += 1
      log_message_array << log_request_response("error", { object: response }, response)
    end
  end
  tracking_message(write_success, write_failure, log_message_array)
rescue StandardError => e
  handle_exception(e, {
                     context: "WEAVIATE:RECORD:WRITE:EXCEPTION",
                     type: "error",
                     sync_id: sync_config.sync_id,
                     sync_run_id: sync_config.sync_run_id
                   })
end