def score_listing(listing): """Calculate and save a listing's score.""" user_weight = app.config.get("BT_USER_WEIGHT") gold_bonus = app.config.get("BT_GOLD_BONUS") age_pivot = app.config.get("BT_AGE_PIVOT") # Age is expressed in days age = (time.time() - float(listing["original_creation_tsz"])) / (60 * 60 * 24) # One day in seconds score = ( (listing["users"] + (gold_bonus if "gold" in listing["materials"] else 0)) * user_weight * abs(1 - (age / age_pivot)) ) / (float(listing["views"]) * float(listing["quantity"]) + 1) r.zadd("treasures", listing["listing_id"], score)
def setup_method(self, method): app.testing = True self.client = app.test_client() for i in range(1000): r.zadd('treasures', i, i) r.set('listings.%s.data' % i, json.dumps({ 'listing_id': i, 'quantity': i, 'materials': [], 'views': i, 'url': 'http://google.com', 'title': 'HEllo', 'Images': [ {'url_170x135': 'http://google.com'}, ], 'price': 123, 'currency_code': 'USD', 'Shop': {'shop_name': 'Heollo', 'url': 'http://google.com'}, 'users': 10, }))
def store_fake_data(listing_id, score=9000): """Stores fake listing data for listing_id.""" r.set('listings.%s.data' % listing_id, '{"hello": "there"}') r.sadd('listings.%s.users' % listing_id, '999') r.zadd('treasures', listing_id, score)
def search(): search = request.args.get("search") id = request.cookies.get("id") if search: r.zadd(id, {search: int(round(time.time() * 1000000))}) page_num = request.args.get("page") if not page_num: page_num = 1 page_num = int(page_num) body = { "query": { "bool": { "should": [ { "match": { "title.spy": search } }, { "match": { "summary.spy": search }}, { "match": { "title": search } }, { "match": { "summary": search }}, { "match_phrase": { "title": { "query": search, "analyzer": "ik_sync_smart" } } }, { "match_phrase": { "summary": { "query": search, "analyzer": "ik_sync_smart" } } }, { "match": { "title.fpy": search } }, { "match": { "summary.fpy": search }} ]} }} count = es.search(index="polycies", body=body, filter_path=["hits"],timeout="60s") count = count.get("hits").get("total") if count == 0: return render_template("v1/no_search.html") else: if count > 10000: count = 10000 body = { "query": { "bool": { "should": [ { "match": { "title.spy": search } }, { "match": { "summary.spy": search }}, { "match_phrase": { "title": { "query": search, "analyzer": "ik_sync_smart" } } }, { "match_phrase": { "summary": { "query": search, "analyzer": "ik_sync_smart" } } }, { "match": { "title.fpy": search } }, { "match": { "summary.fpy": search }} ]} }, "size": 10, "from": (page_num - 1) * 10, "highlight": { "pre_tags": "<b style='color:red'>", "post_tags": "</b>", "fields": [{ "title.fpy": {} }, { "title.spy": {} }, { "summary.spy": {} }, { "summary.spy": {} }, { "title": {} }, { "summary": {} }, { "id": {} }]} } if page_num == 1: body['size']=9 res = es.search(index="polycies", body=body, filter_path=["hits.hits"]) rets = res.get("hits").get("hits") items = [{k.replace(".fpy", "").replace(".spy", ""): ''.join(v) for k, v in item.get("highlight").items()} for item in rets] for item in items: item['id'] = rets[items.index(item)].get("_source").get("id") if page_num==1: body["size"] = 10 body["_source"] = "publishTime" res = es.search(index="polycies", body=body, filter_path=["hits.hits"]) rets = res.get("hits").get("hits") time_list = [ time.strftime("%Y-%m-%d", time.localtime(int(item.get("_source").get("publishTime") / 1000))) for item in rets if item.get("_source").get("publishTime")>0] time_list = list(set(time_list)) time_list.sort() items.insert(0,{"time_list":time_list}) start = (page_num - 1) * 10 end = start + 10 total = count pagination = Pagination(bs_version=3, page=page_num, total=total) context = { "items": items, "pagination": pagination, "page_num":page_num } print(json.dumps(body)) return render_template("v1/turn_page.html", **context) else: return render_template("v1/no_search.html")
def search(): search = request.args.get("search") target = request.args.get("target") id = request.cookies.get("id") if search: r.zadd(id, {search: int(round(time.time() * 1000000))}) body = { "query": { "bool": { "should": [{ "match_phrase": { "title": search } }, { "match_phrase": { "summary": search } }, { "match_phrase": { "title": { "query": search, "analyzer": "jt_cn" } } }, { "match_phrase": { "summary": { "query": search, "analyzer": "jt_cn" } } }] } }, "aggs": { "timeagg": { "cardinality": { "field": "publishTime" } } }, "size": 10000, "_source": ["publishTime", "id", "summary", "content"] } a = time.time() res = es.search(index="polycies2", body=body, filter_path=["hits"]) count = res["hits"]["total"] if count > 0: items = res["hits"]["hits"] items = [ item["_source"] for item in items if item.get("_source").get("publishTime") > 0 ] for item in items: item["publishTime"] = time.strftime( "%Y-%m-%d", time.localtime(int(item.get("publishTime") / 1000))) context = {"items": items} items.sort(key=lambda item: item.get("publishTime")) return render_template("v2/turn_page.html", **context) else: return render_template("v2/no_search.html") return render_template("v2/no_search.html")