From c4f89d79f22d0e44b025cb2785ea8fd322c0fe43 Mon Sep 17 00:00:00 2001 From: neodarz Date: Sun, 20 Jan 2019 10:46:13 +0100 Subject: Use Manticorsearch JSON API instead of custom search API --- src/khanindexer-nevrax.py | 38 ++++++++++++++++++++++++-------------- src/khanindexer.py | 37 ++++++++++++++++++++++++------------- 2 files changed, 48 insertions(+), 27 deletions(-) (limited to 'src') diff --git a/src/khanindexer-nevrax.py b/src/khanindexer-nevrax.py index e5dc66d..326c95c 100644 --- a/src/khanindexer-nevrax.py +++ b/src/khanindexer-nevrax.py @@ -15,18 +15,28 @@ paging = False language_support = False number_of_results = 5 -# search-url -base_url = 'http://127.0.0.1:5000/?index=nevrax' -search_url = '&{query}' - - # do search-request def request(query, params): - params['url'] = base_url +\ - search_url.format(query=urlencode({'search': query})) + params['data'] = json.dumps({ + "index": "nevrax", + "query": { "match": { "_all": query } }, + "highlight": + { + "fields": { + "content": {}, + "url": {}, + "title": {} + }, + "pre_tags": "_", + "post_tags": "_" + } + }) + + params['method'] = 'POST' + + params['url'] = 'http://127.0.0.1:8080/json/search' - print(params['url']) return params @@ -37,14 +47,14 @@ def response(resp): datas = json.loads(resp.text) if not 'error' in datas: - for el in datas: - results.append({ - 'title': el['title'], - 'content': el['excerpts'][0], - 'url': el['url'] + for el in datas['hits']['hits']: + results.append({ + 'title': el["_source"]["title"], + 'content': el["highlight"]["content"][0], + 'url': el["_source"]["url"] }) else: - print("ERROR:Search server: "+str(datas['msg'])) + print("ERROR:Search server: "+str(datas['error'])) # return results return results diff --git a/src/khanindexer.py b/src/khanindexer.py index 05a5602..36dd1ef 100644 --- a/src/khanindexer.py +++ b/src/khanindexer.py @@ -15,16 +15,27 @@ paging = False language_support = False number_of_results = 5 -# search-url -base_url = 'http://127.0.0.1:5000/?index=neodarznet' -search_url = '&{query}' - - # do search-request def request(query, params): - params['url'] = base_url +\ - search_url.format(query=urlencode({'search': query})) + params['data'] = json.dumps({ + "index": "neodarznet", + "query": { "match": { "_all": query } }, + "highlight": + { + "fields": { + "content": {}, + "url": {}, + "title": {} + }, + "pre_tags": "_", + "post_tags": "_" + } + }) + + params['method'] = 'POST' + + params['url'] = 'http://127.0.0.1:8080/json/search' return params @@ -36,14 +47,14 @@ def response(resp): datas = json.loads(resp.text) if not 'error' in datas: - for el in datas: - results.append({ - 'title': el['title'], - 'content': el['excerpts'][0], - 'url': el['url'] + for el in datas['hits']['hits']: + results.append({ + 'title': el["_source"]["title"], + 'content': el["highlight"]["content"][0], + 'url': el["_source"]["url"] }) else: - print("ERROR:Search server: "+str(datas['msg'])) + print("ERROR:Search server: "+str(datas['error'])) # return results return results -- cgit v1.2.1