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

Feature/ogc 508 replace elastic search by postgres v2 #1357

Draft
wants to merge 29 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
ef937bc
Resolve merge conflict
Tschuppi81 Jun 3, 2024
2fdd50e
Adds view and templates for search views
Tschuppi81 Jun 4, 2024
d90e85a
Revert type wrong type annotations
Tschuppi81 Jun 5, 2024
22932f0
Sort search results after score, timestamp
Tschuppi81 Jun 5, 2024
0507b46
Adds a simple ranking
Tschuppi81 Jun 5, 2024
e6b510e
Merge branch 'master' into feature/ogc-508-replace-elastic-search-by-…
Tschuppi81 Jun 6, 2024
b8f3b76
Resolve merge conflicts
Tschuppi81 Jul 12, 2024
3beab01
Person title and user title and userprofile are now hybrid properties
Tschuppi81 Jul 16, 2024
da09357
Rework ranking
Tschuppi81 Jul 16, 2024
db8ae9b
Cleanup
Tschuppi81 Jul 18, 2024
f9ba776
Make tickets searchable
Tschuppi81 Jul 18, 2024
5718732
Make tickets searchable
Tschuppi81 Jul 18, 2024
737fb14
makes directory entries searchable
Tschuppi81 Jul 18, 2024
7846539
Revert "makes directory entries searchable"
Tschuppi81 Jul 18, 2024
cfcd6af
Resolve merge conflict
Tschuppi81 Jul 19, 2024
904a32c
Make mypy almost happy
Tschuppi81 Jul 19, 2024
956befa
Add fixme's as ranking does not work with extra localized text for su…
Tschuppi81 Jul 19, 2024
d1f8037
Merge branch 'master' into feature/ogc-508-replace-elastic-search-by-…
Tschuppi81 Aug 23, 2024
b25a51d
Make a generic class
Tschuppi81 Aug 23, 2024
007d8cb
Fix search results for tickets as not each ticket provides the values
Tschuppi81 Aug 26, 2024
57f1ea4
Transform directory_entry.keywords to hybrid property
Tschuppi81 Aug 27, 2024
abbf86c
Adds weighted vector to search query
Tschuppi81 Sep 9, 2024
4d04f5d
Resove merge conflicts
Tschuppi81 Sep 9, 2024
ac18d16
Fix extra localized text for tickets in search results
Tschuppi81 Sep 9, 2024
45eb359
Adds future events on top of search results (in case of hit)
Tschuppi81 Sep 9, 2024
d7a560a
Make load bach result a cached property
Tschuppi81 Sep 9, 2024
85eff7e
Make mypy happy
Tschuppi81 Sep 9, 2024
6114dad
Adds default implementation for extra localized text
Tschuppi81 Sep 9, 2024
71e61b6
Fixing org search template
Tschuppi81 Sep 9, 2024
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
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ repos:
- id: sass-lint
files: '^src/.*\.scss'
- repo: https://github.com/pre-commit/mirrors-eslint
rev: v9.9.1
rev: v9.10.0
hooks:
- id: eslint
files: '^src/.*\.jsx?$'
Expand Down
16 changes: 13 additions & 3 deletions src/onegov/agency/views/search.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from onegov.agency import AgencyApp
from onegov.agency.layout import AgencySearchLayout
from onegov.core.security import Public
from onegov.org.models import Search
from onegov.org.views.search import search as search_view

from onegov.org.models import Search, SearchPostgres
from onegov.org.views.search import search as search_view, search_postgres

from typing import TYPE_CHECKING
if TYPE_CHECKING:
Expand All @@ -23,3 +22,14 @@
if isinstance(data, dict):
data['layout'] = AgencySearchLayout(self, request)
return data


@AgencyApp.html(model=SearchPostgres, template='search.pt', permission=Public)
def agency_search_postgres(
self: SearchPostgres['Base'],
request: 'AgencyRequest'
) -> 'RenderData | Response':
data = search_postgres(self, request)
if isinstance(data, dict):
data['layout'] = AgencySearchLayout(self, request)
return data

Check warning on line 35 in src/onegov/agency/views/search.py

View check run for this annotation

Codecov / codecov/patch

src/onegov/agency/views/search.py#L32-L35

Added lines #L32 - L35 were not covered by tests
23 changes: 17 additions & 6 deletions src/onegov/directory/models/directory_entry.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from sqlalchemy.ext.hybrid import hybrid_property

