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.
69 lines
2.1 KiB
69 lines
2.1 KiB
from elasticsearch import Elasticsearch
|
|
import warnings
|
|
from Config import Config
|
|
from Config.Config import ES_CONFIG
|
|
import urllib3
|
|
|
|
# 禁用SSL警告
|
|
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
|
|
|
# 1. 初始化Elasticsearch连接
|
|
es = Elasticsearch(
|
|
hosts=Config.ES_CONFIG['hosts'],
|
|
basic_auth=Config.ES_CONFIG['basic_auth'],
|
|
verify_certs=False
|
|
)
|
|
|
|
# 2. 直接在代码中指定要查询的标签
|
|
query_tag = ["MATH_DATA_1", "小学数学"] # 可以修改为其他需要的标签
|
|
|
|
# 3. 构建查询条件
|
|
query = {
|
|
"query": {
|
|
"bool": {
|
|
"must": [
|
|
{
|
|
"terms": {
|
|
"tags.tags": query_tag
|
|
}
|
|
}
|
|
]
|
|
}
|
|
},
|
|
"size": 1000
|
|
}
|
|
|
|
# 4. 执行查询并处理结果
|
|
try:
|
|
response = es.search(index="knowledge_base", body=query)
|
|
hits = response['hits']['hits']
|
|
|
|
if not hits:
|
|
print(f"未找到标签为 '{query_tag}' 的数据。")
|
|
else:
|
|
print(f"找到 {len(hits)} 条标签为 '{query_tag}' 的数据:")
|
|
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("-" * 50)
|
|
except Exception as e:
|
|
print(f"查询出错: {str(e)}")
|
|
|
|
# 4. 执行查询
|
|
try:
|
|
results = es.search(index=ES_CONFIG['index_name'], body=query)
|
|
print(f"查询标签 '{query_tag}' 结果:")
|
|
if results['hits']['hits']:
|
|
for hit in results['hits']['hits']:
|
|
doc = hit['_source']
|
|
print(f"ID: {hit['_id']}")
|
|
print(f"标签: {doc['tags']['tags']}")
|
|
print(f"用户问题: {doc['user_input']}")
|
|
print(f"时间: {doc['timestamp']}")
|
|
print(f"向量: {doc['embedding'][:5]}...")
|
|
print("-" * 40)
|
|
else:
|
|
print(f"未找到标签为 '{query_tag}' 的数据。")
|
|
except Exception as e:
|
|
print(f"查询失败: {e}") |