Skip to content

Commit

Permalink
Suggestion endpoint; from elasticsearch
Browse files Browse the repository at this point in the history
  • Loading branch information
emaillenin committed Mar 7, 2015
1 parent 742b900 commit 6322e0a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
10 changes: 9 additions & 1 deletion flaskr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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

Expand Down
5 changes: 4 additions & 1 deletion opencricket/config/es_config.py
Original file line number Diff line number Diff line change
@@ -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" ] } } } }'
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)
3 changes: 3 additions & 0 deletions opencricket/suggestion/productions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 6322e0a

Please sign in to comment.