def _indexKeywordsForCompletion(self, site_id, keywords): res = self.es.indices.analyze(index=es_search_functions.getESItemIndexName(site_id), text=" ".join(keywords), analyzer="mycn_analyzer_whitespace_pinyin_first_n_full") for token_idx in range(len(res["tokens"])): token = res["tokens"][token_idx] raw_keyword = keywords[token_idx] splitted_token = token["token"].split("||") first_letters = splitted_token[0] full_pinyin = "".join(splitted_token[1:]) result = {"keyword_completion": {"input": [raw_keyword, full_pinyin, first_letters], "output": raw_keyword}} self.es.index(index=es_search_functions.getESItemIndexName(site_id), doc_type='keyword', body=result)
def es_index_item(site_id, item): es = Elasticsearch() fill_keywords(site_id, item) item["item_name_standard_analyzed"] = item["item_name"] item["item_name_no_analysis"] = item["item_name"] item["item_name"] = " ".join(es_search_functions.preprocess_query_str(item["item_name"])) item["tags_standard"] = item["tags"] if item.has_key("price"): item["price"] = float(item["price"]) if item.has_key("market_price"): item["market_price"] = float(item["market_price"]) if item.has_key("_id"): del item["_id"] if item.has_key("created_on"): del item["created_on"] if item.has_key("updated_on"): del item["updated_on"] if item.has_key("item_spec") and item['item_spec']: item['item_spec_ng'] = es_search_functions.strip_item_spec(item['item_spec']) #if item.has_key("origin_place"): # item["origin_place"] = str(item["origin_place"]) item["categories"] = preprocess_categories(item["categories"]) brand = item.get("brand", None) if brand: item["brand"] = brand["id"] item["brand_name"] = brand.get("name", "") res = es.index(index=es_search_functions.getESItemIndexName(site_id), doc_type='item', id=item["item_id"], body=item)
def create_es_item_index(es, site_id): item_index = es_search_functions.getESItemIndexName(site_id) res = es.indices.create(index=item_index, body={ "mappings": MAPPINGS, "settings": INDEX_SETTINGS })
def getTopN(self, site_id, args): keywords = args.get("keywords") if not keywords: return [] try: amount = int(args.get("amount", "5")) except ValueError: raise ArgumentError("amount should be an integer.") s = S().indexes(es_search_functions.getESItemIndexName(site_id)).doctypes("item") query = es_search_functions.construct_or_query(keywords, delimiter=",") s = s.query_raw(query) s = s.filter(available=True) topn = [(item["item_id"], 1) for item in s[:amount]] return topn
def getTopN(self, site_id, args): keywords = args.get("keywords") if not keywords: return [] try: amount = int(args.get("amount", "5")) except ValueError: raise ArgumentError("amount should be an integer.") s = S().indexes( es_search_functions.getESItemIndexName(site_id)).doctypes("item") query = es_search_functions.construct_or_query(keywords, delimiter=",") s = s.query_raw(query) s = s.filter(available=True) topn = [(item["item_id"], 1) for item in s[:amount]] return topn
def es_index_item(site_id, item): es = Elasticsearch() fill_keywords(site_id, item) item["item_name_standard_analyzed"] = item["item_name"] item["item_name_no_analysis"] = item["item_name"] item["item_name"] = " ".join( es_search_functions.preprocess_query_str(item["item_name"])) item["tags_standard"] = item["tags"] if item.has_key("price"): item["price"] = float(item["price"]) if item.has_key("market_price"): item["market_price"] = float(item["market_price"]) if item.has_key("_id"): del item["_id"] if item.has_key("created_on"): del item["created_on"] if item.has_key("updated_on"): del item["updated_on"] if item.has_key("item_spec") and item['item_spec']: item['item_spec_ng'] = es_search_functions.strip_item_spec( item['item_spec']) #if item.has_key("origin_place"): # item["origin_place"] = str(item["origin_place"]) item["categories"] = preprocess_categories(item["categories"]) brand = item.get("brand", None) if brand: item["brand"] = brand["id"] item["brand_name"] = brand.get("name", "") res = es.index(index=es_search_functions.getESItemIndexName(site_id), doc_type='item', id=item["item_id"], body=item)
def refreshSiteItemIndex(self, site_id): self.es.indices.refresh(index=es_search_functions.getESItemIndexName(site_id))
def create_es_item_index(es, site_id): item_index = es_search_functions.getESItemIndexName(site_id) res = es.indices.create( index=item_index, body={"mappings": MAPPINGS, "settings": INDEX_SETTINGS})
def drop_es_item_index(es, site_id): item_index = es_search_functions.getESItemIndexName(site_id) try: es.indices.delete(item_index) except NotFoundError: pass
def refreshSiteItemIndex(self, site_id): self.es.indices.refresh( index=es_search_functions.getESItemIndexName(site_id))