Skip to content
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

remove_texts added to Elasticsearch #526

Merged
merged 2 commits into from
Mar 17, 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
5 changes: 5 additions & 0 deletions lib/langchain/vectorsearch/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ def update_texts(...)
raise NotImplementedError, "#{self.class.name} does not support updating texts"
end

# Method supported by Vectorsearch DB to delete a list of texts from the index
def remove_texts(...)
raise NotImplementedError, "#{self.class.name} does not support deleting texts"
end

# Method supported by Vectorsearch DB to search for similar texts in the index
def similarity_search(...)
raise NotImplementedError, "#{self.class.name} does not support similarity search"
Expand Down
11 changes: 11 additions & 0 deletions lib/langchain/vectorsearch/elasticsearch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ def update_texts(texts: [], ids: [])
es_client.bulk(body: body)
end

# Remove a list of texts from the index
# @param ids [Array<Integer>] The list of ids to delete
# @return [Elasticsearch::Response] from the Elasticsearch server
def remove_texts(ids: [])
body = ids.map do |id|
{delete: {_index: index_name, _id: id}}
end

es_client.bulk(body: body)
end

# Create the index with the default schema
# @return [Elasticsearch::Response] Index creation
def create_default_schema
Expand Down
6 changes: 6 additions & 0 deletions spec/langchain/vectorsearch/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@
end
end

describe "#remove_texts" do
it "raises an error" do
expect { subject.remove_texts }.to raise_error(NotImplementedError)
end
end

describe "#similarity_search" do
it "raises an error" do
expect { subject.similarity_search }.to raise_error(NotImplementedError)
Expand Down
13 changes: 13 additions & 0 deletions spec/langchain/vectorsearch/elasticsearch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@
end
end

describe "#remove_texts" do
it "removes respective document" do
es_body = [
{delete: {_index: "langchain", _id: 1}}
]

allow_any_instance_of(::Elasticsearch::Client).to receive(:bulk).with(body: es_body)
expect_any_instance_of(::Elasticsearch::Client).to receive(:bulk).with(body: es_body).once

subject.remove_texts(ids: [1])
end
end

describe "#default_vector_settings" do
it "returns default vector settings" do
expect(subject.default_vector_settings).to eq({type: "dense_vector", dims: 384})
Expand Down