You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.9 KiB

4 weeks ago
import warnings
4 weeks ago
from elasticsearch import Elasticsearch
3 weeks ago
4 weeks ago
from Config import Config
4 weeks ago
# 抑制HTTPS相关警告
warnings.filterwarnings('ignore', message='Connecting to .* using TLS with verify_certs=False is insecure')
warnings.filterwarnings('ignore', message='Unverified HTTPS request is being made to host')
4 weeks ago
# 初始化Elasticsearch连接
es = Elasticsearch(
hosts=Config.ES_CONFIG['hosts'],
basic_auth=Config.ES_CONFIG['basic_auth'],
verify_certs=False
)
# 查询所有数据
def select_all_data(index_name):
try:
# 构建查询条件 - 匹配所有文档
4 weeks ago
# 修改查询条件为获取前10条数据
4 weeks ago
query = {
"query": {
"match_all": {}
},
4 weeks ago
"size": 1000 # 仅获取10条数据
4 weeks ago
}
# 执行查询
response = es.search(index=index_name, body=query)
hits = response['hits']['hits']
if not hits:
print(f"索引 {index_name} 中没有数据")
else:
print(f"索引 {index_name} 中共有 {len(hits)} 条数据:")
for i, hit in enumerate(hits, 1):
print(f"{i}. ID: {hit['_id']}")
print(f" 内容: {hit['_source'].get('user_input', '')}")
print(f" 标签: {hit['_source'].get('tags', '')}")
print(f" 时间戳: {hit['_source'].get('timestamp', '')}")
4 weeks ago
# 在循环中添加打印完整数据结构的代码
if i == 1: # 只打印第一条数据的完整结构
print("完整数据结构:")
import pprint
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(hit['_source'])
4 weeks ago
print("-" * 50)
except Exception as e:
print(f"查询出错: {e}")
if __name__ == "__main__":
select_all_data(Config.ES_CONFIG['index_name'])