pgvector-haystack 1.2.0__tar.gz → 1.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (22) hide show
  1. {pgvector_haystack-1.2.0 → pgvector_haystack-1.3.0}/CHANGELOG.md +14 -0
  2. {pgvector_haystack-1.2.0 → pgvector_haystack-1.3.0}/PKG-INFO +5 -4
  3. {pgvector_haystack-1.2.0 → pgvector_haystack-1.3.0}/README.md +1 -1
  4. {pgvector_haystack-1.2.0 → pgvector_haystack-1.3.0}/src/haystack_integrations/document_stores/pgvector/document_store.py +3 -1
  5. {pgvector_haystack-1.2.0 → pgvector_haystack-1.3.0}/src/haystack_integrations/document_stores/pgvector/filters.py +16 -0
  6. {pgvector_haystack-1.2.0 → pgvector_haystack-1.3.0}/tests/test_document_store.py +45 -0
  7. {pgvector_haystack-1.2.0 → pgvector_haystack-1.3.0}/tests/test_filters.py +58 -2
  8. {pgvector_haystack-1.2.0 → pgvector_haystack-1.3.0}/.gitignore +0 -0
  9. {pgvector_haystack-1.2.0 → pgvector_haystack-1.3.0}/LICENSE.txt +0 -0
  10. {pgvector_haystack-1.2.0 → pgvector_haystack-1.3.0}/examples/embedding_retrieval.py +0 -0
  11. {pgvector_haystack-1.2.0 → pgvector_haystack-1.3.0}/examples/hybrid_retrieval.py +0 -0
  12. {pgvector_haystack-1.2.0 → pgvector_haystack-1.3.0}/pydoc/config.yml +0 -0
  13. {pgvector_haystack-1.2.0 → pgvector_haystack-1.3.0}/pyproject.toml +0 -0
  14. {pgvector_haystack-1.2.0 → pgvector_haystack-1.3.0}/src/haystack_integrations/components/retrievers/pgvector/__init__.py +0 -0
  15. {pgvector_haystack-1.2.0 → pgvector_haystack-1.3.0}/src/haystack_integrations/components/retrievers/pgvector/embedding_retriever.py +0 -0
  16. {pgvector_haystack-1.2.0 → pgvector_haystack-1.3.0}/src/haystack_integrations/components/retrievers/pgvector/keyword_retriever.py +0 -0
  17. {pgvector_haystack-1.2.0 → pgvector_haystack-1.3.0}/src/haystack_integrations/document_stores/pgvector/__init__.py +0 -0
  18. {pgvector_haystack-1.2.0 → pgvector_haystack-1.3.0}/tests/__init__.py +0 -0
  19. {pgvector_haystack-1.2.0 → pgvector_haystack-1.3.0}/tests/conftest.py +0 -0
  20. {pgvector_haystack-1.2.0 → pgvector_haystack-1.3.0}/tests/test_embedding_retrieval.py +0 -0
  21. {pgvector_haystack-1.2.0 → pgvector_haystack-1.3.0}/tests/test_keyword_retrieval.py +0 -0
  22. {pgvector_haystack-1.2.0 → pgvector_haystack-1.3.0}/tests/test_retrievers.py +0 -0
@@ -1,5 +1,19 @@
1
1
  # Changelog
2
2
 