from onegov.core.orm import Base
from onegov.core.orm.mixins import ContentMixin
from onegov.core.orm.mixins import TimestampMixin
Expand All @@ -6,7 +8,7 @@
from onegov.file import AssociatedFiles
from onegov.gis import CoordinatesMixin
from onegov.search import SearchableContent
from sqlalchemy import Column
from sqlalchemy import Column, func, cast, ARRAY, String
from sqlalchemy import ForeignKey
from sqlalchemy import Index
from sqlalchemy import Text
Expand All @@ -30,10 +32,10 @@ class DirectoryEntry(Base, ContentMixin, CoordinatesMixin, TimestampMixin,
__tablename__ = 'directory_entries'

es_properties = {
'keywords': {'type': 'keyword'},
'title': {'type': 'localized'},
'lead': {'type': 'localized'},
'directory_id': {'type': 'keyword'},
'keywords': {'type': 'keyword'},
# 'directory_id': {'type': 'keyword'},

# since the searchable text might include html, we remove it
# even if there's no html -> possibly decreasing the search
Expand Down Expand Up @@ -113,17 +115,26 @@ def external_link_visible(self) -> bool | None:
def directory_name(self) -> str:
return self.directory.name

@property
@hybrid_property
def keywords(self) -> set[str]:
return set(self._keywords.keys()) if self._keywords else set()

# FIXME: asymmetric properties are not supported by mypy, switch to
# a custom descriptor, if desired.
@keywords.setter
@keywords.setter # type:ignore[no-redef]
def keywords(self, value: 'Collection[str] | None') -> None:
self._keywords = dict.fromkeys(value, '') if value else None

@property
@keywords.expression # type:ignore[no-redef]
def keywords(cls):
return func.array_to_string(
func.array_agg(
cast(func.jsonb_each_text(cls._keywords).keys(), ARRAY(String))
),
' '
)

@hybrid_property
def text(self) -> str:
return self.directory.configuration.extract_searchable(self.values)

Expand Down
23 changes: 21 additions & 2 deletions src/onegov/fsi/views/search.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from onegov.core.security import Personal
from onegov.fsi import FsiApp
from onegov.org.models import Search
from onegov.town6.views.search import town_search as search_view
from onegov.org.models import Search, SearchPostgres
from onegov.org.views.search import search as search_view
from onegov.org.views.search import search_postgres as search_postgres_view
from onegov.org.views.search import suggestions as suggestions_view
from onegov.org.views.search import (suggestions_postgres as
suggestions_postgres_view)


from typing import TYPE_CHECKING
Expand All @@ -21,9 +24,25 @@
return search_view(self, request)


@FsiApp.html(model=SearchPostgres, template='search.pt', permission=Personal)
def search_postgres(
self: SearchPostgres['Base'],
request: 'FsiRequest'
) -> 'RenderData | Response':
return search_postgres_view(self, request)

Check warning on line 32 in src/onegov/fsi/views/search.py

View check run for this annotation

Codecov / codecov/patch

src/onegov/fsi/views/search.py#L32

Added line #L32 was not covered by tests


@FsiApp.json(model=Search, name='suggest', permission=Personal)
def suggestions(
self: Search['Base'],
request: 'FsiRequest'
) -> 'JSON_ro':
return suggestions_view(self, request)


@FsiApp.json(model=SearchPostgres, name='suggest', permission=Personal)
def suggestions_postgres(
self: SearchPostgres['Base'],
request: 'FsiRequest'
) -> 'JSON_ro':
return suggestions_postgres_view(self, request)

Check warning on line 48 in src/onegov/fsi/views/search.py

View check run for this annotation

Codecov / codecov/patch

src/onegov/fsi/views/search.py#L48

Added line #L48 was not covered by tests
14 changes: 11 additions & 3 deletions src/onegov/landsgemeinde/views/search.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from onegov.core.security import Public
from onegov.landsgemeinde import LandsgemeindeApp
from onegov.landsgemeinde.layouts import DefaultLayout
from onegov.org.models import Search
from onegov.org.views.search import search

from onegov.org.models import Search, SearchPostgres
from onegov.org.views.search import search, search_postgres

from typing import TYPE_CHECKING
if TYPE_CHECKING:
Expand All @@ -19,3 +18,12 @@
request: 'LandsgemeindeRequest'
) -> 'RenderData | Response':
return search(self, request, DefaultLayout(self, request))


@LandsgemeindeApp.html(model=SearchPostgres, template='search.pt',
permission=Public)
def landsgemeinde_search_postgres(
self: SearchPostgres['Base'],
request: 'LandsgemeindeRequest'
) -> 'RenderData | Response':
return search_postgres(self, request, DefaultLayout(self, request))

Check warning on line 29 in src/onegov/landsgemeinde/views/search.py

View check run for this annotation

Codecov / codecov/patch

src/onegov/landsgemeinde/views/search.py#L29

Added line #L29 was not covered by tests
4 changes: 2 additions & 2 deletions src/onegov/onboarding/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
from onegov.file import DepotApp
from onegov.onboarding.theme import OnboardingTheme
from onegov.reservation import LibresIntegration
from onegov.search import ElasticsearchApp
from onegov.search import SearchApp


from typing import Any, TYPE_CHECKING
if TYPE_CHECKING:
from collections.abc import Iterator


class OnboardingApp(Framework, LibresIntegration, DepotApp, ElasticsearchApp):
class OnboardingApp(Framework, LibresIntegration, DepotApp, SearchApp):

serve_static_files = True

Expand Down
2 changes: 1 addition & 1 deletion src/onegov/onboarding/models/town_assistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def add_town(
'org': name
})

