From 7bdbbf10b208364f54006ceeae42e9f7b0ad6202 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Mon, 4 Jul 2022 04:32:27 +0000 Subject: [PATCH] Return search results directly from the server --- apiserver/reindex.py | 3 +-- apiserver/search.py | 20 +++++++++++--------- apiserver/server.py | 6 ++---- webclient/src/Results.js | 2 +- webclient/src/Search.js | 2 +- 5 files changed, 16 insertions(+), 17 deletions(-) diff --git a/apiserver/reindex.py b/apiserver/reindex.py index 27bc963..7a29bf9 100644 --- a/apiserver/reindex.py +++ b/apiserver/reindex.py @@ -59,8 +59,7 @@ if __name__ == '__main__': story = database.get_story(sid) print('Indexing {}/{} id: {} title: {}'.format(count, num_stories, sid[0], story.title)) story_obj = json.loads(story.meta_json) - to_add = dict(title=story_obj['title'], id=story_obj['id'], date=story_obj['date']) - stories.append(to_add) + stories.append(story_obj) count += 1 res = put_stories(stories) diff --git a/apiserver/search.py b/apiserver/search.py index 184c386..8f4a0a3 100644 --- a/apiserver/search.py +++ b/apiserver/search.py @@ -8,12 +8,15 @@ import settings SEARCH_ENABLED = bool(settings.MEILI_URL) -def meili_api(method, route, json=None, params=None): +def meili_api(method, route, json=None, params=None, parse_json=True): try: r = method(settings.MEILI_URL + route, json=json, params=params, timeout=4) if r.status_code > 299: raise Exception('Bad response code ' + str(r.status_code)) - return r.json() + if parse_json: + return r.json() + else: + return r.text except KeyboardInterrupt: raise except BaseException as e: @@ -29,9 +32,9 @@ def update_rankings(): return meili_api(requests.post, 'indexes/qotnews/settings/ranking-rules', json=json) def update_attributes(): - json = ['title'] + json = ['title', 'url', 'author'] r = meili_api(requests.post, 'indexes/qotnews/settings/searchable-attributes', json=json) - json = ['id'] + json = ['id', 'ref', 'source', 'author', 'author_link', 'score', 'date', 'title', 'link', 'url', 'num_comments'] r = meili_api(requests.post, 'indexes/qotnews/settings/displayed-attributes', json=json) return r @@ -45,18 +48,17 @@ def init(): def put_story(story): if not SEARCH_ENABLED: return - to_add = dict(title=story['title'], id=story['id'], date=story['date']) - return meili_api(requests.post, 'indexes/qotnews/documents', [to_add]) + return meili_api(requests.post, 'indexes/qotnews/documents', [story]) def search(q): if not SEARCH_ENABLED: return [] params = dict(q=q, limit=250) - r = meili_api(requests.get, 'indexes/qotnews/search', params=params) - return r['hits'] + r = meili_api(requests.get, 'indexes/qotnews/search', params=params, parse_json=False) + return r if __name__ == '__main__': init() print(update_rankings()) - print(search('qot')) + print(search('facebook')) diff --git a/apiserver/server.py b/apiserver/server.py index 52bd18f..d567229 100644 --- a/apiserver/server.py +++ b/apiserver/server.py @@ -56,10 +56,8 @@ def apisearch(): if len(q) >= 3: results = search.search(q) else: - results = [] - story_metas = [database.get_story(x['id']).meta_json for x in results] - # hacky nested json - res = Response('{"results":[' + ','.join(story_metas) + ']}') + results = '[]' + res = Response(results) res.headers['content-type'] = 'application/json' return res diff --git a/webclient/src/Results.js b/webclient/src/Results.js index 2f193fb..ea339fc 100644 --- a/webclient/src/Results.js +++ b/webclient/src/Results.js @@ -29,7 +29,7 @@ class Results extends React.Component { .then(res => res.json()) .then( (result) => { - this.setState({ stories: result.results }); + this.setState({ stories: result.hits }); }, (error) => { if (error.message !== 'The operation was aborted. ') { diff --git a/webclient/src/Search.js b/webclient/src/Search.js index e5d1d3d..80396c0 100644 --- a/webclient/src/Search.js +++ b/webclient/src/Search.js @@ -37,7 +37,7 @@ class Search extends Component {