'commit'
This commit is contained in:
55
dsSchoolBuddy/ElasticSearch/T4_SelectAllData.py
Normal file
55
dsSchoolBuddy/ElasticSearch/T4_SelectAllData.py
Normal file
@@ -0,0 +1,55 @@
|
||||
import warnings
|
||||
|
||||
from elasticsearch import Elasticsearch
|
||||
|
||||
from Config import Config
|
||||
|
||||
# 抑制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')
|
||||
|
||||
|
||||
# 初始化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:
|
||||
# 构建查询条件 - 匹配所有文档
|
||||
# 修改查询条件为获取前10条数据
|
||||
query = {
|
||||
"query": {
|
||||
"match_all": {}
|
||||
},
|
||||
"size": 1000 # 仅获取10条数据
|
||||
}
|
||||
|
||||
# 执行查询
|
||||
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', '')}")
|
||||
# 在循环中添加打印完整数据结构的代码
|
||||
if i == 1: # 只打印第一条数据的完整结构
|
||||
print("完整数据结构:")
|
||||
import pprint
|
||||
pp = pprint.PrettyPrinter(indent=4)
|
||||
pp.pprint(hit['_source'])
|
||||
print("-" * 50)
|
||||
except Exception as e:
|
||||
print(f"查询出错: {e}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
select_all_data(Config.ES_CONFIG['index_name'])
|
Reference in New Issue
Block a user