Files
qiaoxinjiu 6994b185a3 addproject
2026-01-22 19:10:37 +08:00

86 lines
3.7 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# coding: utf-8
import json
import requests
from base_framework.public_tools.custom_error import BusinessError
class ElasticsearchApi:
"""
| 功能说明: | 查询Elasticsearch |
| 作者信息: | 作者 huaxuemin |
| 修改时间: | 2022-12-05 |
"""
def __init__(self):
self.es_url = "http://es-cn-zvp2bgn8a004cbosn.elasticsearch.aliyuncs.com:9200"
self.headers = {"Authorization": "Basic ZWxhc3RpYzpMa3N3aV5hbEl3"}
def kw_es_query(self, es_query_body, index="user_personas_index_qa", es_url=None, headers=None):
"""
| 功能说明: | 查询es |
| 输入参数: |
| | es_query_body | es请求参数符合es语法 |
| | index | 索引 |
| | es_url | es_url |
| 返回参数: | json |
| 作者信息: | 作者 huaxuemin | 修改时间 2022-12-05 |
说明根据es语法查询es
"""
es_url = es_url if es_url else self.es_url
req_es_url = es_url + "/" + index + "/" + "_search"
headers = self.headers.update(headers) if headers else self.headers
try:
res = requests.post(req_es_url, json=es_query_body, headers=headers)
return json.loads(res.text)
except Exception as e:
return e
def kw_es_replace(self, es_id, es_replace_body, index="user_personas_index_qa", es_url=None, headers=None):
"""
| 功能说明: | 根据id替换es数据 |
| 输入参数: |
| | es_id | es_id |
| | es_query_body | es请求参数符合es语法 |
| | index | 索引 |
| | es_url | es_url |
| 返回参数: | json |
| 作者信息: | 作者 huaxuemin | 修改时间 2022-12-05 |
说明根据es语法替换es数据
"""
es_url = es_url if es_url else self.es_url
req_es_url = es_url + "/" + index + "/" + "_doc" + "/" + es_id
headers = self.headers.update(headers) if headers else self.headers
try:
res = requests.post(req_es_url, json=es_replace_body, headers=headers)
return json.loads(res.text)
except Exception as e:
return e
def kw_dbs_to_es(self, index_name, env="qa", es_svr="db-to-es"):
"""
| 功能说明 | 同步某个索引的es数据 |
| 请求参数名 | 说明 | 类型 | 是否必填 | 如无要求时的值 |
| index_name | 索引名称 | string | 0 | peppa-classes-supply |
| env | 环境qasim | string | 0 | qa |
| es_svr | ES服务db-to-esteach-to-es等 | string | 0 | db-to-es |
:return
详见: https://tm.huohua.cn/162891389180100609/articles/215540479291179010
教务接口切换ES详见apollo配置peppa-teach-common teach.common change.to.es
"""
if env not in ("qa", "sim"):
raise BusinessError("只支持qa和sim")
resp = requests.get(url="http://{}.{}.huohua.cn?index={}".format(es_svr, env, index_name))
if resp.status_code != 200:
raise Exception('同步失败,错误信息:{}'.format(resp.text))
return True
if __name__ == '__main__':
es = ElasticsearchApi()
# print(es.kw_dbs_to_es(index_name='peppa-classes-supply'))
q_sql = {"query":{"bool":{"must":[{"term":{"classes.business_region":"101"}},{"term":{"classes.year":"2023"}}],"must_not":[],"should":[]}},"from":0,"size":10,"sort":[{"classes.id":{"order":"desc"}}],"aggs":{}}
q_url = "http://10.250.200.194:9200"
rsp = es.kw_es_query(es_query_body=q_sql, es_url=q_url, index="peppa-classes-supply2")
print(rsp)