From c403c8032cc139bb89f20860183ce5eed0c63267 Mon Sep 17 00:00:00 2001 From: Cameron Hyde Date: Wed, 5 Jun 2024 12:38:26 +1000 Subject: [PATCH 1/2] Better logging on search errors --- app/home/search/search.py | 59 ++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 25 deletions(-) diff --git a/app/home/search/search.py b/app/home/search/search.py index d09fa51..c1e0c31 100644 --- a/app/home/search/search.py +++ b/app/home/search/search.py @@ -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}") From a3a2b641bdcf74a4963bb3e6ffbc0a70c452dace Mon Sep 17 00:00:00 2001 From: Cameron Hyde Date: Wed, 5 Jun 2024 14:11:02 +1000 Subject: [PATCH 2/2] Set Runserver port to 8001 to avoid conflict --- app/home/search/index.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/home/search/index.py b/app/home/search/index.py index b0b3a36..725f815 100644 --- a/app/home/search/index.py +++ b/app/home/search/index.py @@ -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',