exportEs.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. #coding:utf8
  2. import elasticsearch
  3. from elasticsearch import Elasticsearch
  4. from utils.Utils import getLegal_str
  5. # 内网:http://es-cn-lbj3cjmy3000djxak.elasticsearch.aliyuncs.com:9200
  6. # 外网:http://es-cn-lbj3cjmy3000djxak.public.elasticsearch.aliyuncs.com:9200
  7. es = Elasticsearch(["http://es-cn-lbj3cjmy3000djxak.public.elasticsearch.aliyuncs.com:9200"],
  8. http_auth=('elastic','WWBu9#1HWHo$$gJm'),
  9. port=9200,timeout=60
  10. )
  11. print(es.info())
  12. keywords = "无人机、游艇、风电、碳塑、改性塑料、高分子、新材料、自行车、复合材料、碳纤维"
  13. # keywords = "无人机、游艇、风电、碳塑、改性塑料"
  14. keywords = "高分子、新材料、自行车、复合材料、碳纤维"
  15. list_should = []
  16. for keyword in keywords.split("、"):
  17. list_should.extend([
  18. # {'match_phrase':
  19. # {'nicknames':keyword
  20. # # {
  21. # # "query": "医院", # >= 大于等于
  22. # # # # "lt": 1650038400000 # < 小于
  23. # # }
  24. # },
  25. # },
  26. {'match_phrase':
  27. {'business_scope':keyword
  28. # {
  29. # "query": "医院", # >= 大于等于
  30. # # # "lt": 1650038400000 # < 小于
  31. # }
  32. },
  33. },
  34. # {'match_phrase':
  35. # {'description':keyword
  36. # # {
  37. # # "query": "医院", # >= 大于等于
  38. # # # # "lt": 1650038400000 # < 小于
  39. # # }
  40. # },
  41. # }
  42. ])
  43. # print(es.indices.get_mapping())
  44. body = {
  45. "_source": "_name",
  46. 'query': { # 查询命令
  47. "bool": {
  48. "must":[
  49. {"has_child":{
  50. "type":"contacts",
  51. "query":{
  52. "bool":{
  53. 'must': [
  54. {'match_phrase':
  55. {'nicknames':'医院'
  56. # {
  57. # "query": "医院", # >= 大于等于
  58. # # # "lt": 1650038400000 # < 小于
  59. # }
  60. },
  61. },
  62. {'term':
  63. {'contacts_is_legal_person':0
  64. # {
  65. # "query": "医院", # >= 大于等于
  66. # # # "lt": 1650038400000 # < 小于
  67. # }
  68. },
  69. },{'term':
  70. {'contacts_is_mobile':1
  71. # {
  72. # "query": "医院", # >= 大于等于
  73. # # # "lt": 1650038400000 # < 小于
  74. # }
  75. },
  76. }
  77. ]
  78. }
  79. }
  80. }},
  81. # {"bool":{
  82. # "should":[
  83. # {"term":{
  84. # "district":"鹤山"
  85. # }
  86. # }
  87. # ]
  88. # }},
  89. # {"bool":{
  90. # "should":list_should
  91. # }}
  92. ]
  93. # 'must': [
  94. #
  95. #
  96. # # {"range":{"tyc_id":{"gt":1111}}}
  97. # # {'match_phrase':
  98. # # {'contacts_is_legal_person':1
  99. # # # {
  100. # # # "query": "医院", # >= 大于等于
  101. # # # # # "lt": 1650038400000 # < 小于
  102. # # # }
  103. # # },
  104. # # },{'match_phrase':
  105. # # {'contacts_is_mobile':1
  106. # # # {
  107. # # # "query": "医院", # >= 大于等于
  108. # # # # # "lt": 1650038400000 # < 小于
  109. # # # }
  110. # # },
  111. # # }
  112. # ]
  113. },
  114. },
  115. # "sort": [
  116. # {"id": "desc"}
  117. # ]
  118. "_source":"reg_location,address,location"
  119. }
  120. str_district = '''
  121. 梅河口
  122. 公主岭
  123. 定州
  124. 辛集
  125. 晋州
  126. 新乐
  127. 滦州
  128. 遵化
  129. 迁安
  130. 塔城
  131. 乌苏
  132. 武安
  133. 阿勒泰
  134. 南宫
  135. 沙河
  136. 安国
  137. 高碑店
  138. 涿州
  139. 伊宁市
  140. 奎屯
  141. 霍尔果斯
  142. 平泉
  143. 阿图什
  144. 泊头
  145. 黄骅
  146. 河间
  147. 任丘
  148. 库尔勒
  149. 霸州
  150. 三河
  151. 深州
  152. 博乐
  153. 阿拉山口
  154. 昌吉
  155. 阜康
  156. 和田市
  157. 喀什
  158. 阿克苏
  159. 库车
  160. 古交
  161. 高平
  162. 怀仁
  163. 介休
  164. 河津
  165. 永济
  166. 原平
  167. 侯马
  168. 霍州
  169. 青铜峡
  170. 孝义
  171. 汾阳
  172. 灵武
  173. 德令哈
  174. 格尔木
  175. 霍林郭勒
  176. 玉树
  177. 满洲里
  178. 牙克石
  179. 扎兰屯
  180. 额尔古纳
  181. 根河
  182. 丰镇
  183. 乌兰浩特
  184. 阿尔山
  185. 锡林浩特
  186. 二连浩特
  187. 同仁
  188. 合作
  189. 江阴
  190. 宜兴
  191. 临夏市
  192. 邳州
  193. 新沂
  194. 溧阳
  195. 张家港
  196. 昆山
  197. 太仓
  198. 常熟
  199. 海安
  200. 如皋
  201. 启东
  202. 华亭
  203. 东台
  204. 仪征
  205. 高邮
  206. 丹阳
  207. 扬中
  208. 句容
  209. 玉门
  210. 敦煌
  211. 兴化
  212. 靖江
  213. 泰兴
  214. 建德
  215. 余姚
  216. 慈溪
  217. 瑞安
  218. 乐清
  219. 诸暨
  220. 嵊州
  221. 海宁
  222. 平湖
  223. 桐乡
  224. 兰溪
  225. 东阳
  226. 永康
  227. 义乌
  228. 神木
  229. 江山
  230. 子长
  231. 临海
  232. 温岭
  233. 玉环
  234. 龙泉
  235. 韩城
  236. 华阴
  237. 兴平
  238. 彬州
  239. 延吉大安
  240. 图们
  241. 敦化
  242. 和龙
  243. 珲春
  244. 龙井
  245. 巢湖
  246. 扶余
  247. 无为
  248. 洮南
  249. 临江
  250. 集安
  251. 双辽
  252. 磐石
  253. 桦甸
  254. 蛟河
  255. 舒兰
  256. 榆树
  257. 德惠
  258. 桐城
  259. 潜山
  260. 兴城
  261. 北票
  262. 凌源
  263. 界首
  264. 调兵山
  265. 开原
  266. 天长
  267. 明光
  268. 灯塔
  269. 大石桥
  270. 盖州
  271. 广德
  272. 宁国
  273. 凌海
  274. 北镇
  275. 东港
  276. 凤城
  277. 海城
  278. 瓦房店
  279. 庄河
  280. 新民
  281. 漠河
  282. 安达
  283. 肇东
  284. 海伦
  285. 嫩江
  286. 北安
  287. 五大连池
  288. 绥芬河
  289. 海林
  290. 宁安
  291. 穆棱
  292. 东宁
  293. 福清
  294. 龙海
  295. 同江
  296. 富锦
  297. 抚远
  298. 石狮
  299. 晋江
  300. 南安
  301. 铁力
  302. 永安
  303. 邵武
  304. 武夷山
  305. 建瓯
  306. 漳平
  307. 虎林
  308. 密山
  309. 福安
  310. 福鼎
  311. 讷河
  312. 尚志
  313. 五常
  314. 瑞昌
  315. 共青城
  316. 庐山
  317. 德兴
  318. 高安
  319. 丰城
  320. 樟树
  321. 景洪
  322. 井冈山
  323. 文山
  324. 龙南
  325. 瑞金
  326. 乐平
  327. 蒙自
  328. 个旧
  329. 开远
  330. 弥勒
  331. 楚雄
  332. 大理
  333. 贵溪
  334. 香格里拉
  335. 泸水
  336. 胶州
  337. 平度
  338. 莱西
  339. 芒市
  340. 瑞丽
  341. 滕州
  342. 龙口
  343. 莱阳
  344. 莱州
  345. 招远
  346. 栖霞
  347. 海阳
  348. 诸城
  349. 青州
  350. 寿光
  351. 安丘
  352. 昌邑
  353. 高密
  354. 邹城
  355. 曲阜
  356. 新泰
  357. 肥城
  358. 荣成
  359. 乳山
  360. 腾冲
  361. 水富
  362. 澄江
  363. 宣威
  364. 安宁
  365. 都匀
  366. 福泉
  367. 乐陵
  368. 禹城
  369. 凯里
  370. 临清
  371. 兴义
  372. 兴仁
  373. 赤水
  374. 仁怀
  375. 盘州
  376. 新郑
  377. 巩义
  378. 登封
  379. 荥阳
  380. 新密
  381. 清镇
  382. 偃师
  383. 舞钢
  384. 汝州
  385. 林州
  386. 华蓥
  387. 卫辉
  388. 辉县
  389. 长垣
  390. 西昌
  391. 沁阳
  392. 孟州
  393. 康定
  394. 马尔康
  395. 禹州
  396. 长葛
  397. 万源
  398. 灵宝
  399. 义马
  400. 阆中
  401. 永城
  402. 项城
  403. 峨眉山
  404. 隆昌
  405. 射洪
  406. 邓州
  407. 广汉
  408. 什邡
  409. 绵竹
  410. 江油
  411. 都江堰
  412. 彭州
  413. 崇州
  414. 邛崃
  415. 简阳
  416. 大冶
  417. 丹江口
  418. 宜都
  419. 当阳
  420. 枝江
  421. 凭祥
  422. 枣阳
  423. 宜城
  424. 老河口
  425. 合山
  426. 靖西
  427. 平果
  428. 钟祥
  429. 京山
  430. 北流
  431. 汉川
  432. 应城
  433. 安陆
  434. 桂平
  435. 东兴
  436. 松滋
  437. 石首
  438. 监利
  439. 洪湖
  440. 岑溪
  441. 麻城
  442. 武穴
  443. 荔浦
  444. 赤壁
  445. 广水
  446. 恩施
  447. 罗定
  448. 普宁
  449. 宁乡
  450. 浏阳
  451. 英德
  452. 连州
  453. 醴陵
  454. 韶山
  455. 湘乡
  456. 阳春
  457. 耒阳
  458. 常宁
  459. 邵东
  460. 武冈
  461. 陆丰
  462. 汨罗
  463. 临湘
  464. 津市
  465. 沅江
  466. 涟源
  467. 冷水江
  468. 兴宁
  469. 资兴
  470. 高州
  471. 化州
  472. 信宜
  473. 台山
  474. 鹤山
  475. 开平
  476. 恩平
  477. 洪江
  478. 吉首
  479. 四会
  480. 廉江
  481. 吴川
  482. 雷州
  483. 乐昌
  484. 南雄
  485. 龙港
  486. 邹平
  487. 利川
  488. 茫崖
  489. '''
  490. list_district = []
  491. for s in str_district.split("\n"):
  492. s = s.strip()
  493. if s!="":
  494. list_district.append(s)
  495. #查询联系人
  496. body = {
  497. "query":{
  498. "bool":{
  499. "must":[
  500. {"term": {"my_join_field": "contacts"}},
  501. {"term":{"contacts_is_mobile":1}},
  502. {"term":{"contacts_is_legal_person":0}},
  503. {"range":{"contacts_update_time":{"gte":"2023-01-01 00:00:00"}}},
  504. # {"nested":{
  505. # "path":"my_join_field",
  506. # "query":{
  507. # "bool":{
  508. # "must":[
  509. # {"match_phrase":{"my_join_field.parent":"医院"}}
  510. # ]
  511. # }
  512. # }
  513. # }}
  514. # {"match_phrase":{"contacts_enterprise_name":"广州"}}
  515. {
  516. "has_parent":{
  517. "parent_type": "enterprise",
  518. "query":{
  519. "bool":{
  520. "must":[
  521. {"term":{"district":"鹤山"}},
  522. {"match_phrase":{"nicknames":"医院"}},
  523. ],
  524. "must_not":[
  525. {"match_phrase":{"nicknames":"美容"}},
  526. {"match_phrase":{"nicknames":"动物"}},
  527. {"match_phrase":{"nicknames":"植物"}},
  528. ]
  529. }
  530. }
  531. }
  532. }
  533. ],
  534. }
  535. },
  536. "_source":["contacts_enterprise_name","contacts_person_name","contacts_phone_no"]
  537. }
  538. def search_data(es,body,nums,get_total=False):
  539. every_page_number = min(nums,1000)
  540. list_result = []
  541. query = es.search(body,rest_total_hits_as_int=True,scroll='5m',size=every_page_number)
  542. total_count = query["hits"]["total"]
  543. _scroll_id = query["_scroll_id"]
  544. print(query["hits"]["hits"])
  545. list_result.extend(query["hits"]["hits"])
  546. print("es total_count:%d"%(total_count))
  547. if get_total:
  548. get_nums = total_count
  549. else:
  550. get_nums = min(nums,total_count)
  551. page_numbers = get_nums//every_page_number+1
  552. for _n in range(page_numbers):
  553. if len(list_result)>=get_nums:
  554. break
  555. query = es.scroll(scroll_id=_scroll_id,scroll='5m')
  556. _scroll_id = query["_scroll_id"]
  557. list_result.extend(query["hits"]["hits"])
  558. print("%d/%d"%(len(list_result),total_count))
  559. return list_result
  560. def data_to_excel(list_result,columns):
  561. import pandas as pd
  562. from utils.Utils import getCurrent_date
  563. df_data = {}
  564. for c in columns:
  565. df_data[c] = []
  566. for _d in list_result:
  567. _d = _d.get("_source",{})
  568. for c in columns:
  569. print(c,getLegal_str(_d.get(c)))
  570. df_data[c].append(getLegal_str(_d.get(c)))
  571. df = pd.DataFrame(df_data)
  572. df.to_excel("../data/%s_to_excel.xlsx"%(getCurrent_date(format="%Y-%m-%d_%H%M%S")),columns=columns)
  573. if __name__ == '__main__':
  574. # print(search_data(es,body,3000))
  575. list_result = []
  576. list_district_should = []
  577. for district in list_district:
  578. list_district_should.append({"term":{"district":district}})
  579. body = {
  580. "query":{
  581. "bool":{
  582. "must":[
  583. {"term":{"contacts_status":1}},
  584. {"term": {"my_join_field": "contacts"}},
  585. {"term":{"contacts_is_mobile":1}},
  586. {"term":{"contacts_is_legal_person":0}},
  587. {"range":{"contacts_update_time":{"gte":"2023-01-01 00:00:00"}}},
  588. # {"nested":{
  589. # "path":"my_join_field",
  590. # "query":{
  591. # "bool":{
  592. # "must":[
  593. # {"match_phrase":{"my_join_field.parent":"医院"}}
  594. # ]
  595. # }
  596. # }
  597. # }}
  598. # {"match_phrase":{"contacts_enterprise_name":"广州"}}
  599. {
  600. "has_parent":{
  601. "parent_type": "enterprise",
  602. "query":{
  603. "bool":{
  604. "must":[
  605. {"bool":{"should":list_district_should}},
  606. # {"term":{"district":district}},
  607. {"match_phrase":{"nicknames":"医院"}},
  608. ],
  609. "must_not":[
  610. {"match_phrase":{"nicknames":"美容"}},
  611. {"match_phrase":{"nicknames":"动物"}},
  612. {"match_phrase":{"nicknames":"宠物"}},
  613. {"match_phrase":{"nicknames":"植物"}},
  614. ]
  615. }
  616. }
  617. }
  618. }
  619. ],
  620. }
  621. },
  622. "_source":["contacts_enterprise_name","contacts_person_name","contacts_phone_no"]
  623. }
  624. list_result.extend(search_data(es,body,10000000,False))
  625. data_to_excel(list_result,["contacts_enterprise_name","contacts_person_name","contacts_phone_no"])