def define_autocomplete_dictionary(): """Creates an autocomplete search dictionary for `name`. Also creats autocomplete dictionary for `categories` if checked in E Commerce Settings""" cache = frappe.cache() name_ac = AutoCompleter(make_key(WEBSITE_ITEM_NAME_AUTOCOMPLETE), conn=cache) cat_ac = AutoCompleter(make_key(WEBSITE_ITEM_CATEGORY_AUTOCOMPLETE), conn=cache) ac_categories = frappe.db.get_single_value( 'E Commerce Settings', 'show_categories_in_search_autocomplete') # Delete both autocomplete dicts try: cache.delete(make_key(WEBSITE_ITEM_NAME_AUTOCOMPLETE)) cache.delete(make_key(WEBSITE_ITEM_CATEGORY_AUTOCOMPLETE)) except Exception: return False items = frappe.get_all('Website Item', fields=['web_item_name', 'item_group'], filters={"published": 1}) for item in items: name_ac.add_suggestions(Suggestion(item.web_item_name)) if ac_categories and item.item_group: cat_ac.add_suggestions(Suggestion(item.item_group)) return True
def insert(): # insertion of search/suggestion data suggestion_client = Client('movie') suggestion_client.create_index([TextField('title'), TagField('genres', separator = '|')]) for i in range(0, len(movie_df)): suggestion_client.add_document(movie_df['tmdbId'][i], title = movie_df['title'][i], genres = movie_df['genres'][i]) # insertion of auto-completion data completion_client = AutoCompleter('ac') for i in range(0, len(movie_df)): completion_client.add_suggestions(Suggestion(movie_df['title'][i]))
class TAS_Autocompleter: #constructor def __init__(self, autocomplete_name, host='localhost', port=6379): try: self.host = host self.port = port self.table_name = autocomplete_name self.ac = AutoCompleter(autocomplete_name, host, port) except Exception as e: print >> sys.stderr, "TAS_Autocompleter Error inside constructor Index:\'", self.table_name, "\' HOST:\'", self.host, "\' PORT:\'", self.port, "\'\n" print >> sys.stderr, e #will add the list to auto-complete entries in the respective name def add_to_autocomplete(self, new_words): try: for word in new_words: self.ac.add_suggestions(Suggestion(word, 1.0)) except Exception as e: print >> sys.stderr, "TAS_Autocompleter Error inside add_to_autocomplete Index:\'", self.table_name, "\' HOST:\'", self.host, "\' PORT:\'", self.port, "\'\n" print >> sys.stderr, e #not working as is intented, researching more... #btw, will add all the words in autocomplete dictionary def add_to_autocomplete_combination(self, new_words): try: for word in new_words: splitted = word.split(' ') splittedLength = len(splitted) for index in range(0, splittedLength): toAdd = ' '.join(splitted[index:splittedLength]) self.ac.add_suggestions(Suggestion(toAdd, 1.0)) except Exception as e: print >> sys.stderr, "TAS_Autocompleter Error inside add_to_autocomplete_combination Index:\'", self.table_name, "\' HOST:\'", self.host, "\' PORT:\'", self.port, "\'\n" print >> sys.stderr, e #will return auto-suggestions for the give prefix def suggest(self, prefix): try: return self.ac.get_suggestions(prefix, fuzzy=True) except Exception as e: print >> sys.stderr, "TAS_Autocompleter Error inside suggest Index:\'", self.table_name, "\' HOST:\'", self.host, "\' PORT:\'", self.port, "\'\n" print >> sys.stderr, e def delete(self, prefix): try: return self.ac.delete(prefix) except Exception as e: print >> sys.stderr, "TAS_Autocompleter Error inside delete Index:\'", self.table_name, "\' HOST:\'", self.host, "\' PORT:\'", self.port, "\'\n" print >> sys.stderr, e
def post(self): mydata = request.json location = str(mydata['location']) ac = AutoCompleter(current_app.config["REDISSEARCH_INDEX"], current_app.config["REDISSEARCH_URI"]) res = ac.add_suggestions(Suggestion(location, 1.0), increment = False) data = {'msg': res} return data, 200
class TTAutoCompleter: def __init__(self, host=ip, port=port, db=db, autocomplete_name='Default'): self.ipAdd = host self.ipPort = port self.db = db self.autocomplete = AutoCompleter(autocomplete_name, host, port) def create_auto_complete(self, res): for word in res: self.autocomplete.add_suggestions(Suggestion(word, 1.0)) def suggest(self, keyword): return self.autocomplete.get_suggestions(keyword) #return self.autocomplete.get_suggestions(keyword,fuzzy=True) def delete_auto_complete(self, key): self.autocomplete.delete(key)
class SuggestionUpdater: def __init__(self, args): self.host = args.host self.port = args.port self.key = args.key self.file = args.file self.rows = args.rows self.client = AutoCompleter(self.key, self.host, self.port) def loadFile(self): suggs = [] with open(self.file) as fp: for cnt, line in enumerate(fp): line = line.strip() print("Line {}: {}".format(cnt, line)) suggs.append(Suggestion(line, '1')) if cnt == self.rows - 1: break self.client.add_suggestions(*suggs) print('Finished loading ' + str(cnt) + ' rows.')
class CacheEngine: def __init__(self, hostname: str, idx_name: str, port=6379) -> None: self._ready = False self._setup_client(hostname, idx_name, port) def _setup_client(self, hostname: str, idx_name: str, port=6379) -> None: try: self._client = Client(idx_name, host=hostname, port=port) self._auto_compl = AutoCompleter(idx_name, hostname, port=port) self._hostname = hostname self._port = port self._idx = idx_name self._ready = True LOGGER.info("Cache engine is ready") except: self._client = None LOGGER.error("Cache engine is faulty!") def add_doc(self, doc_id: str, data: dict) -> Any: if dict is None: return False results = self._client.redis.hset(doc_id, mapping=data) return results def search(self, text_to_search: str) -> Result: results: Result = self._client.search(text_to_search) return results def get_doc(self, doc_id) -> Document: try: data = self._client.load_document(doc_id) return data except: return None def add_suggestion(self, suggestion) -> bool: results = None try: results = self._auto_compl.add_suggestions(Suggestion(suggestion)) except: return False return True def get_suggestion(self, str_to_suggest: str) -> List: suggs = self._auto_compl.get_suggestions(str_to_suggest, fuzzy=len(str_to_suggest) > 3) return suggs
def create_product_autocompleter(): auto_completer = AutoCompleter('productAutocompleter') products = Product.objects.filter(active=True) for product in products: title = product.name category = product.category.name auto_completer.add_suggestions(Suggestion(title, 5.0), Suggestion(category, 3.0)) for tag in Tag.objects.all(): auto_completer.add_suggestions(Suggestion(tag.tag, 5.0)) for cv in CategoryVarient.objects.all(): auto_completer.add_suggestions(Suggestion(cv.value, 2.0)) return True
from redisearch import TextField, NumericField, Query, AutoCompleter, Suggestion ac = AutoCompleter('ac', 'redis-search') ac.add_suggestions(Suggestion('google', 5.0), Suggestion('goo', 1.0)) f = open("location-cnt.txt", "r") for line in f: keywords = line.split(" ") keywords[1] = keywords[1].replace("_", " ") ac.add_suggestions(Suggestion(keywords[1].rstrip("\n"), float(keywords[0])))
def insert_to_name_ac(web_name, doc_name): ac = AutoCompleter(make_key(WEBSITE_ITEM_NAME_AUTOCOMPLETE), conn=frappe.cache()) ac.add_suggestions(Suggestion(web_name, payload=doc_name))
def load_data(redis_server, redis_port, redis_password): load_client = Client( 'fortune500-v1', host=redis_server, password=redis_password, port=redis_port ) load_ac = AutoCompleter( 'ac', conn = load_client.redis ) definition = IndexDefinition( prefix=['fortune500:'], language='English', score_field='title', score=0.5 ) load_client.create_index( ( TextField("title", weight=5.0), TextField('website'), TextField('company'), NumericField('employees', sortable=True), TextField('industry', sortable=True), TextField('sector', sortable=True), TextField('hqcity', sortable=True), TextField('hqstate', sortable=True), TextField('ceo'), TextField('ceoTitle'), NumericField('rank', sortable=True), NumericField('assets', sortable=True), NumericField('revenues', sortable=True), NumericField('profits', sortable=True), NumericField('equity', sortable=True), TagField('tags'), TextField('ticker') ), definition=definition) with open('./fortune500.csv', encoding='utf-8') as csv_file: csv_reader = csv.reader(csv_file, delimiter=',') line_count = 0 for row in csv_reader: if line_count > 0: load_ac.add_suggestions(Suggestion(row[1].replace('"', ''), 1.0)) load_client.redis.hset( "fortune500:%s" %(row[1].replace(" ", '')), mapping = { 'title': row[1], 'company': row[1], 'rank': row[0], 'website': row[2], 'employees': row[3], 'sector': row[4], 'tags': ",".join(row[4].replace('&', '').replace(',', '').replace(' ', ' ').split()).lower(), 'industry': row[5], 'hqcity': row[8], 'hqstate': row[9], 'ceo': row[12], 'ceoTitle': row[13], 'ticker': row[15], 'revenues': row[17], 'profits': row[19], 'assets': row[21], 'equity': row[22] }) line_count += 1 # Finally Create the alias load_client.aliasadd("fortune500")