Skip to content

Commit

Permalink
🔥 Drop all search functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
simonwoerpel committed Dec 23, 2024
1 parent d2f95b4 commit 329bdd9
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 149 deletions.
45 changes: 4 additions & 41 deletions ftmq/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from nomenklatura.entity import CE

from ftmq.aggregations import Aggregation, Aggregator
from ftmq.enums import Aggregations, Comparators, Properties
from ftmq.enums import Aggregations, Properties
from ftmq.exceptions import ValidationError
from ftmq.filters import (
FILTERS,
Expand Down Expand Up @@ -56,24 +56,15 @@ def serialize(self) -> list[str]:


class Query:
DEFAULT_SEARCH_PROPS = (
Properties["name"],
Properties["firstName"],
Properties["middleName"],
Properties["lastName"],
)

def __init__(
self,
filters: Iterable[F] | None = None,
search_filters: Iterable[F] | None = None,
aggregations: Iterable[Aggregation] | None = None,
aggregator: Aggregator | None = None,
sort: Sort | None = None,
slice: Slice | None = None,
):
self.filters = set(ensure_list(filters))
self.search_filters = set(ensure_list(search_filters))
self.aggregations = set(ensure_list(aggregations))
self.aggregator = aggregator
self.sort = sort
Expand Down Expand Up @@ -162,13 +153,6 @@ def lookups(self) -> dict[str, Any]:
"""
return self._get_lookups(self.filters)

@property
def search_lookups(self) -> dict[str, Any]:
"""
The current search lookups as dictionary
"""
return self._get_lookups(self.search_filters)

@property
def limit(self) -> int | None:
"""
Expand Down Expand Up @@ -283,9 +267,6 @@ def to_dict(self) -> dict[str, Any]:
```
"""
data = self.lookups
search_data = self.search_lookups
if search_data:
data["search"] = search_data
if self.sort:
data["order_by"] = self.sort.serialize()
if self.slice:
Expand Down Expand Up @@ -364,14 +345,6 @@ def where(self, **lookup: Any) -> Q:

return self._chain()

def search(self, q: str, props: Iterable[Properties | str] = None) -> Q:
# reset existing search
self.search_filters: set[F] = set()
props = props or self.DEFAULT_SEARCH_PROPS
for prop in props:
self.search_filters.add(PropertyFilter(prop, q, Comparators.ilike))
return self._chain()

def order_by(self, *values: Iterable[str], ascending: bool | None = True) -> Q:
"""
Add or update the current sorting.
Expand Down Expand Up @@ -401,23 +374,13 @@ def aggregate(
def get_aggregator(self) -> Aggregator:
return Aggregator(aggregations=self.aggregations)

def apply_filter(self, proxy: CE) -> bool:
if not self.filters:
return True
return all(f.apply(proxy) for f in self.filters)

def apply_search(self, proxy: CE) -> bool:
if not self.search_filters:
return True
return any(f.apply(proxy) for f in self.search_filters)

def apply(self, proxy: CE) -> bool:
"""
Test if a proxy matches the current `Query` instance.
"""
if self.apply_filter(proxy):
return self.apply_search(proxy)
return False
if not self.filters:
return True
return all(f.apply(proxy) for f in self.filters)

def apply_iter(self, proxies: CEGenerator) -> CEGenerator:
"""
Expand Down
24 changes: 1 addition & 23 deletions ftmq/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,26 +117,9 @@ def clause(self) -> BooleanClauseList:
)
return and_(*clauses)

@cached_property
def search_clause(self) -> BooleanClauseList | None:
if not self.q.search_filters:
return
return or_(
and_(
self.table.c.prop == f.key,
self.get_expression(self.table.c.value, f),
)
for f in self.q.search_filters
)

@cached_property
def canonical_ids(self) -> Select:
q = select(self.table.c.canonical_id.distinct()).where(self.clause)
if self.q.search_filters:
search_ids = select(self.table.c.canonical_id.distinct()).where(
self.search_clause
)
q = q.where(self.table.c.canonical_id.in_(search_ids))
if self.q.sort is None:
q = q.limit(self.q.limit).offset(self.q.offset)
return q
Expand All @@ -148,12 +131,7 @@ def all_canonical_ids(self) -> Select:
@cached_property
def _unsorted_statements(self) -> Select:
where = self.clause
if (
self.q.properties
or self.q.reversed
or self.q.search_filters
or self.q.limit
):
if self.q.properties or self.q.reversed or self.q.limit:
where = self.table.c.canonical_id.in_(self.canonical_ids)
return select(self.table).where(where).order_by(self.table.c.canonical_id)

Expand Down
25 changes: 0 additions & 25 deletions tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,31 +172,6 @@ def test_query_filter_comparators():
assert q.to_dict() == {"dataset__startswith": "a"}


def test_query_search():
q = Query().where(dataset="test", schema="Event", date__gte=2023)
q = q.search("meeting")
assert q.to_dict() == {
"date__gte": "2023",
"dataset": "test",
"schema": "Event",
"search": {
"firstName__ilike": "meeting",
"middleName__ilike": "meeting",
"lastName__ilike": "meeting",
"name__ilike": "meeting",
},
}
q = q.search("brussels", ["location"])
assert q.to_dict() == {
"date__gte": "2023",
"dataset": "test",
"schema": "Event",
"search": {
"location__ilike": "brussels",
},
}


def test_query_ids():
q = Query().where(entity_id="e-id")
assert q.to_dict() == {"entity_id": "e-id"}
Expand Down
28 changes: 0 additions & 28 deletions tests/test_search.py

This file was deleted.

28 changes: 1 addition & 27 deletions tests/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ def test_sql():
""",
)

# simplified if no properties, reversed or search:
# simplified if no properties or reversed
q = Query()
assert _compare_str(
q.sql.statements,
Expand Down Expand Up @@ -365,32 +365,6 @@ def test_sql():
assert "canonical_id IN" in str(q[:10].sql.statements)


def test_sql_search_query():
q = Query().search("agency", ["name"])
assert _compare_str(
str(q.sql.canonical_ids.compile(compile_kwargs={"literal_binds": True})),
"""
SELECT DISTINCT test_table.canonical_id
FROM test_table
WHERE test_table.canonical_id IN (SELECT DISTINCT test_table.canonical_id
FROM test_table
WHERE test_table.prop = 'name' AND lower(test_table.value) LIKE lower('%agency%'))
""",
)

q = Query().where(dataset="foo", date__gte=2023).search("agency", ["name"])
assert _compare_str(
str(q.sql.canonical_ids.compile(compile_kwargs={"literal_binds": True})),
"""
SELECT DISTINCT test_table.canonical_id
FROM test_table
WHERE test_table.dataset = 'foo' AND test_table.prop = 'date' AND test_table.value >= '2023' AND test_table.canonical_id IN (SELECT DISTINCT test_table.canonical_id
FROM test_table
WHERE test_table.prop = 'name' AND lower(test_table.value) LIKE lower('%agency%'))
""",
)


def test_sql_ids():
q = Query().where(entity_id="eu-authorities-chafea")
assert "WHERE test_table.entity_id = :entity_id_1" in str(q.sql.statements)
Expand Down
5 changes: 0 additions & 5 deletions tests/test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,6 @@ def _run_store_test(cls: Store, proxies, **kwargs):
res = [p for p in q.apply_iter(proxies)]
assert len(res) == 0

# search
q = Query().where(dataset="eu_authorities").search("agency")
res = [p for p in view.entities(q)]
assert len(res) == 23

# ids
q = Query().where(entity_id="eu-authorities-chafea")
res = [p for p in view.entities(q)]
Expand Down

0 comments on commit 329bdd9

Please sign in to comment.