From 6322e0a14507c3b2969b7568afbbb783815ceab9 Mon Sep 17 00:00:00 2001 From: Lenin Raj Rajasekaran Date: Sat, 7 Mar 2015 11:47:50 +0530 Subject: [PATCH] Suggestion endpoint; from elasticsearch --- flaskr.py | 10 +++++++++- opencricket/config/es_config.py | 5 ++++- opencricket/suggestion/productions.py | 3 +++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/flaskr.py b/flaskr.py index 2c3440a..3253772 100644 --- a/flaskr.py +++ b/flaskr.py @@ -37,6 +37,11 @@ def production(): r.mimetype = 'application/json' return r + +@app.route("/suggestions") +def suggestions(): + return json_response(Productions().suggestions(request.args.get('search', ''))) + @app.route("/load_index") def load_index(): r = make_response(Productions().load_index(config.exploded_dir)) @@ -58,7 +63,10 @@ def ping(): return ok() def ok(): - r = make_response(json.dumps({'status': 'ok'})) + return json_response({'status': 'ok'}) + +def json_response(dict_response): + r = make_response(json.dumps(dict_response)) r.mimetype = 'application/json' return r diff --git a/opencricket/config/es_config.py b/opencricket/config/es_config.py index f4a1357..a07fbc1 100644 --- a/opencricket/config/es_config.py +++ b/opencricket/config/es_config.py @@ -1,2 +1,5 @@ index_settings='{ "settings": { "index": { "analysis": { "filter": { "stemmer": { "type": "stemmer", "language": "english" }, "autocompleteFilter": { "max_shingle_size": "5", "min_shingle_size": "2", "type": "shingle" }, "stopwords": { "type": "stop", "stopwords": [ "_english_" ] } }, "analyzer": { "didYouMean": { "filter": [ "lowercase" ], "char_filter": [ "html_strip" ], "type": "custom", "tokenizer": "standard" }, "autocomplete": { "filter": [ "lowercase", "autocompleteFilter" ], "char_filter": [ "html_strip" ], "type": "custom", "tokenizer": "standard" }, "default": { "filter": [ "lowercase", "stopwords", "stemmer" ], "char_filter": [ "html_strip" ], "type": "custom", "tokenizer": "standard" } } } } } }' -mapping='{ "player_stats": { "properties": { "autocomplete": { "type": "string", "analyzer": "autocomplete" }, "did_you_mean": { "type": "string", "analyzer": "didYouMean" }, "question": { "type": "string", "copy_to": [ "autocomplete", "did_you_mean" ] } } } }' \ No newline at end of file +mapping='{ "player_stats": { "properties": { "autocomplete": { "type": "string", "analyzer": "autocomplete" }, "did_you_mean": { "type": "string", "analyzer": "didYouMean" }, "question": { "type": "string", "copy_to": [ "autocomplete", "did_you_mean" ] } } } }' + +def es_suggestion(search_string): + return '{"suggest":{"didYouMean":{"text":"%s","phrase":{"field":"did_you_mean"}}},"query":{"match":{"question":"%s"}}}' % (search_string, search_string) \ No newline at end of file diff --git a/opencricket/suggestion/productions.py b/opencricket/suggestion/productions.py index cc3c994..acadf38 100644 --- a/opencricket/suggestion/productions.py +++ b/opencricket/suggestion/productions.py @@ -68,6 +68,9 @@ def explode(self, expansions_dir, exploded_dir): f.write('\n'.join([tmp % a for a in list(product(*final_items))]) + '\n') + def suggestions(self, search_string): + return self.es.search(index='opencricket', body=es_config.es_suggestion(search_string)) + def create_index(self): self.es.indices.create(index='opencricket', body=es_config.index_settings) self.es.indices.put_mapping(index='opencricket', doc_type='player_stats', body=es_config.mapping)