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.
110 lines
4.2 KiB
110 lines
4.2 KiB
import logging
|
|
import warnings
|
|
|
|
from Config.Config import ES_CONFIG
|
|
from Util.EsSearchUtil import EsSearchUtil
|
|
|
|
# 初始化日志
|
|
logger = logging.getLogger(__name__)
|
|
logger.setLevel(logging.INFO)
|
|
|
|
# 初始化EsSearchUtil
|
|
esClient = EsSearchUtil(ES_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')
|
|
|
|
if __name__ == "__main__":
|
|
# 测试查询
|
|
# query = "小学数学中有哪些模型"
|
|
query = "文言虚词"
|
|
query_tags = ["MATH_1"] # 默认搜索标签,可修改
|
|
print(f"\n=== 开始执行查询 ===")
|
|
print(f"原始查询文本: {query}")
|
|
|
|
# 执行混合搜索
|
|
es_conn = esClient.es_pool.get_connection()
|
|
try:
|
|
# 向量搜索
|
|
print("\n=== 向量搜索阶段 ===")
|
|
print("1. 文本分词和向量化处理中...")
|
|
query_embedding = esClient.text_to_embedding(query)
|
|
print(f"2. 生成的查询向量维度: {len(query_embedding)}")
|
|
print(f"3. 前3维向量值: {query_embedding[:3]}")
|
|
|
|
print("4. 正在执行Elasticsearch向量搜索...")
|
|
vector_results = es_conn.search(
|
|
index=ES_CONFIG['index_name'],
|
|
body={
|
|
"query": {
|
|
"script_score": {
|
|
"query": {
|
|
"bool": {
|
|
"should": [
|
|
{
|
|
"terms": {
|
|
"tags.tags": query_tags
|
|
}
|
|
}
|
|
],
|
|
"minimum_should_match": 1
|
|
}
|
|
},
|
|
"script": {
|
|
"source": "double score = cosineSimilarity(params.query_vector, 'embedding'); return score >= 0 ? score : 0",
|
|
"params": {"query_vector": query_embedding}
|
|
}
|
|
}
|
|
},
|
|
"size": 3
|
|
}
|
|
)
|
|
print(f"5. 向量搜索结果数量: {len(vector_results['hits']['hits'])}")
|
|
|
|
# 文本精确搜索
|
|
print("\n=== 文本精确搜索阶段 ===")
|
|
print("1. 正在执行Elasticsearch文本精确搜索...")
|
|
text_results = es_conn.search(
|
|
index=ES_CONFIG['index_name'],
|
|
body={
|
|
"query": {
|
|
"bool": {
|
|
"must": [
|
|
{
|
|
"match": {
|
|
"user_input": query
|
|
}
|
|
},
|
|
{
|
|
"terms": {
|
|
"tags.tags": query_tags
|
|
}
|
|
}
|
|
]
|
|
}
|
|
},
|
|
"size": 3
|
|
}
|
|
)
|
|
print(f"2. 文本搜索结果数量: {len(text_results['hits']['hits'])}")
|
|
|
|
# 打印详细结果
|
|
print("\n=== 最终搜索结果 ===")
|
|
|
|
vector_int = 0
|
|
for i, hit in enumerate(vector_results['hits']['hits'], 1):
|
|
if hit['_score'] > 0.4: # 阀值0.4
|
|
print(f" {i}. 文档ID: {hit['_id']}, 相似度分数: {hit['_score']:.2f}")
|
|
print(f" 内容: {hit['_source']['user_input']}")
|
|
vector_int = vector_int + 1
|
|
print(f" 向量搜索结果: {vector_int}条")
|
|
|
|
print("\n文本精确搜索结果:")
|
|
for i, hit in enumerate(text_results['hits']['hits']):
|
|
print(f" {i + 1}. 文档ID: {hit['_id']}, 匹配分数: {hit['_score']:.2f}")
|
|
print(f" 内容: {hit['_source']['user_input']}")
|
|
# print(f" 详细: {hit['_source']['tags']['full_content']}")
|
|
|
|
finally:
|
|
esClient.es_pool.release_connection(es_conn)
|