3
+ ## [integrations/pgvector-v1.2.1] - 2025-01-10
4
+
5
+ ### 🐛 Bug Fixes
6
+
7
+ - PgvectorDocumentStore - use appropriate schema name if dropping index (#1277)
8
+
9
+
10
+ ## [integrations/pgvector-v1.2.0] - 2024-11-22
11
+
12
+ ### 🚀 Features
13
+
14
+ - Add `create_extension` parameter to control vector extension creation (#1213)
15
+
16
+
3
17
  ## [integrations/pgvector-v1.1.0] - 2024-11-21
4
18
 
5
19
  ### 🚀 Features
@@ -1,12 +1,13 @@
1
- Metadata-Version: 2.3
1
+ Metadata-Version: 2.4
2
2
  Name: pgvector-haystack
3
- Version: 1.2.0
3
+ Version: 1.3.0
4
4
  Summary: An integration of pgvector (vector search extension for Postgres) with Haystack
5
5
  Project-URL: Source, https://github.com/deepset-ai/haystack-core-integrations
6
6
  Project-URL: Documentation, https://github.com/deepset-ai/haystack-core-integrations/blob/main/integrations/pgvector/README.md
7
7
  Project-URL: Issues, https://github.com/deepset-ai/haystack-core-integrations/issues
8
8
  Author-email: deepset GmbH <info@deepset.ai>
9
- License: Apache-2.0
9
+ License-Expression: Apache-2.0
10
+ License-File: LICENSE.txt
10
11
  Classifier: Development Status :: 4 - Beta
11
12
  Classifier: License :: OSI Approved :: Apache Software License
12
13
  Classifier: Programming Language :: Python
@@ -47,7 +48,7 @@ pip install pgvector-haystack
47
48
 
48
49
  Ensure that you have a PostgreSQL running with the `pgvector` extension. For a quick setup using Docker, run:
49
50
  ```
50
- docker run -d -p 5432:5432 -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=postgres ankane/pgvector
51
+ docker run -d -p 5432:5432 -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=postgres pgvector/pgvector:pg17
51
52
  ```
52
53
 
53
54
  then run the tests:
@@ -22,7 +22,7 @@ pip install pgvector-haystack
22
22
 
23
23
  Ensure that you have a PostgreSQL running with the `pgvector` extension. For a quick setup using Docker, run:
24
24
  ```
25
- docker run -d -p 5432:5432 -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=postgres ankane/pgvector
25
+ docker run -d -p 5432:5432 -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=postgres pgvector/pgvector:pg17
26
26
  ```
27
27
 
28
28
  then run the tests:
@@ -389,7 +389,9 @@ class PgvectorDocumentStore:
389
389
  )
390
390
  return
391
391
 
392
- sql_drop_index = SQL("DROP INDEX IF EXISTS {index_name}").format(index_name=Identifier(self.hnsw_index_name))
392
+ sql_drop_index = SQL("DROP INDEX IF EXISTS {schema_name}.{index_name}").format(
393
+ schema_name=Identifier(self.schema_name), index_name=Identifier(self.hnsw_index_name)
394
+ )
393
395
  self._execute_sql(sql_drop_index, error_msg="Could not drop HNSW index")
394
396
 
395
397
  self._create_hnsw_index()
@@ -232,6 +232,20 @@ def _in(field: str, value: Any) -> Tuple[str, List]:
232
232
  return f"{field} = ANY(%s)", [value]
233
233
 
234
234
 
235
+ def _like(field: str, value: Any) -> Tuple[str, Any]:
236
+ if not isinstance(value, str):
237
+ msg = f"{field}'s value must be a str when using 'LIKE' "
238
+ raise FilterError(msg)
239
+ return f"{field} LIKE %s", value
240
+
241
+
242
+ def _not_like(field: str, value: Any) -> Tuple[str, Any]:
243
+ if not isinstance(value, str):
244
+ msg = f"{field}'s value must be a str when using 'LIKE' "
245
+ raise FilterError(msg)
246
+ return f"{field} NOT LIKE %s", value
247
+
248
+
235
249
  COMPARISON_OPERATORS = {
236
250
  "==": _equal,
237
251
  "!=": _not_equal,
@@ -241,4 +255,6 @@ COMPARISON_OPERATORS = {
241
255
  "<=": _less_than_equal,
242
256
  "in": _in,
243
257
  "not in": _not_in,
258
+ "like": _like,
259
+ "not like": _not_like,
244
260
  }
@@ -5,6 +5,7 @@
5
5
  from unittest.mock import patch
6
6
 
7
7
  import numpy as np
8
+ import psycopg
8
9
  import pytest
9
10
  from haystack.dataclasses.document import ByteStream, Document
10
11
  from haystack.document_stores.errors import DuplicateDocumentError
@@ -259,3 +260,47 @@ def test_from_pg_to_haystack_documents():
259
260
  assert haystack_docs[2].meta == {"meta_key": "meta_value"}
260
261
  assert haystack_docs[2].embedding == [0.7, 0.8, 0.9]
261
262
  assert haystack_docs[2].score is None
263
+
264
+
265
+ @pytest.mark.integration
266
+ def test_hnsw_index_recreation():
267
+ def get_index_oid(document_store, schema_name, index_name):
268
+ sql_get_index_oid = """
269
+ SELECT c.oid
270
+ FROM pg_class c
271
+ JOIN pg_namespace n ON n.oid = c.relnamespace
272
+ WHERE c.relkind = 'i'
273
+ AND n.nspname = %s
274
+ AND c.relname = %s;
275
+ """
276
+ return document_store.cursor.execute(sql_get_index_oid, (schema_name, index_name)).fetchone()[0]
277
+
278
+ # create a new schema
279
+ connection_string = "postgresql://postgres:postgres@localhost:5432/postgres"
280
+ schema_name = "test_schema"
281
+ with psycopg.connect(connection_string, autocommit=True) as conn:
282
+ conn.execute(f"CREATE SCHEMA IF NOT EXISTS {schema_name}")
283
+
284
+ # create a first document store and trigger the creation of the hnsw index
285
+ params = {
286
+ "connection_string": Secret.from_token(connection_string),
287
+ "schema_name": schema_name,
288
+ "table_name": "haystack_test_hnsw_index_recreation",
289
+ "search_strategy": "hnsw",
290
+ }
291
+ ds1 = PgvectorDocumentStore(**params)
292
+ ds1._initialize_table()
293
+
294
+ # get the hnsw index oid
295
+ hnws_index_name = "haystack_hnsw_index"
296
+ first_oid = get_index_oid(ds1, ds1.schema_name, hnws_index_name)
297
+
298
+ # create second document store with recreation enabled
299
+ ds2 = PgvectorDocumentStore(**params, hnsw_recreate_index_if_exists=True)
300
+ ds2._initialize_table()
301
+
302
+ # get the index oid
303
+ second_oid = get_index_oid(ds2, ds2.schema_name, hnws_index_name)
304
+
305
+ # verify that oids differ
306
+ assert second_oid != first_oid, "Index was not recreated (OID remained the same)"
@@ -2,7 +2,7 @@ from typing import List
2
2
 
3
3
  import pytest
4
4
  from haystack.dataclasses.document import Document
5
- from haystack.testing.document_store import FilterDocumentsTest
5
+ from haystack.testing.document_store import FilterDocumentsTest, FilterDocumentsTestWithDataframe
6
6
  from pandas import DataFrame
7
7
  from psycopg.sql import SQL
8
8
  from psycopg.types.json import Jsonb
@@ -17,7 +17,7 @@ from haystack_integrations.document_stores.pgvector.filters import (
17
17
 
18
18
 
19
19
  @pytest.mark.integration
20
- class TestFilters(FilterDocumentsTest):
20
+ class TestFilters(FilterDocumentsTest, FilterDocumentsTestWithDataframe):
21
21
  def assert_documents_are_equal(self, received: List[Document], expected: List[Document]):
22
22
  """
23
23
  This overrides the default assert_documents_are_equal from FilterDocumentsTest.
@@ -40,6 +40,62 @@ class TestFilters(FilterDocumentsTest):
40
40
  @pytest.mark.skip(reason="NOT operator is not supported in PgvectorDocumentStore")
41
41
  def test_not_operator(self, document_store, filterable_docs): ...
42
42
 
43
+ def test_like_operator(self, document_store, filterable_docs):
44
+ document_store.write_documents(filterable_docs)
45
+ filters = {"field": "content", "operator": "like", "value": "%Foo%"}
46
+ result = document_store.filter_documents(filters=filters)
47
+
48
+ self.assert_documents_are_equal(result, [d for d in filterable_docs if "Foo" in d.content])
49
+
50
+ def test_like_operator_startswith(self, document_store, filterable_docs):
51
+ document_store.write_documents(filterable_docs)
52
+ filters = {"field": "content", "operator": "like", "value": "Foo%"}
53
+ result = document_store.filter_documents(filters=filters)
54
+
55
+ self.assert_documents_are_equal(result, [d for d in filterable_docs if d.content.startswith("Foo")])
56
+
57
+ def test_like_operator_nb_chars(self, document_store, filterable_docs):
58
+ document_store.write_documents(filterable_docs)
59
+ filters = {"field": "content", "operator": "like", "value": "A Foobar Document__"}
60
+ result = document_store.filter_documents(filters=filters)
61
+
62
+ self.assert_documents_are_equal(
63
+ result,
64
+ [
65
+ d
66
+ for d in filterable_docs
67
+ if (d.content.startswith("A Foobar Document") and len(d.content) == (len("A Foobar Document") + 2))
68
+ ],
69
+ )
70
+
71
+ def test_not_like_operator(self, document_store, filterable_docs):
72
+ document_store.write_documents(filterable_docs)
73
+ filters = {"field": "content", "operator": "not like", "value": "%Foo%"}
74
+ result = document_store.filter_documents(filters=filters)
75
+
76
+ self.assert_documents_are_equal(result, [d for d in filterable_docs if "Foo" not in d.content])
77
+
78
+ def test_not_like_operator_startswith(self, document_store, filterable_docs):
79
+ document_store.write_documents(filterable_docs)
80
+ filters = {"field": "content", "operator": "not like", "value": "Foo%"}
81
+ result = document_store.filter_documents(filters=filters)
82
+
83
+ self.assert_documents_are_equal(result, [d for d in filterable_docs if not d.content.startswith("Foo")])
84
+
85
+ def test_not_like_operator_nb_chars(self, document_store, filterable_docs):
86
+ document_store.write_documents(filterable_docs)
87
+ filters = {"field": "content", "operator": "not like", "value": "A Foobar Document__"}
88
+ result = document_store.filter_documents(filters=filters)
89
+
90
+ self.assert_documents_are_equal(
91
+ result,
92
+ [
93
+ d
94
+ for d in filterable_docs
95
+ if not (d.content.startswith("A Foobar Document") and len(d.content) == (len("A Foobar Document") + 2))
96
+ ],
97
+ )
98
+
43
99
  def test_complex_filter(self, document_store, filterable_docs):
44
100
  document_store.write_documents(filterable_docs)
45
101
  filters = {