self.app.es_perform_reindex()
self.app.perform_reindex()
self.app.send_transactional_email(
subject=title,
receivers=(user, ),
Expand Down
4 changes: 2 additions & 2 deletions src/onegov/org/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from onegov.page import Page, PageCollection
from onegov.pay import PayApp
from onegov.reservation import LibresIntegration
from onegov.search import ElasticsearchApp
from onegov.search import SearchApp
from onegov.ticket import TicketCollection
from onegov.ticket import TicketPermission
from onegov.user import UserApp
Expand All @@ -59,7 +59,7 @@
from webob import Response


class OrgApp(Framework, LibresIntegration, ElasticsearchApp, MapboxApp,
class OrgApp(Framework, LibresIntegration, SearchApp, MapboxApp,
DepotApp, PayApp, FormApp, UserApp, WebsocketsApp):

serve_static_files = True
Expand Down
9 changes: 9 additions & 0 deletions src/onegov/org/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
from onegov.org.models.extensions import PersonLinkExtension
from onegov.org.models.external_link import ExternalLinkCollection
from onegov.org.models.form import submission_deletable
from onegov.org.models.search import SearchPostgres
from onegov.org.open_graph import OpenGraphMixin
from onegov.org.theme.org_theme import user_options
from onegov.org.utils import IMG_URLS
Expand Down Expand Up @@ -334,11 +335,19 @@
@cached_property
def search_url(self) -> str:
""" Returns the url to the search page. """
# Allows using postgres search while es search remains default
if (self.request.path_info
and 'search-postgres' in self.request.path_info):
return self.request.class_link(SearchPostgres)

Check warning on line 341 in src/onegov/org/layout.py

View check run for this annotation

Codecov / codecov/patch

src/onegov/org/layout.py#L341

Added line #L341 was not covered by tests
return self.request.class_link(Search)

@cached_property
def suggestions_url(self) -> str:
""" Returns the url to the suggestions json view. """
# Allows using postgres search while es search remains default
if (self.request.path_info
and 'search-postgres' in self.request.path_info):
return self.request.class_link(SearchPostgres, name='suggest')

Check warning on line 350 in src/onegov/org/layout.py

View check run for this annotation

Codecov / codecov/patch

src/onegov/org/layout.py#L350

Added line #L350 was not covered by tests
return self.request.class_link(Search, name='suggest')

@cached_property
Expand Down
3 changes: 2 additions & 1 deletion src/onegov/org/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
from onegov.org.models.recipient import ResourceRecipient
from onegov.org.models.recipient import ResourceRecipientCollection
from onegov.org.models.resource import DaypassResource
from onegov.org.models.search import Search
from onegov.org.models.search import Search, SearchPostgres
from onegov.org.models.sitecollection import SiteCollection
from onegov.org.models.swiss_holidays import SwissHolidays
from onegov.org.models.tan import TANAccess
Expand Down Expand Up @@ -101,6 +101,7 @@
'ResourceRecipient',
'ResourceRecipientCollection',
'Search',
'SearchPostgres',
'SiteCollection',
'SubmissionMessage',
'SwissHolidays',
Expand Down
Loading
Loading