Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add CLI for testing search queries #105

Merged
merged 7 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
20 changes: 10 additions & 10 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ black = "^24.2.0"
moto = { extras = ["s3"], version = "^5.0.13" }
pytest-dotenv = "^0.5.2"

[tool.poetry.group.cli]
optional = true

[tool.poetry.group.cli.dependencies]
typer = "^0.12.5"
rich = "^13.8.1"

[tool.pytest.ini_options]
addopts = "-p no:cacheprovider"
Expand Down
80 changes: 80 additions & 0 deletions src/cpr_sdk/cli/run_search_query.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import json
from rich.console import Console
from rich.table import Table
from rich import print_json
from rich import print as rprint
import typer
from src.cpr_sdk.search_adaptors import VespaSearchAdapter
from src.cpr_sdk.models.search import SearchParameters
from tests.conftest import VESPA_TEST_SEARCH_URL


def main(
instance_url: str = VESPA_TEST_SEARCH_URL,
exact_match: bool = False,
limit: int = 10,
):
"""Run a search query with different rank profiles."""
console = Console()
search_adapter = VespaSearchAdapter(instance_url)

while True:
query = input("Enter your search query (or 'q' to quit): ")
if query.lower() == "q":
break

search_parameters = SearchParameters(
query_string=query, exact_match=exact_match, limit=limit
)
search_response = search_adapter.search(search_parameters)

for family in search_response.families:
family_data = family.hits[0].model_dump()
console.rule(
title=f"{family_data['family_name']} ({family_data['family_geography']} ,{family_data['family_import_id']})"
)
print_json(
json.dumps(
{
k: v
for k, v in family_data.items()
if not k.startswith("text_block") and "metadata" not in k
},
default=str,
)
)

# There's some typing weirdness going on here:
# hasattr(family.hits[0], 'text_blocks') can be False, but
# family.hits[0].text_block exists
try:
rprint("Text blocks:")
table = Table(title="Hits Table")

# Add columns to the table
table.add_column(
"Text Block ID", justify="right", style="cyan", no_wrap=True
)
table.add_column("Text Block", style="magenta")

# Add rows to the table
for hit in family.hits:
try:
table.add_row(f"{hit.text_block_id}", f"{hit.text_block}") # type: ignore
except Exception:
pass

# Print the table
console.print(table)

if family == search_response.families[-1]:
print("No more families to show.")
break
except AttributeError:
print("No text blocks found.")

input("Press any key to show next family")


if __name__ == "__main__":
typer.run(main)
Loading