From 60d37a1677d819856dd8202adb9d6753530ed3bd Mon Sep 17 00:00:00 2001 From: Derek Eder Date: Fri, 6 Dec 2024 15:56:46 -0600 Subject: [PATCH 1/2] working through update implementation with Chicago councilmatic --- postgres_fts_backend/__init__.py | 25 ++++++++++++++++++++++--- pyproject.toml | 3 +-- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/postgres_fts_backend/__init__.py b/postgres_fts_backend/__init__.py index cebc97e..12c982e 100644 --- a/postgres_fts_backend/__init__.py +++ b/postgres_fts_backend/__init__.py @@ -14,15 +14,34 @@ SearchNode, log_query, ) -from haystack.constants import DEFAULT_ALIAS +from haystack.constants import DEFAULT_ALIAS, ID +from haystack.exceptions import SkipDocument from haystack.inputs import Clean, PythonData from haystack.models import SearchResult from haystack.utils import get_model_ct_tuple class PostgresFTSSearchBackend(BaseSearchBackend): - def update(self, indexer, iterable, commit=True): - warn("update is not implemented in this backend") + def update(self, index, iterable, commit=True): + prepped_docs = [] + + for obj in iterable[0:10]: + try: + print(obj) + prepped_data = index.full_prepare(obj) + prepped_data["_id"] = prepped_data[ID] + prepped_docs.append(prepped_data) + except SkipDocument: + self.log.debug("Indexing for object `%s` skipped", obj) + + from chicago.models import SearchBill + prepped_models = [] + for d in prepped_docs: + prepped_models.append(SearchBill(text=d['text'], bill=d['django_id'])) + + SearchBill.objects.bulk_create(prepped_models) + print("success") + def remove(self, obj, commit=True): warn("remove is not implemented in this backend") diff --git a/pyproject.toml b/pyproject.toml index 0bd106e..1cafdc9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,10 +6,9 @@ build-backend = "setuptools.build_meta" name = "postgres-fts-backend" version = "0.0.0" dependencies = [ - 'django>=5.0', 'django-haystack>=2.8.0', ] -requires-python = ">= 3.8" +requires-python = ">= 3.7" authors = [{name = "Forest Gregg", email = "fgregg@datamade.us" }] description = "A PostgreSQL Full Text Seach Backend for Haystack" readme = "README.md" From f4d1928ab8a55108907f863376c4e45a8dc19c18 Mon Sep 17 00:00:00 2001 From: Derek Eder Date: Fri, 13 Dec 2024 16:12:02 -0600 Subject: [PATCH 2/2] saving objects to SearchBill model --- postgres_fts_backend/__init__.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/postgres_fts_backend/__init__.py b/postgres_fts_backend/__init__.py index 12c982e..b755c28 100644 --- a/postgres_fts_backend/__init__.py +++ b/postgres_fts_backend/__init__.py @@ -34,10 +34,14 @@ def update(self, index, iterable, commit=True): except SkipDocument: self.log.debug("Indexing for object `%s` skipped", obj) - from chicago.models import SearchBill + print("objects prepped") + from chicago.models import SearchBill, ChicagoBill + prepped_models = [] for d in prepped_docs: - prepped_models.append(SearchBill(text=d['text'], bill=d['django_id'])) + chicago_bill = ChicagoBill.objects.get(id=d['django_id']) + search_bill = SearchBill(text=d['text'], bill=chicago_bill, extras={'django_ct': d['django_ct'], 'django_id': d['django_id']}) + prepped_models.append(search_bill) SearchBill.objects.bulk_create(prepped_models) print("success")