Skip to content

Commit

Permalink
Merge pull request #35 from AU-Biocommons/dev
Browse files Browse the repository at this point in the history
Better logging on search errors
  • Loading branch information
neoformit authored Jun 5, 2024
2 parents 906f0ba + a3a2b64 commit f8342cf
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 26 deletions.
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}")

0 comments on commit f8342cf

Please sign in to comment.