Skip to content

Commit

Permalink
remove_texts added to Elasticsearch (#526)
Browse files Browse the repository at this point in the history
* Added remove_texts on base vectorsearch

* Added remove_texts on elasticsearch vectorsearch

---------

Co-authored-by: Gregorio Galante <[email protected]>
  • Loading branch information
andreibondarev and gregogalante authored Mar 17, 2024
1 parent 0d3d405 commit 4edbc42
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
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

0 comments on commit 4edbc42

Please sign in to comment.