Commit 5bc91bff authored by 杨林's avatar 杨林

搜索es方法

parent c02cdaf9
......@@ -5,9 +5,13 @@ class ESTools:
"""
es工具
"""
# es库
_index = 'goods'
# es主机
_host = [{"host": '172.18.45.18', "port": 9200}, {"host": '172.18.45.17', "port": 9200},
{"host": '172.18.45.16', "port": 9200}, {"host": '172.18.69.190', "port": 9200}]
{"host": '172.18.45.16', "port": 9200}, {"host": '172.18.69.190', "port": 9200},
{"host": '172.18.69.192', "port": 9200}]
_TB_RATE = 0.7
_JD_RATE = 0.55
......@@ -23,6 +27,7 @@ class ESTools:
:return: 搜索结果和分数
"""
if self.es.ping():
# es查询语句
query = {
"query": {
"bool": {
......@@ -43,8 +48,10 @@ class ESTools:
"from": 0,
"size": size
}
# 查询结果
hits = self.es.search(index=self._index, body=query)
res = hits['hits']['hits']
# 取出查询分数
scores = [i['_score'] for i in res]
scores = sorted(scores)
return res, scores
......@@ -68,17 +75,27 @@ class ESTools:
score_median = sco[int(len(sco) / 2)]
for i in res:
# 对每条数据进行处理
# 分数大于中位数
if i["_score"] > score_median:
# 分数大于平均数
if i["_score"] > score_avg:
# 源数据
source = i['_source']
# 平台
platform = source["platform"]
# 券后佣金
coupon_commission = source["coupon_commission"]
# 给用户的佣金比例
rate = self._JD_RATE if platform == 2 else self._TB_RATE
# 券金额
coupon_amount = source["coupon_amount"]
# 优惠文案
discount_text = f'¥{coupon_amount}券' if coupon_amount > 0 else ''
# 用户佣金
user_commission = "{:.2f}".format(coupon_commission * rate)
# 券后价
end_price = source["coupon_price"]
# 最终优惠金额
final_price = round(price - end_price, 2)
result = {
"item_id": source["item_id"],
......@@ -97,6 +114,7 @@ class ESTools:
"user_commission_text": f'返现{user_commission}'
}
temp.append(result)
# 按价格排序
results = sorted(temp, key=lambda x: x["end_price"])
return results
else:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment