⚡️ /stop API endpoint uses stop and towns names to resolve queries
This commit is contained in:
@@ -5,9 +5,12 @@ from typing import Iterable, Sequence, TYPE_CHECKING
|
|||||||
from sqlalchemy import (
|
from sqlalchemy import (
|
||||||
BigInteger,
|
BigInteger,
|
||||||
Column,
|
Column,
|
||||||
|
Computed,
|
||||||
|
desc,
|
||||||
Enum,
|
Enum,
|
||||||
Float,
|
Float,
|
||||||
ForeignKey,
|
ForeignKey,
|
||||||
|
func,
|
||||||
Integer,
|
Integer,
|
||||||
JSON,
|
JSON,
|
||||||
select,
|
select,
|
||||||
@@ -22,6 +25,7 @@ from sqlalchemy.orm import (
|
|||||||
with_polymorphic,
|
with_polymorphic,
|
||||||
)
|
)
|
||||||
from sqlalchemy.schema import Index
|
from sqlalchemy.schema import Index
|
||||||
|
from sqlalchemy_utils.types.ts_vector import TSVectorType
|
||||||
|
|
||||||
from ..db import Base, db
|
from ..db import Base, db
|
||||||
from ..idfm_interface.idfm_types import TransportMode, IdfmState, StopAreaType
|
from ..idfm_interface.idfm_types import TransportMode, IdfmState, StopAreaType
|
||||||
@@ -71,13 +75,17 @@ class _Stop(Base):
|
|||||||
back_populates="stops", lazy="selectin"
|
back_populates="stops", lazy="selectin"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
names_tsv = mapped_column(
|
||||||
|
TSVectorType("name", "town_name", regconfig="french"),
|
||||||
|
Computed("to_tsvector('french', name || ' ' || town_name)", persisted=True),
|
||||||
|
)
|
||||||
|
|
||||||
__tablename__ = "_stops"
|
__tablename__ = "_stops"
|
||||||
__mapper_args__ = {"polymorphic_identity": "_stops", "polymorphic_on": kind}
|
__mapper_args__ = {"polymorphic_identity": "_stops", "polymorphic_on": kind}
|
||||||
__table_args__ = (
|
__table_args__ = (
|
||||||
# To optimize the ilike requests
|
|
||||||
Index(
|
Index(
|
||||||
"name_idx_gin",
|
"names_tsv_idx",
|
||||||
name,
|
names_tsv,
|
||||||
postgresql_ops={"name": "gin_trgm_ops"},
|
postgresql_ops={"name": "gin_trgm_ops"},
|
||||||
postgresql_using="gin",
|
postgresql_using="gin",
|
||||||
),
|
),
|
||||||
@@ -92,9 +100,15 @@ class _Stop(Base):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
stop_stop_area = with_polymorphic(_Stop, [Stop, StopArea])
|
stop_stop_area = with_polymorphic(_Stop, [Stop, StopArea])
|
||||||
|
match_stmt = stop_stop_area.names_tsv.match(name, postgresql_regconfig="french")
|
||||||
|
ranking_stmt = func.ts_rank_cd(
|
||||||
|
stop_stop_area.names_tsv, func.plainto_tsquery("french", name)
|
||||||
|
)
|
||||||
|
|
||||||
stmt = (
|
stmt = (
|
||||||
select(stop_stop_area)
|
select(stop_stop_area)
|
||||||
.where(stop_stop_area.name.ilike(f"%{name}%"))
|
.filter(match_stmt)
|
||||||
|
.order_by(desc(ranking_stmt))
|
||||||
.options(
|
.options(
|
||||||
selectinload(stop_stop_area.areas),
|
selectinload(stop_stop_area.areas),
|
||||||
selectinload(stop_stop_area.lines),
|
selectinload(stop_stop_area.lines),
|
||||||
|
Reference in New Issue
Block a user