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

fix: user can check --help even without valid URI #5

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
19 changes: 12 additions & 7 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@

from dotenv import load_dotenv
from opensearchpy import OpenSearch
from customized_exceptions import InvalidServiceURI

load_dotenv()

INDEX_NAME = "epicurious-recipes"
SERVICE_URI = os.getenv("SERVICE_URI")
if SERVICE_URI == "https://user:pass@hostname:port" or SERVICE_URI is None:
print(f"Update SERVICE_URI to your cluster uri. Current value for SERVICE_URI={SERVICE_URI}")
exit(-1)
INDEX_NAME = "recipes"

client = OpenSearch(SERVICE_URI, use_ssl=True)

def create_client():
"""Create OpenSearch client."""
load_dotenv()
SERVICE_URI = os.getenv("SERVICE_URI")
if SERVICE_URI == "https://user:pass@hostname:port" or SERVICE_URI is None:
raise InvalidServiceURI(
f"Update SERVICE_URI to your cluster uri. Current value for SERVICE_URI={SERVICE_URI}"
)
return OpenSearch(SERVICE_URI, use_ssl=True)
6 changes: 5 additions & 1 deletion index.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import typer
from opensearchpy import NotFoundError, helpers, OpenSearch

from config import INDEX_NAME, client
from config import INDEX_NAME, create_client


app = typer.Typer()
Expand All @@ -34,26 +34,30 @@ def load_data():

data = load_data()
print(f"Ingesting {INDEX_NAME} data")
client = create_client()
response = helpers.bulk(client, data)
print(f"Data sent to your OpenSearch with response: {response}")


@app.command("delete-index")
def delete_index(index_name=INDEX_NAME):
"""Delete all the documents of certain index name, and do not raise exceptions"""
client = create_client()
client.indices.delete(index=index_name, ignore=[400, 404])


@app.command("get-cluster-info")
def get_cluster_info():
"""Get information about your OpenSearch cluster"""
client = create_client()
return pprint(OpenSearch.info(client), width=100, indent=1)


@app.command("get-mapping")
def get_mapping():
"""Retrieve mapping for the index.
The mapping lists all the fields and their data types."""
client = create_client()

# list of all the cluster's indices
indices = client.indices.get_alias("*").keys()
Expand Down
11 changes: 10 additions & 1 deletion search.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@
"""
import typer

from config import INDEX_NAME, SERVICE_URI, client
from config import INDEX_NAME, create_client
from helpers import log_titles
from typing import List

app = typer.Typer()


@app.command("match")
def search_match(field: str, query: str, operator: str = "or") -> None:
"""Perform search by relevance for certain field and query."""
typer.echo(f"Searching for {query} in the field {field} \n")
query_body = {"query": {"match": {field: {"query": query, "operator": operator}}}}
client = create_client()
resp = client.search(index=INDEX_NAME, body=query_body)
log_titles(resp)

Expand All @@ -29,6 +31,7 @@ def search_multi_match(fields: List[str], query: str) -> None:
"""Perform search by relevance for certain field and query."""
typer.echo(f"Searching for {query} in the field {fields} \n")
query_body = {"query": {"multi_match": {"query": query, "fields": fields}}}
client = create_client()
resp = client.search(index=INDEX_NAME, body=query_body)
log_titles(resp)

Expand All @@ -38,6 +41,7 @@ def search_match_phrase(field, query):
"""Search by match phrase for specific phrases in a field."""
typer.echo(f"Searching for {query} in the field {field}")
query_body = {"query": {"match_phrase": {field: {"query": query}}}}
client = create_client()
resp = client.search(index=INDEX_NAME, body=query_body)
log_titles(resp)

Expand All @@ -47,6 +51,7 @@ def search_range(field: str, gte, lte) -> None:
"""Search by specifying a range of values for a field"""
typer.echo(f"Searching for values in the {field} ranging from {gte} to {lte} \n")
query_body = {"query": {"range": {field: {"gte": gte, "lte": lte}}}}
client = create_client()
resp = client.search(index=INDEX_NAME, body=query_body)
log_titles(resp)

Expand All @@ -67,6 +72,7 @@ def search_fuzzy(field, value, fuzziness) -> None:
}
}
}
client = create_client()
resp = client.search(index=INDEX_NAME, body=query_body)
log_titles(resp)

Expand All @@ -85,6 +91,7 @@ def search_query_string(field: str, query: str, size: int) -> None:
}
}
}
client = create_client()
resp = client.search(index=INDEX_NAME, body=query_body, size=size)
log_titles(resp)

Expand All @@ -94,6 +101,7 @@ def search_slop(field, query, slop):
"""Search by specifying a slop - a distance between search word"""
typer.echo(f"Searching for {query} with slop value {slop} in the field {field}")
query_body = {"query": {"match_phrase": {field: {"query": query, "slop": slop}}}}
client = create_client()
resp = client.search(index=INDEX_NAME, body=query_body)
log_titles(resp)

Expand All @@ -113,6 +121,7 @@ def search_combined_queries():
}
}
}
client = create_client()
resp = client.search(index=INDEX_NAME, body=query_body)
log_titles(resp)

Expand Down