diff --git a/swh/web/common/archive.py b/swh/web/common/archive.py --- a/swh/web/common/archive.py +++ b/swh/web/common/archive.py @@ -359,11 +359,18 @@ Iterable of origin metadata information for existing origins """ - matches = idx_storage.origin_intrinsic_metadata_search_fulltext( - conjunction=[fulltext], limit=limit - ) - matches = [match.to_dict() for match in matches] results = [] + if search and config.get_config()["metadata_search_backend"] == "swh-search": + page_result = search.origin_search(metadata_pattern=fulltext, limit=limit,) + matches = idx_storage.origin_intrinsic_metadata_get( + [r["url"] for r in page_result.results] + ) + else: + matches = idx_storage.origin_intrinsic_metadata_search_fulltext( + conjunction=[fulltext], limit=limit + ) + + matches = [match.to_dict() for match in matches] origins = storage.origin_get([match["id"] for match in matches]) for origin, match in zip(origins, matches): if not origin: @@ -371,6 +378,7 @@ match["from_revision"] = hashutil.hash_to_hex(match["from_revision"]) del match["id"] results.append(OriginMetadataInfo(url=origin.url, metadata=match)) + return results diff --git a/swh/web/config.py b/swh/web/config.py --- a/swh/web/config.py +++ b/swh/web/config.py @@ -103,6 +103,7 @@ "json_path": "1.0/status/578e5eddcdc0cc7951000520", }, ), + "metadata_search_backend": ("string", "swh-indexer-storage"), # or "swh-search" } swhweb_config = {} # type: Dict[str, Any] diff --git a/swh/web/tests/api/views/test_origin.py b/swh/web/tests/api/views/test_origin.py --- a/swh/web/tests/api/views/test_origin.py +++ b/swh/web/tests/api/views/test_origin.py @@ -565,7 +565,11 @@ assert len(rv.data) == 1000 -def test_api_origin_metadata_search(api_client): +@pytest.mark.parametrize("backend", ["swh-search", "swh-indexer-storage"]) +def test_api_origin_metadata_search(api_client, mocker, backend): + + mock_config = mocker.patch("swh.web.common.archive.config") + mock_config.get_config.return_value = {"metadata_search_backend": backend} url = reverse( "api-1-origin-metadata-search", query_params={"fulltext": ORIGIN_METADATA_VALUE} diff --git a/swh/web/tests/data.py b/swh/web/tests/data.py --- a/swh/web/tests/data.py +++ b/swh/web/tests/data.py @@ -238,6 +238,7 @@ content_path = {} # Get all objects loaded into the test archive + metadata = {ORIGIN_METADATA_KEY: ORIGIN_METADATA_VALUE} for origin in _TEST_ORIGINS: snp = snapshot_get_latest(storage, origin["url"]) snapshots.add(hash_to_hex(snp.id)) @@ -251,10 +252,14 @@ id=origin["url"], from_revision=branch_data.target, indexer_configuration_id=idx_tool["id"], - metadata={ORIGIN_METADATA_KEY: ORIGIN_METADATA_VALUE}, + metadata=metadata, mappings=[], ) idx_storage.origin_intrinsic_metadata_add([origin_metadata]) + search.origin_update( + [{"url": origin["url"], "intrinsic_metadata": metadata}] + ) + ORIGIN_MASTER_REVISION[origin["url"]] = hash_to_hex( branch_data.target )