84 lines
2.8 KiB
Python
84 lines
2.8 KiB
Python
"""
|
|
置信度及总分计算
|
|
"""
|
|
import logging
|
|
import pandas as pd
|
|
from goojia_utils import MAIN_MONGO_CLIENT, ElasticSearchClient, dateutils, data_utils
|
|
from elasticsearch import helpers
|
|
import sys
|
|
|
|
log = logging.getLogger(__name__)
|
|
seaweed_conn = MAIN_MONGO_CLIENT.get_collection('fangjia', 'seaweed')
|
|
es206_client = ElasticSearchClient(host='192.168.0.89', username='elastic', password='elasticsearch')
|
|
|
|
index_name = "community_summary"
|
|
path = "/home/airflow/data/community"
|
|
m_date = dateutils.iso_format()
|
|
avg_price_map = dict()
|
|
|
|
|
|
def sync(city=None):
|
|
log.info(f"开始置信度打分 {index_name}")
|
|
|
|
# 加载库中均价数据
|
|
query_filter = [{"range": {"consumption_rating": {"gt": 0}}}]
|
|
if pd.notna(city):
|
|
query_filter.append({"term": {"city": city}})
|
|
|
|
query = {
|
|
"_source": ["city", "region", "credit_detail"],
|
|
"query": {
|
|
"bool": {
|
|
"filter": query_filter
|
|
}
|
|
}
|
|
}
|
|
community_summary_df = es206_client.scan_to_data_frame("community_summary", query)
|
|
community_summary_df = data_utils.flatten_json(community_summary_df, 'credit_detail')
|
|
community_summary_df['total_price_score'].fillna(0, inplace=True)
|
|
community_summary_df['relative_price_ranking_score'].fillna(0, inplace=True)
|
|
community_summary_df['estate_charge_score'].fillna(0, inplace=True)
|
|
|
|
community_summary_df['total_score'] = round(0.6 * community_summary_df['total_price_score'] + 0.2 * community_summary_df['relative_price_ranking_score'] + 0.2 * community_summary_df['estate_charge_score'])
|
|
community_summary_df['credit'] = round(community_summary_df['total_score'] / 10, 2)
|
|
|
|
actions = list()
|
|
count = 0
|
|
|
|
for index, item in community_summary_df[community_summary_df['total_score'].notna()].iterrows():
|
|
doc_info = {
|
|
'credit': item['credit'],
|
|
'credit_detail': {
|
|
'total_score': item['total_score']
|
|
}
|
|
}
|
|
|
|
action = {
|
|
"_op_type": "update",
|
|
"_index": index_name,
|
|
"_id": item['_id'],
|
|
"doc": doc_info,
|
|
"_retry_on_conflict": 5
|
|
}
|
|
actions.append(action)
|
|
if len(actions) == 5000:
|
|
helpers.bulk(es206_client.elastic_client, actions)
|
|
count += len(actions)
|
|
logging.info("已更新:" + str(count))
|
|
actions.clear()
|
|
if len(actions) > 0:
|
|
helpers.bulk(es206_client.elastic_client, actions)
|
|
count += len(actions)
|
|
logging.info("已更新:" + str(count))
|
|
actions.clear()
|
|
log.info(f"完成置信度打分 {index_name}")
|
|
|
|
|
|
sys_city = sys.argv[1] if len(sys.argv) > 1 else None
|
|
if pd.notna(sys_city) and "," in sys_city:
|
|
cities = sys_city.split(",")
|
|
for city in cities:
|
|
sync(city)
|
|
else:
|
|
sync(sys_city)
|