-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Suggestions - Related search (fuzzy) & Did you mean
- Loading branch information
1 parent
6322e0a
commit 44f8051
Showing
7 changed files
with
104 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,15 @@ | ||
from elasticsearch import Elasticsearch | ||
|
||
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" ] } } } }' | ||
|
||
def es_suggestion(search_string): | ||
return '{"suggest":{"didYouMean":{"text":"%s","phrase":{"field":"did_you_mean"}}},"query":{"match":{"question":"%s"}}}' % (search_string, search_string) | ||
return '{"suggest":{"didYouMean":{"text":"%s","phrase":{"field":"did_you_mean"}}},"query":{"match":{"question":"%s"}}}' % (search_string, search_string) | ||
|
||
def es_fuzzy_match(search_string): | ||
return '{"query":{"match":{"question":{"query":"%s","fuzziness":3,"prefix_length":2}}}}' % search_string | ||
|
||
|
||
def es_builder(hosts=None): | ||
if (hosts == None): hosts = '127.0.0.1' | ||
return Elasticsearch(hosts=hosts) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from opencricket.config import es_config | ||
|
||
|
||
class Suggestions: | ||
def __init__(self, es_host=None): | ||
self.es = es_config.es_builder(es_host) | ||
|
||
def suggestions(self, search_string): | ||
return self.es.search(index='opencricket', body=es_config.es_suggestion(search_string)) | ||
|
||
def first_suggestion(self, search_string): | ||
suggestions = self.suggestions(search_string) | ||
hits = suggestions['hits']['hits'] | ||
if len(hits) > 0: | ||
return hits[0]['_source']['question'] | ||
else: | ||
return None | ||
|
||
def related_search(self, search_string): | ||
fuzzy_matches = self.es.search(index='opencricket', body=es_config.es_fuzzy_match(search_string)) | ||
hits = fuzzy_matches['hits']['hits'] | ||
if len(hits) > 0: | ||
return [match['_source']['question'] for match in hits] | ||
else: | ||
return None | ||
|
||
def did_you_mean(self, search_string): | ||
suggestions = self.suggestions(search_string) | ||
did_you_mean_options = suggestions['suggest']['didYouMean'][0]['options'] | ||
if len(did_you_mean_options) > 0: | ||
return [dym['text'] for dym in did_you_mean_options] | ||
else: | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import unittest | ||
import json | ||
import flaskr | ||
|
||
class TestSearch(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.app = flaskr.app.test_client() | ||
self.expected_response = json.loads('{"suggested": false, "root": "player_stats", "player_stats": {"word_stats": "stats", "player": {"player1": "virat", "player2": "kohli"}}}') | ||
|
||
def test_search(self): | ||
rv = self.app.get('/?search=Virat Kohli Stats') | ||
self.assertEqual(rv._status_code, 200) | ||
self.assertEqual(json.loads(rv.data.decode("utf-8")), self.expected_response) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() | ||
|