优化字段同步

This commit is contained in:
shenjian@fangjia.com
2022-10-17 15:39:12 +08:00
parent 416a8a8ebd
commit 3d69b5d3a8
3 changed files with 25 additions and 13 deletions
+8 -2
View File
@@ -77,6 +77,7 @@ def sync():
actions = list()
# 对于存在的更新
count = 0
for index, item in seaweed_existed_df.iterrows():
doc_info = build_doc(item)
@@ -88,11 +89,14 @@ def sync():
}
actions.append(action)
if len(actions) == 1000:
if len(actions) == 5000:
helpers.bulk(es206_client.elastic_client, actions)
count += len(actions)
print("更新:" + str(count))
actions.clear()
# 对于不存在的插入
count = 0
for index, item in seaweed_not_existed_df.iterrows():
doc_info = build_doc(item)
@@ -104,8 +108,10 @@ def sync():
}
actions.append(action)
if len(actions) == 1000:
if len(actions) == 5000:
helpers.bulk(es206_client.elastic_client, actions)
count += len(actions)
print("插入:" + str(count))
actions.clear()
if len(actions) > 0:
helpers.bulk(es206_client.elastic_client, actions)
@@ -11,5 +11,5 @@ log.info("同步类型:" + type)
if type == 'all':
comm.sync()
price.sync()
#price.sync()
pass
+16 -10
View File
@@ -31,19 +31,25 @@ def build_doc(item):
return doc_dict
def sync():
def sync(city=None):
seaweed_query = {'cat': 'district', 'status': 0}
if pd.notna(city):
seaweed_query['city'] = city
cities = [city]
else:
cities = None
seaweed_df = pd.DataFrame(
list(seaweed_conn.find({'city': '上海', 'cat': 'district', 'status': 0}, {'city': 1, 'region': 1, 'name': 1})
list(seaweed_conn.find(seaweed_query, {'city': 1, 'region': 1, 'name': 1})
.batch_size(100000)))
# 小区所在区域均价
region_price_df = commutils.get_region_price()
region_price_df = commutils.get_region_price(city=city)
region_price_df = region_price_df[['city', 'region', 'avg_price']]
region_price_df = region_price_df.rename(columns={'avg_price': 'region_avg_price'})
seaweed_df = pd.merge(seaweed_df, region_price_df, how='left', on=['city', 'region'])
# 小区均价
avg_price_df = commutils.get_avg_price()
avg_price_df = commutils.get_avg_price(city=city)
avg_price_df = avg_price_df[['city', 'district_name', 'region', 'avg_price']]
avg_price_df = avg_price_df.rename(columns={'district_name': 'name'})
seaweed_df = pd.merge(seaweed_df, avg_price_df, how='left', on=['city', 'region', 'name'])
@@ -54,24 +60,26 @@ def sync():
seaweed_df['avg_price_ranking'] = seaweed_df.groupby(by=['city']).rank(method='first', na_option='bottom', ascending=False)['avg_price']
# 12个月均价涨跌幅
avg_price_up_and_downs_df = commutils.get_avg_price_up_and_downs(months=-12)
avg_price_up_and_downs_df = commutils.get_avg_price_up_and_downs(cities=cities, months=-12)
avg_price_up_and_downs_df = avg_price_up_and_downs_df[['city', 'region', 'district_name', 'up_and_downs']]
avg_price_up_and_downs_df = avg_price_up_and_downs_df.rename(columns={'district_name': 'name', 'up_and_downs': 'year_on_year'})
seaweed_df = pd.merge(seaweed_df, avg_price_up_and_downs_df, how='left', on=['city', 'region', 'name'])
# 近半年均价涨跌幅
avg_price_up_and_downs_df = commutils.get_avg_price_up_and_downs(months=-6)
avg_price_up_and_downs_df = commutils.get_avg_price_up_and_downs(cities=cities, months=-6)
avg_price_up_and_downs_df = avg_price_up_and_downs_df[['city', 'region', 'district_name', 'up_and_downs']]
avg_price_up_and_downs_df = avg_price_up_and_downs_df.rename(
columns={'district_name': 'name', 'up_and_downs': 'half_on_year'})
seaweed_df = pd.merge(seaweed_df, avg_price_up_and_downs_df, how='left', on=['city', 'region', 'name'])
# 平米租金
rent_price_df = commutils.get_rent_price()
rent_price_df = commutils.get_rent_price(city=city)
rent_price_df = rent_price_df[['city', 'region', 'district_name', 'avg_price']]
rent_price_df = rent_price_df.rename(columns={'district_name': 'name', 'avg_price': 'rent_price'})
seaweed_df = pd.merge(seaweed_df, rent_price_df, how='left', on=['city', 'region', 'name'])
seaweed_df['_id'] = seaweed_df['_id'].astype(str)
actions = list()
for index, item in seaweed_df.iterrows():
doc_info = build_doc(item)
@@ -79,7 +87,7 @@ def sync():
action = {
"_op_type": "update",
"_index": index_name,
"_id": item['id'],
"_id": item['_id'],
"doc": doc_info
}
@@ -90,5 +98,3 @@ def sync():
if len(actions) > 0:
helpers.bulk(es206_client.elastic_client, actions)
actions.clear()
sync()