diff --git a/lib/langchain/vectorsearch/base.rb b/lib/langchain/vectorsearch/base.rb index 7574e7730..2fe205e89 100644 --- a/lib/langchain/vectorsearch/base.rb +++ b/lib/langchain/vectorsearch/base.rb @@ -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" diff --git a/lib/langchain/vectorsearch/elasticsearch.rb b/lib/langchain/vectorsearch/elasticsearch.rb index bf35a4367..2297c3fae 100644 --- a/lib/langchain/vectorsearch/elasticsearch.rb +++ b/lib/langchain/vectorsearch/elasticsearch.rb @@ -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] 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 diff --git a/spec/langchain/vectorsearch/base_spec.rb b/spec/langchain/vectorsearch/base_spec.rb index 4697b0603..21084b5b8 100644 --- a/spec/langchain/vectorsearch/base_spec.rb +++ b/spec/langchain/vectorsearch/base_spec.rb @@ -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) diff --git a/spec/langchain/vectorsearch/elasticsearch_spec.rb b/spec/langchain/vectorsearch/elasticsearch_spec.rb index ebeefd0da..72a753239 100644 --- a/spec/langchain/vectorsearch/elasticsearch_spec.rb +++ b/spec/langchain/vectorsearch/elasticsearch_spec.rb @@ -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})