'commit'
This commit is contained in:
@@ -16,22 +16,14 @@ es = Elasticsearch(
|
||||
verify_certs=False
|
||||
)
|
||||
|
||||
# 2. 直接在代码中指定要查询的标签
|
||||
query_tag = ["MATH_1"] # 可以修改为其他需要的标签
|
||||
# 2. 直接在代码中指定要查询的关键字
|
||||
query_keyword = "混凝土"
|
||||
|
||||
# 3. 构建查询条件
|
||||
query = {
|
||||
"query": {
|
||||
"bool": {
|
||||
"should": [
|
||||
{
|
||||
"terms": {
|
||||
"tags.tags": query_tag
|
||||
}
|
||||
}
|
||||
],
|
||||
"minimum_should_match": 1
|
||||
|
||||
"match": {
|
||||
"user_input": query_keyword
|
||||
}
|
||||
},
|
||||
"size": 1000
|
||||
@@ -39,41 +31,23 @@ query = {
|
||||
|
||||
# 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):
|
||||
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']
|
||||
print(f"{i}. ID: {hit['_id']}")
|
||||
print(f" 内容: {hit['_source'].get('user_input', '')}")
|
||||
print(f" 标签: {hit['_source']['tags']['tags']}")
|
||||
print(f" 标签: {doc['tags']['tags']}")
|
||||
print(f" 用户问题: {doc['user_input']}")
|
||||
print(f" 时间: {doc['timestamp']}")
|
||||
print(f" 向量: {doc['embedding'][:5]}...")
|
||||
print("-" * 50)
|
||||
else:
|
||||
print(f"未找到包含 '{query_keyword}' 的数据。")
|
||||
except Exception as e:
|
||||
print(f"查询执行失败,错误详情: {str(e)}")
|
||||
print(f"查询失败: {e}")
|
||||
print(f"查询条件: {query}")
|
||||
if hasattr(e, 'info') and isinstance(e.info, dict):
|
||||
print(f"Elasticsearch错误详情: {e.info}")
|
||||
if hasattr(e, 'status_code'):
|
||||
print(f"HTTP状态码: {e.status_code}")
|
||||
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}")
|
||||
print(f"HTTP状态码: {e.status_code}")
|
Reference in New Issue
Block a user