Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
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
25 changes: 24 additions & 1 deletion common/sds_opensearch_query/mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,29 @@
},
}

# RFC §6 — fed-datasets search fields
# RFC §6 — fed-datasets search fields (aligned with FederatedDatasetDoc / export)
RFC_FED_DATASET_PROPERTIES: dict[str, dict[str, Any]] = {
"uuid": {"type": "keyword"},
"site_name": {"type": "keyword"}, # peer FQDN (federation.toml [site].fqdn)
"name": {"type": "text", "fields": {"keyword": {"type": "keyword"}}},
"version": {"type": "integer"},
"description": {"type": "text"},
"abstract": {"type": "text"},
"keywords": {"type": "keyword"},
"owner_name": {"type": "keyword"},
"status": {"type": "keyword"},
"is_public": {"type": "boolean"},
"is_deleted": {"type": "boolean"},
"created_at": {"type": "date", "format": "strict_date_optional_time||epoch_millis"},
"updated_at": {"type": "date", "format": "strict_date_optional_time||epoch_millis"},
"federation_event_at": {
"type": "date",
"format": "strict_date_optional_time||epoch_millis",
},
"size": {"type": "long"},
"capture_count": {"type": "integer"},
"capture_file_count": {"type": "integer"},
"artifact_file_count": {"type": "integer"},
"url": {"type": "keyword"},
}

