Skip to content

Commit 6f8fa0d

Browse files
committed
feat(cli): add --meta flag to search command to include metadata in results
Add optional --meta/-m flag to the search command that retrieves and augments matched results with asset metadata using the index get method. When enabled, the flag fetches full asset details for each match and includes the metadata field in the JSON output if available.
1 parent 02dcf12 commit 6f8fa0d

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

iscc_search/cli/__init__.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ def get(
309309
def search(
310310
iscc_code, # type: str
311311
limit=typer.Option(3, "--limit", "-l", help="Maximum number of results"), # type: int
312+
meta: bool = typer.Option(False, "--meta", "-m", help="Include metadata for matched results"),
312313
):
313314
# type: (...) -> None
314315
"""
@@ -318,7 +319,7 @@ def search(
318319
319320
Example:
320321
iscc-search search ISCC:KECYCMZIOY36XXGZ7S6QJQ2AEEXPOVEHZYPK6GMSFLU3WF54UPZMTPY
321-
iscc-search search ISCC:KEC... --limit 10
322+
iscc-search search ISCC:KEC... --limit 10 --meta
322323
"""
323324
from iscc_search.schema import IsccAsset
324325

@@ -345,21 +346,35 @@ def search(
345346
results = index.search_assets("default", query, limit=int(limit))
346347
progress.remove_task(task)
347348

349+
# Build matches output
350+
matches_output = []
351+
for match in results.matches:
352+
match_dict = {
353+
"iscc_id": match.iscc_id,
354+
"score": match.score,
355+
"matches": match.matches,
356+
}
357+
358+
# If --meta flag is set, retrieve and add metadata
359+
if meta:
360+
try:
361+
asset = index.get_asset("default", match.iscc_id)
362+
if asset.metadata:
363+
match_dict["metadata"] = asset.metadata
364+
except FileNotFoundError:
365+
# Asset not found, skip metadata
366+
pass
367+
368+
matches_output.append(match_dict)
369+
348370
# Close index
349371
index.close()
350372

351373
# Output as JSON
352374
output = {
353375
"query": {"iscc_code": results.query.iscc_code, "units": results.query.units},
354376
"metric": results.metric,
355-
"matches": [
356-
{
357-
"iscc_id": match.iscc_id,
358-
"score": match.score,
359-
"matches": match.matches,
360-
}
361-
for match in results.matches
362-
],
377+
"matches": matches_output,
363378
}
364379

365380
console.print_json(json.dumps(output))

0 commit comments

Comments
 (0)