Skip to content

ftmq.search

Source code in ftmq/search/store/__init__.py
@cache
def get_store(**kwargs: Any) -> BaseStore:
    settings = Settings()
    uri = kwargs.pop("uri", None)
    if uri is None:
        if settings.yaml_uri is not None:
            store = BaseStore.from_yaml_uri(settings.yaml_uri, **kwargs)
            return get_store(**store.model_dump())
        if settings.json_uri is not None:
            store = BaseStore.from_json_uri(settings.json_uri, **kwargs)
            return get_store(**store.model_dump())
        uri = settings.uri
    uri = ensure_uri(uri)
    parsed = urlparse(uri)
    if parsed.scheme == "sqlite":
        return SQliteStore(uri=uri, **kwargs)
    if parsed.scheme in ("tantivy", "memory"):
        try:
            from ftmq.search.store.tantivy import TantivyStore

            return TantivyStore(uri=uri, memory=parsed.scheme == "memory")
        except ImportError as e:
            raise ImportError(
                "Can not load TantivyStore. Install `tantivy` (`ftmq[search]`)"
            ) from e
    raise NotImplementedError(f"Store scheme: `{parsed.scheme}`")

FilterTerm dataclass

One search-index predicate: field holds any of values (none, if negated).

The terms of a query AND together.

Source code in ftmq/search/store/base.py
@dataclass(frozen=True)
class FilterTerm:
    """One search-index predicate: `field` holds any of `values` (none, if `negated`).

    The terms of a query AND together.
    """

    field: str
    values: frozenset[str]
    negated: bool = False

get_filters(query)

Compile a query's filter tree into the flat term list a search index applies.

Only datasets, schema and countries are indexed, filters on other fields are dropped. A not / not_in comparator (or ~ around a single condition) becomes a negated term.

Parameters:

Name Type Description Default
query Query | None

The query to compile (None means no filters).

required

Returns:

Type Description
list[FilterTerm]

The terms to AND together.

Raises:

Type Description
QueryError

For a shape ANDed terms can't express (a cross-field OR, a negated group, a comparator like ilike on an indexed field).

Source code in ftmq/search/store/base.py
def get_filters(query: Query | None) -> list[FilterTerm]:
    """Compile a query's filter tree into the flat term list a search index applies.

    Only `datasets`, `schema` and `countries` are indexed, filters on other fields
    are dropped. A `not` / `not_in` comparator (or `~` around a single condition)
    becomes a negated term.

    Args:
        query: The query to compile (`None` means no filters).

    Returns:
        The terms to AND together.

    Raises:
        QueryError: For a shape ANDed terms can't express (a cross-field OR, a
            negated group, a comparator like `ilike` on an indexed field).
    """
    if query is None or query.q is None:
        return []
    return _collect(query.q)