Skip to content

Add object.replace method #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
## [Unreleased]

## [0.9.0] - 2024-07-08

- Add object.replace method which uses PUT which performs a complete object replacement

### Breaking
- Change the object.update method to use PATCH which only performs a partial update(previously performed a replacement)

## [0.8.11] - 2024-07-02
- Allow the user to specify any options they want for multi-tenancy when creating a schema using their own hash
- Allow Ollama vectorizer

## [0.8.10] - 2024-01-25

Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
weaviate-ruby (0.8.11)
weaviate-ruby (0.9.0)
faraday (>= 2.0.1, < 3.0)
graphlient (~> 0.7.0)

Expand Down
19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,17 @@ client.objects.exists?(
id: "uuid"
)

# Delete a single data object from Weaviate.
client.objects.delete(
# Perform a partial update on an object based on its uuid.
client.objects.update(
class_name: "Question",
id: "uuid"
id: "uuid",
properties: {
category: "simple-math"
}
)

# Update a single data object based on its uuid.
client.objects.update(
# Replace an object based on its uuid.
client.objects.replace(
class_name: "Question",
id: "uuid",
properties: {
Expand All @@ -185,6 +188,12 @@ client.objects.update(
}
)

# Delete a single data object from Weaviate.
client.objects.delete(
class_name: "Question",
id: "uuid"
)

# Batch create objects
client.objects.batch_create(objects: [
{
Expand Down
25 changes: 25 additions & 0 deletions lib/weaviate/objects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,31 @@ def update(
)
validate_consistency_level!(consistency_level) unless consistency_level.nil?

response = client.connection.patch("#{PATH}/#{class_name}/#{id}") do |req|
req.params["consistency_level"] = consistency_level.to_s.upcase unless consistency_level.nil?

req.body = {}
req.body["id"] = id
req.body["class"] = class_name
req.body["properties"] = properties
req.body["vector"] = vector unless vector.nil?
req.body["tenant"] = tenant unless tenant.nil?
end

response.body
end

# Replace an individual data object based on its uuid.
def replace(
class_name:,
id:,
properties:,
vector: nil,
tenant: nil,
consistency_level: nil
)
validate_consistency_level!(consistency_level) unless consistency_level.nil?

response = client.connection.put("#{PATH}/#{class_name}/#{id}") do |req|
req.params["consistency_level"] = consistency_level.to_s.upcase unless consistency_level.nil?

Expand Down
2 changes: 1 addition & 1 deletion lib/weaviate/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Weaviate
VERSION = "0.8.11"
VERSION = "0.9.0"
end
25 changes: 24 additions & 1 deletion spec/weaviate/objects_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
let(:response) { OpenStruct.new(success?: true, body: object_fixture) }

before do
allow_any_instance_of(Faraday::Connection).to receive(:put)
allow_any_instance_of(Faraday::Connection).to receive(:patch)
.with("objects/Question/123")
.and_return(response)
end
Expand All @@ -151,6 +151,29 @@
end
end

describe "#replace" do
let(:response) { OpenStruct.new(success?: true, body: object_fixture) }

before do
allow_any_instance_of(Faraday::Connection).to receive(:put)
.with("objects/Question/123")
.and_return(response)
end

it "returns the schema" do
response = objects.replace(
class_name: "Question",
id: "123",
properties: {
question: "What does 6 times 7 equal to?",
category: "math",
answer: "42"
}
)
expect(response.dig("class")).to eq("Question")
end
end

describe "#batch_create" do
let(:response) { OpenStruct.new(success?: true, body: objects_fixture) }

Expand Down