Blame view

test_source_fields.py 3.95 KB
13377199   tangwang   接口优化
1
2
3
4
5
6
7
8
9
  #!/usr/bin/env python3
  """
  测试ES source_fields配置的脚本
  """
  
  import sys
  import os
  sys.path.append(os.path.dirname(os.path.abspath(__file__)))
  
9cb7528e   tangwang   店匠体系数据的搜索:mock da...
10
  from config import ConfigLoader, SearchConfig
13377199   tangwang   接口优化
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
  
  def test_source_fields_config():
      """测试source_fields配置是否正确加载"""
      print("测试ES source_fields配置...")
  
      # 加载配置
      config_loader = ConfigLoader("config/schema")
  
      try:
          # 加载customer1配置
          config = config_loader.load_customer_config("customer1")
          print(f"✓ 成功加载配置: {config.customer_id}")
  
          # 检查source_fields配置
          source_fields = config.query_config.source_fields
          print(f"✓ source_fields配置 ({len(source_fields)}个字段):")
          for i, field in enumerate(source_fields, 1):
              print(f"  {i:2d}. {field}")
  
          # 检查默认字段列表是否包含预期字段
          expected_fields = ["id", "title", "brandName", "price", "image"]
          for field in expected_fields:
              if field in source_fields:
                  print(f"✓ 包含预期字段: {field}")
              else:
                  print(f"⚠ 缺少预期字段: {field}")
  
          return True
  
      except Exception as e:
          print(f"✗ 配置加载失败: {e}")
          return False
  
  def test_es_query_builder():
      """测试ES查询构建器是否正确应用source_fields"""
      print("\n测试ES查询构建器...")
  
      try:
          from search.es_query_builder import ESQueryBuilder
  
          # 测试基础查询构建器
          builder = ESQueryBuilder(
              index_name="test_index",
              match_fields=["title", "content"],
              source_fields=["id", "title", "price"]
          )
  
          # 构建查询
          query = builder.build_query("test query")
  
          print("✓ ES查询构建成功")
          print(f"查询结构:")
          print(f"  size: {query.get('size')}")
          print(f"  _source: {query.get('_source')}")
  
          # 检查_source配置
          if "_source" in query:
              source_config = query["_source"]
              if "includes" in source_config:
                  print(f"✓ _source includes配置正确: {source_config['includes']}")
              else:
                  print("✗ _source配置中缺少includes字段")
          else:
              print("✗ 查询中缺少_source配置")
  
          return True
  
      except Exception as e:
          print(f"✗ ES查询构建器测试失败: {e}")
          import traceback
          traceback.print_exc()
          return False
  
  def test_multilang_query_builder():
      """测试多语言查询构建器"""
      print("\n测试多语言查询构建器...")
  
      try:
          from search.multilang_query_builder import MultiLanguageQueryBuilder
  
          # 加载配置
          config_loader = ConfigLoader("config/schema")
          config = config_loader.load_customer_config("customer1")
  
          # 创建多语言查询构建器
          builder = MultiLanguageQueryBuilder(
              config=config,
              index_name=config.es_index_name,
              text_embedding_field="text_embedding",
              image_embedding_field="image_embedding",
              source_fields=config.query_config.source_fields
          )
  
          print("✓ 多语言查询构建器创建成功")
          print(f"  source_fields配置: {builder.source_fields}")
  
          return True
  
      except Exception as e:
          print(f"✗ 多语言查询构建器测试失败: {e}")
          import traceback
          traceback.print_exc()
          return False
  
  if __name__ == "__main__":
      print("=" * 60)
      print("ES Source Fields 配置测试")
      print("=" * 60)
  
      success = True
  
      # 运行所有测试
      success &= test_source_fields_config()
      success &= test_es_query_builder()
      success &= test_multilang_query_builder()
  
      print("\n" + "=" * 60)
      if success:
          print("✓ 所有测试通过!source_fields配置已正确实现。")
      else:
          print("✗ 部分测试失败,请检查配置和代码。")
      print("=" * 60)