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

Better logging on search errors #35

Merged
merged 2 commits into from
Jun 5, 2024
Merged
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
2 changes: 1 addition & 1 deletion app/home/search/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class PageSchema(SchemaClass):
class Runserver:
"""Run Django development server to be queried by ElasticSearch."""

HOSTNAME = '127.0.0.1:8000'
HOSTNAME = '127.0.0.1:8001'
ARGS = (
sys.executable,
'manage.py',
Expand Down
59 changes: 34 additions & 25 deletions app/home/search/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,39 @@
]


class WhooshSearchError(Exception):
pass


def search(query):
ix = index.open_dir(settings.SEARCH_INDEX_DIR)
with ix.searcher() as searcher:
parser = MultifieldParser(
SEARCH_FIELDS,
schema=ix.schema,
group=OrGroup,
)
parsed_query = parser.parse(query)
hits = searcher.search(
parsed_query,
limit=None,
sortedby="",
terms=True,
)
fragmenter = highlight.ContextFragmenter(maxchars=100, surround=75)
hits.fragmenter = fragmenter
try:
ix = index.open_dir(settings.SEARCH_INDEX_DIR)
with ix.searcher() as searcher:
parser = MultifieldParser(
SEARCH_FIELDS,
schema=ix.schema,
group=OrGroup,
)
parsed_query = parser.parse(query)
hits = searcher.search(
parsed_query,
limit=None,
sortedby="",
terms=True,
)
fragmenter = highlight.ContextFragmenter(maxchars=100, surround=75)
hits.fragmenter = fragmenter

return [
{
"url": hit["url"],
"title": hit["title"],
"description": hit["description"],
"highlight": hit.highlights("body"),
}
for hit in hits
]
return [
{
"url": hit["url"],
"title": hit["title"],
"description": hit["description"],
"highlight": hit.highlights("body"),
}
for hit in hits
]
except Exception as exc:
raise WhooshSearchError(
f"Error searching index {settings.SEARCH_INDEX_DIR}\n"
f"Query: {query}\n{exc}")
Loading