Expand All @@ -35,8 +45,21 @@
RFC_FED_CAPTURE_PROPERTIES: dict[str, dict[str, Any]] = {
"uuid": {"type": "keyword"},
"site_name": {"type": "keyword"}, # peer FQDN (federation.toml [site].fqdn)
"name": {"type": "text", "fields": {"keyword": {"type": "keyword"}}},
"capture_type": {"type": "keyword"},
"channel": {"type": "keyword"},
"scan_group": {"type": "keyword"},
"top_level_dir": {"type": "keyword"},
"owner_name": {"type": "keyword"},
Comment thread
klpoland marked this conversation as resolved.
Resolved
"file_count": {"type": "integer"},
"size": {"type": "long"},
"created_at": {"type": "date", "format": "strict_date_optional_time||epoch_millis"},
"updated_at": {"type": "date", "format": "strict_date_optional_time||epoch_millis"},
"federation_event_at": {
"type": "date",
"format": "strict_date_optional_time||epoch_millis",
},
Comment thread
klpoland marked this conversation as resolved.
Resolved
"is_deleted": {"type": "boolean"},
"capture_props": {"type": "nested", "dynamic": True},
"search_props": {"type": "nested", "dynamic": True},
"public_dataset_ids": {"type": "keyword"},
Expand Down
2 changes: 2 additions & 0 deletions common/sds_opensearch_query/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ def federation_not_deleted_clause() -> dict[str, Any]:

def bool_must_search_body(
*must_clauses: dict[str, Any],
must_not_clauses: list[dict[str, Any]] | None = None,
source_includes: list[str] | None = None,
) -> dict[str, Any]:
body: dict[str, Any] = {
"query": {
"bool": {
"must": list(must_clauses),
"must_not": list(must_not_clauses) if must_not_clauses else [],
},
},
}
Expand Down
17 changes: 17 additions & 0 deletions federation/tests/test_regression_opensearch_indices.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,21 @@
"uuid",
"site_name",
"name",
"version",
"description",
"abstract",
"keywords",
"owner_name",
"status",
"is_public",
"is_deleted",
"created_at",
"updated_at",
"federation_event_at",
"size",
"capture_count",
"capture_file_count",
"artifact_file_count",
"url",
},
)
Expand All @@ -31,8 +38,18 @@
{
"uuid",
"site_name",
"name",
"capture_type",
"channel",
"scan_group",
"top_level_dir",
"owner_name",
"file_count",
"size",
"created_at",
"updated_at",
"federation_event_at",
"is_deleted",
"capture_props",
"search_props",
"public_dataset_ids",
Expand Down
58 changes: 47 additions & 11 deletions gateway/sds_gateway/api_methods/federation/search_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import TYPE_CHECKING
from typing import Any

from django.conf import settings
from sds_opensearch_query import bool_must_search_body
from sds_opensearch_query import build_metadata_filter_clauses
from sds_opensearch_query import federation_not_deleted_clause
Expand Down Expand Up @@ -44,7 +45,7 @@
def _site_clause(site: str | None) -> dict[str, Any] | None:
if site is None or site.strip().lower() in _WILDCARD_SITES:
return None
return term_clause("site_name", site.strip())
return term_clause("site_name", value=site.strip())


def _text_clause(q: str | None, fields: list[str]) -> dict[str, Any] | None:
Expand All @@ -70,7 +71,6 @@ def _hits_to_response(hits: list[dict[str, Any]]) -> dict[str, Any]:
def _build_fed_must_clauses(
*,
q: str | None,
site: str | None,
metadata_filters: list[dict[str, Any]] | None,
rfc_properties: Mapping[str, dict[str, Any]],
text_fields: list[str],
Expand All @@ -79,13 +79,9 @@ def _build_fed_must_clauses(
known = flatten_property_paths(rfc_properties)
must: list[dict[str, Any]] = [federation_not_deleted_clause()]

site_filter = _site_clause(site)
if site_filter is not None:
must.append(site_filter)

for field, value in extra_terms or ():
if value is not None and value != "":
must.append(term_clause(field, value))
must.append(term_clause(field, value=value))

text = _text_clause(q, text_fields)
if text is not None:
Expand All @@ -100,6 +96,48 @@ def _build_fed_must_clauses(
return must


def _local_peer_site() -> str:
return str(getattr(settings, "SDS_SITE_FQDN", "") or "").strip()


def federated_published_capture_visibility_must() -> dict[str, Any]:
"""Published peer captures expose ``public_dataset_ids`` (no ``is_public`` on fed-captures)."""
return {"exists": {"field": "public_dataset_ids"}}


def _build_fed_must_not_clauses(
*,
exclude_site: str | None = None,
for_datasets: bool,
) -> list[dict[str, Any]]:
must_not = [term_clause("is_deleted", value=True)]
if for_datasets:
must_not.append(term_clause("is_public", value=False))
if exclude_site:
must_not.append(term_clause("site_name", value=exclude_site))
return must_not
Comment thread
klpoland marked this conversation as resolved.
Resolved


def _build_fed_search_body(
*,
must: list[dict[str, Any]],
site: str | None,
for_datasets: bool,
) -> dict[str, Any]:
site_filter = _site_clause(site)
if site_filter is not None:
must = [*must, site_filter]
if not for_datasets:
must = [*must, federated_published_capture_visibility_must()]
# Fed indices mirror published local assets.
# Postgres is authoritative for this site.
must_not = _build_fed_must_not_clauses(
exclude_site=_local_peer_site() or None,
for_datasets=for_datasets,
)
return bool_must_search_body(*must, must_not_clauses=must_not)


def search_federated_datasets(
client: OpenSearch,
*,
Expand All @@ -110,12 +148,11 @@ def search_federated_datasets(
) -> dict[str, Any]:
must = _build_fed_must_clauses(
q=q,
site=site,
metadata_filters=metadata_filters,
rfc_properties=RFC_FED_DATASET_PROPERTIES,
text_fields=FED_DATASET_TEXT_FIELDS,
)
body = bool_must_search_body(*must)
body = _build_fed_search_body(must=must, site=site, for_datasets=True)
hits = run_search(
client,
index=FED_DATASETS_INDEX,
Expand All @@ -136,13 +173,12 @@ def search_federated_captures(
) -> dict[str, Any]:
must = _build_fed_must_clauses(
q=q,
site=site,
metadata_filters=metadata_filters,
rfc_properties=RFC_FED_CAPTURE_PROPERTIES,
text_fields=FED_CAPTURE_TEXT_FIELDS,
extra_terms=[("capture_type", capture_type)],
)
body = bool_must_search_body(*must)
body = _build_fed_search_body(must=must, site=site, for_datasets=False)
hits = run_search(
client,
index=FED_CAPTURES_INDEX,
Expand Down
Loading
Loading