#coding:utf8 import elasticsearch from elasticsearch import Elasticsearch from utils.Utils import getLegal_str # 内网:http://es-cn-lbj3cjmy3000djxak.elasticsearch.aliyuncs.com:9200 # 外网:http://es-cn-lbj3cjmy3000djxak.public.elasticsearch.aliyuncs.com:9200 es = Elasticsearch(["http://es-cn-lbj3cjmy3000djxak.public.elasticsearch.aliyuncs.com:9200"], http_auth=('elastic','WWBu9#1HWHo$$gJm'), port=9200,timeout=60 ) print(es.info()) keywords = "无人机、游艇、风电、碳塑、改性塑料、高分子、新材料、自行车、复合材料、碳纤维" # keywords = "无人机、游艇、风电、碳塑、改性塑料" keywords = "高分子、新材料、自行车、复合材料、碳纤维" list_should = [] for keyword in keywords.split("、"): list_should.extend([ # {'match_phrase': # {'nicknames':keyword # # { # # "query": "医院", # >= 大于等于 # # # # "lt": 1650038400000 # < 小于 # # } # }, # }, {'match_phrase': {'business_scope':keyword # { # "query": "医院", # >= 大于等于 # # # "lt": 1650038400000 # < 小于 # } }, }, # {'match_phrase': # {'description':keyword # # { # # "query": "医院", # >= 大于等于 # # # # "lt": 1650038400000 # < 小于 # # } # }, # } ]) # print(es.indices.get_mapping()) body = { "_source": "_name", 'query': { # 查询命令 "bool": { "must":[ {"has_child":{ "type":"contacts", "query":{ "bool":{ 'must': [ {'match_phrase': {'nicknames':'医院' # { # "query": "医院", # >= 大于等于 # # # "lt": 1650038400000 # < 小于 # } }, }, {'term': {'contacts_is_legal_person':0 # { # "query": "医院", # >= 大于等于 # # # "lt": 1650038400000 # < 小于 # } }, },{'term': {'contacts_is_mobile':1 # { # "query": "医院", # >= 大于等于 # # # "lt": 1650038400000 # < 小于 # } }, } ] } } }}, # {"bool":{ # "should":[ # {"term":{ # "district":"鹤山" # } # } # ] # }}, # {"bool":{ # "should":list_should # }} ] # 'must': [ # # # # {"range":{"tyc_id":{"gt":1111}}} # # {'match_phrase': # # {'contacts_is_legal_person':1 # # # { # # # "query": "医院", # >= 大于等于 # # # # # "lt": 1650038400000 # < 小于 # # # } # # }, # # },{'match_phrase': # # {'contacts_is_mobile':1 # # # { # # # "query": "医院", # >= 大于等于 # # # # # "lt": 1650038400000 # < 小于 # # # } # # }, # # } # ] }, }, # "sort": [ # {"id": "desc"} # ] "_source":"reg_location,address,location" } str_district = ''' 梅河口 公主岭 定州 辛集 晋州 新乐 滦州 遵化 迁安 塔城 乌苏 武安 阿勒泰 南宫 沙河 安国 高碑店 涿州 伊宁市 奎屯 霍尔果斯 平泉 阿图什 泊头 黄骅 河间 任丘 库尔勒 霸州 三河 深州 博乐 阿拉山口 昌吉 阜康 和田市 喀什 阿克苏 库车 古交 高平 怀仁 介休 河津 永济 原平 侯马 霍州 青铜峡 孝义 汾阳 灵武 德令哈 格尔木 霍林郭勒 玉树 满洲里 牙克石 扎兰屯 额尔古纳 根河 丰镇 乌兰浩特 阿尔山 锡林浩特 二连浩特 同仁 合作 江阴 宜兴 临夏市 邳州 新沂 溧阳 张家港 昆山 太仓 常熟 海安 如皋 启东 华亭 东台 仪征 高邮 丹阳 扬中 句容 玉门 敦煌 兴化 靖江 泰兴 建德 余姚 慈溪 瑞安 乐清 诸暨 嵊州 海宁 平湖 桐乡 兰溪 东阳 永康 义乌 神木 江山 子长 临海 温岭 玉环 龙泉 韩城 华阴 兴平 彬州 延吉大安 图们 敦化 和龙 珲春 龙井 巢湖 扶余 无为 洮南 临江 集安 双辽 磐石 桦甸 蛟河 舒兰 榆树 德惠 桐城 潜山 兴城 北票 凌源 界首 调兵山 开原 天长 明光 灯塔 大石桥 盖州 广德 宁国 凌海 北镇 东港 凤城 海城 瓦房店 庄河 新民 漠河 安达 肇东 海伦 嫩江 北安 五大连池 绥芬河 海林 宁安 穆棱 东宁 福清 龙海 同江 富锦 抚远 石狮 晋江 南安 铁力 永安 邵武 武夷山 建瓯 漳平 虎林 密山 福安 福鼎 讷河 尚志 五常 瑞昌 共青城 庐山 德兴 高安 丰城 樟树 景洪 井冈山 文山 龙南 瑞金 乐平 蒙自 个旧 开远 弥勒 楚雄 大理 贵溪 香格里拉 泸水 胶州 平度 莱西 芒市 瑞丽 滕州 龙口 莱阳 莱州 招远 栖霞 海阳 诸城 青州 寿光 安丘 昌邑 高密 邹城 曲阜 新泰 肥城 荣成 乳山 腾冲 水富 澄江 宣威 安宁 都匀 福泉 乐陵 禹城 凯里 临清 兴义 兴仁 赤水 仁怀 盘州 新郑 巩义 登封 荥阳 新密 清镇 偃师 舞钢 汝州 林州 华蓥 卫辉 辉县 长垣 西昌 沁阳 孟州 康定 马尔康 禹州 长葛 万源 灵宝 义马 阆中 永城 项城 峨眉山 隆昌 射洪 邓州 广汉 什邡 绵竹 江油 都江堰 彭州 崇州 邛崃 简阳 大冶 丹江口 宜都 当阳 枝江 凭祥 枣阳 宜城 老河口 合山 靖西 平果 钟祥 京山 北流 汉川 应城 安陆 桂平 东兴 松滋 石首 监利 洪湖 岑溪 麻城 武穴 荔浦 赤壁 广水 恩施 罗定 普宁 宁乡 浏阳 英德 连州 醴陵 韶山 湘乡 阳春 耒阳 常宁 邵东 武冈 陆丰 汨罗 临湘 津市 沅江 涟源 冷水江 兴宁 资兴 高州 化州 信宜 台山 鹤山 开平 恩平 洪江 吉首 四会 廉江 吴川 雷州 乐昌 南雄 龙港 邹平 利川 茫崖 ''' list_district = [] for s in str_district.split("\n"): s = s.strip() if s!="": list_district.append(s) #查询联系人 body = { "query":{ "bool":{ "must":[ {"term": {"my_join_field": "contacts"}}, {"term":{"contacts_is_mobile":1}}, {"term":{"contacts_is_legal_person":0}}, {"range":{"contacts_update_time":{"gte":"2023-01-01 00:00:00"}}}, # {"nested":{ # "path":"my_join_field", # "query":{ # "bool":{ # "must":[ # {"match_phrase":{"my_join_field.parent":"医院"}} # ] # } # } # }} # {"match_phrase":{"contacts_enterprise_name":"广州"}} { "has_parent":{ "parent_type": "enterprise", "query":{ "bool":{ "must":[ {"term":{"district":"鹤山"}}, {"match_phrase":{"nicknames":"医院"}}, ], "must_not":[ {"match_phrase":{"nicknames":"美容"}}, {"match_phrase":{"nicknames":"动物"}}, {"match_phrase":{"nicknames":"植物"}}, ] } } } } ], } }, "_source":["contacts_enterprise_name","contacts_person_name","contacts_phone_no"] } def search_data(es,body,nums,get_total=False): every_page_number = min(nums,1000) list_result = [] query = es.search(body,rest_total_hits_as_int=True,scroll='5m',size=every_page_number) total_count = query["hits"]["total"] _scroll_id = query["_scroll_id"] print(query["hits"]["hits"]) list_result.extend(query["hits"]["hits"]) print("es total_count:%d"%(total_count)) if get_total: get_nums = total_count else: get_nums = min(nums,total_count) page_numbers = get_nums//every_page_number+1 for _n in range(page_numbers): if len(list_result)>=get_nums: break query = es.scroll(scroll_id=_scroll_id,scroll='5m') _scroll_id = query["_scroll_id"] list_result.extend(query["hits"]["hits"]) print("%d/%d"%(len(list_result),total_count)) return list_result def data_to_excel(list_result,columns): import pandas as pd from utils.Utils import getCurrent_date df_data = {} for c in columns: df_data[c] = [] for _d in list_result: _d = _d.get("_source",{}) for c in columns: print(c,getLegal_str(_d.get(c))) df_data[c].append(getLegal_str(_d.get(c))) df = pd.DataFrame(df_data) df.to_excel("../data/%s_to_excel.xlsx"%(getCurrent_date(format="%Y-%m-%d_%H%M%S")),columns=columns) if __name__ == '__main__': # print(search_data(es,body,3000)) list_result = [] list_district_should = [] for district in list_district: list_district_should.append({"term":{"district":district}}) body = { "query":{ "bool":{ "must":[ {"term":{"contacts_status":1}}, {"term": {"my_join_field": "contacts"}}, {"term":{"contacts_is_mobile":1}}, {"term":{"contacts_is_legal_person":0}}, {"range":{"contacts_update_time":{"gte":"2023-01-01 00:00:00"}}}, # {"nested":{ # "path":"my_join_field", # "query":{ # "bool":{ # "must":[ # {"match_phrase":{"my_join_field.parent":"医院"}} # ] # } # } # }} # {"match_phrase":{"contacts_enterprise_name":"广州"}} { "has_parent":{ "parent_type": "enterprise", "query":{ "bool":{ "must":[ {"bool":{"should":list_district_should}}, # {"term":{"district":district}}, {"match_phrase":{"nicknames":"医院"}}, ], "must_not":[ {"match_phrase":{"nicknames":"美容"}}, {"match_phrase":{"nicknames":"动物"}}, {"match_phrase":{"nicknames":"宠物"}}, {"match_phrase":{"nicknames":"植物"}}, ] } } } } ], } }, "_source":["contacts_enterprise_name","contacts_person_name","contacts_phone_no"] } list_result.extend(search_data(es,body,10000000,False)) data_to_excel(list_result,["contacts_enterprise_name","contacts_person_name","contacts_phone_no"])