Files
dsProject/dsLightRag/ElasticSearch/T5_SelectByKeyWord.py
2025-08-19 14:02:48 +08:00

28 lines
1.1 KiB
Python
Raw 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.

from Config import Config
from ElasticSearch.Utils.EsSearchUtil import EsSearchUtil
# 1. 创建EsSearchUtil实例已封装连接池
search_util = EsSearchUtil(Config.ES_CONFIG)
# 2. 直接在代码中指定要查询的关键字
query_keyword = "混凝土"
# 3. 执行查询并处理结果
try:
# 使用连接池进行查询
results = search_util.text_search(query_keyword, size=1000)
print(f"查询关键字 '{query_keyword}' 结果:")
if results['hits']['hits']:
for i, hit in enumerate(results['hits']['hits'], 1):
doc = hit['_source']
print(f"{i}. ID: {hit['_id']}")
print(f" 标签: {doc['tags']['tags'] if 'tags' in doc and 'tags' in doc['tags'] else ''}")
print(f" 用户问题: {doc['user_input'] if 'user_input' in doc else ''}")
print(f" 时间: {doc['timestamp'] if 'timestamp' in doc else ''}")
print("-" * 50)
else:
print(f"未找到包含 '{query_keyword}' 的数据。")
except Exception as e:
print(f"查询失败: {e}")
print(f"查询关键字: {query_keyword}")