Blame view

config/utils.py 1.73 KB
9f96d6f3   tangwang   短query不用语义搜索
1
2
3
4
5
6
7
8
9
10
11
12
  """
  Configuration utility functions.
  
  Helper functions for working with SearchConfig objects.
  """
  
  from typing import Dict, List
  from .config_loader import SearchConfig
  
  
  def get_match_fields_for_index(config: SearchConfig, index_name: str = "default") -> List[str]:
      """
33839b37   tangwang   属性值参与搜索:
13
      Generate match fields list with boost from field_boosts.
9f96d6f3   tangwang   短query不用语义搜索
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
      
      Args:
          config: SearchConfig instance
          index_name: Name of the index domain (default: "default")
      
      Returns:
          List of field names with boost, e.g., ["title_zh^3.0", "brief_zh^1.5"]
      """
      # Find the index config
      index_config = None
      for idx in config.indexes:
          if idx.name == index_name:
              index_config = idx
              break
      
      if not index_config:
          return []
      
9f96d6f3   tangwang   短query不用语义搜索
32
33
34
      # Generate match fields with boost
      match_fields = []
      for field_name in index_config.fields:
33839b37   tangwang   属性值参与搜索:
35
36
37
38
39
40
41
42
          # Get field boost from field_boosts dictionary
          field_boost = config.field_boosts.get(field_name, 1.0)
          
          # Combine index boost and field boost
          total_boost = index_config.boost * field_boost
          
          if total_boost != 1.0:
              match_fields.append(f"{field_name}^{total_boost}")
9f96d6f3   tangwang   短query不用语义搜索
43
          else:
33839b37   tangwang   属性值参与搜索:
44
              match_fields.append(field_name)
9f96d6f3   tangwang   短query不用语义搜索
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
      
      return match_fields
  
  
  def get_domain_fields(config: SearchConfig) -> Dict[str, List[str]]:
      """
      Generate domain-specific match fields from all index configs.
      
      Args:
          config: SearchConfig instance
      
      Returns:
          Dictionary mapping domain name to list of match fields
      """
      domain_fields = {}
      for index_config in config.indexes:
          domain_fields[index_config.name] = get_match_fields_for_index(config, index_config.name)
      return domain_fields