2025-08-19 07:34:39 +08:00
|
|
|
from elasticsearch import Elasticsearch
|
|
|
|
import warnings
|
|
|
|
from Config import Config
|
|
|
|
from Config.Config import ES_CONFIG
|
|
|
|
import urllib3
|
|
|
|
|
|
|
|
# 抑制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')
|
|
|
|
|
|
|
|
|
|
|
|
# 1. 初始化Elasticsearch连接
|
|
|
|
es = Elasticsearch(
|
|
|
|
hosts=Config.ES_CONFIG['hosts'],
|
|
|
|
basic_auth=Config.ES_CONFIG['basic_auth'],
|
|
|
|
verify_certs=False
|
|
|
|
)
|
|
|
|
|
2025-08-19 08:33:30 +08:00
|
|
|
# 2. 直接在代码中指定要查询的关键字
|
|
|
|
query_keyword = "混凝土"
|
2025-08-19 07:34:39 +08:00
|
|
|
|
|
|
|
# 3. 构建查询条件
|
|
|
|
query = {
|
|
|
|
"query": {
|
2025-08-19 08:33:30 +08:00
|
|
|
"match": {
|
|
|
|
"user_input": query_keyword
|
2025-08-19 07:34:39 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"size": 1000
|
|
|
|
}
|
|
|
|
|
|
|
|
# 4. 执行查询并处理结果
|
|
|
|
try:
|
2025-08-19 08:33:30 +08:00
|
|
|
results = es.search(index=ES_CONFIG['index_name'], body=query)
|
|
|
|
print(f"查询关键字 '{query_keyword}' 结果:")
|
|
|
|
if results['hits']['hits']:
|
|
|
|
for i, hit in enumerate(results['hits']['hits'], 1):
|
|
|
|
doc = hit['_source']
|
2025-08-19 07:34:39 +08:00
|
|
|
print(f"{i}. ID: {hit['_id']}")
|
2025-08-19 08:33:30 +08:00
|
|
|
print(f" 标签: {doc['tags']['tags']}")
|
|
|
|
print(f" 用户问题: {doc['user_input']}")
|
|
|
|
print(f" 时间: {doc['timestamp']}")
|
|
|
|
print(f" 向量: {doc['embedding'][:5]}...")
|
2025-08-19 07:34:39 +08:00
|
|
|
print("-" * 50)
|
2025-08-19 08:33:30 +08:00
|
|
|
else:
|
|
|
|
print(f"未找到包含 '{query_keyword}' 的数据。")
|
2025-08-19 07:34:39 +08:00
|
|
|
except Exception as e:
|
2025-08-19 08:33:30 +08:00
|
|
|
print(f"查询失败: {e}")
|
2025-08-19 07:34:39 +08:00
|
|
|
print(f"查询条件: {query}")
|
|
|
|
if hasattr(e, 'info') and isinstance(e.info, dict):
|
|
|
|
print(f"Elasticsearch错误详情: {e.info}")
|
|
|
|
if hasattr(e, 'status_code'):
|
2025-08-19 08:33:30 +08:00
|
|
|
print(f"HTTP状态码: {e.status_code}")
|