Blame view

search/searcher.py 10.8 KB
be52af70   tangwang   first commit
1
2
3
4
5
6
7
8
9
10
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
  """
  Main Searcher module - executes search queries against Elasticsearch.
  
  Handles query parsing, boolean expressions, ranking, and result formatting.
  """
  
  from typing import Dict, Any, List, Optional
  import time
  
  from config import CustomerConfig
  from utils.es_client import ESClient
  from query import QueryParser, ParsedQuery
  from indexer import MappingGenerator
  from .boolean_parser import BooleanParser, QueryNode
  from .es_query_builder import ESQueryBuilder
  from .ranking_engine import RankingEngine
  
  
  class SearchResult:
      """Container for search results."""
  
      def __init__(
          self,
          hits: List[Dict[str, Any]],
          total: int,
          max_score: float,
          took_ms: int,
          aggregations: Optional[Dict[str, Any]] = None,
          query_info: Optional[Dict[str, Any]] = None
      ):
          self.hits = hits
          self.total = total
          self.max_score = max_score
          self.took_ms = took_ms
          self.aggregations = aggregations or {}
          self.query_info = query_info or {}
  
      def to_dict(self) -> Dict[str, Any]:
          """Convert to dictionary representation."""
          return {
              "hits": self.hits,
              "total": self.total,
              "max_score": self.max_score,
              "took_ms": self.took_ms,
              "aggregations": self.aggregations,
              "query_info": self.query_info
          }
  
  
  class Searcher:
      """
      Main search engine class.
  
      Handles:
      - Query parsing and translation
      - Boolean expression parsing
      - ES query building
      - Result ranking and formatting
      """
  
      def __init__(
          self,
          config: CustomerConfig,
          es_client: ESClient,
          query_parser: Optional[QueryParser] = None
      ):
          """
          Initialize searcher.
  
          Args:
              config: Customer configuration
              es_client: Elasticsearch client
              query_parser: Query parser (created if not provided)
          """
          self.config = config
          self.es_client = es_client
          self.query_parser = query_parser or QueryParser(config)
  
          # Initialize components
          self.boolean_parser = BooleanParser()
          self.ranking_engine = RankingEngine(config.ranking.expression)
  
          # Get mapping info
          mapping_gen = MappingGenerator(config)
          self.match_fields = mapping_gen.get_match_fields_for_domain("default")
          self.text_embedding_field = mapping_gen.get_text_embedding_field()
          self.image_embedding_field = mapping_gen.get_image_embedding_field()
  
          # Query builder
          self.query_builder = ESQueryBuilder(
              index_name=config.es_index_name,
              match_fields=self.match_fields,
              text_embedding_field=self.text_embedding_field,
              image_embedding_field=self.image_embedding_field
          )
  
      def search(
          self,
          query: str,
          size: int = 10,
          from_: int = 0,
          filters: Optional[Dict[str, Any]] = None,
          enable_translation: bool = True,
          enable_embedding: bool = True,
          enable_rerank: bool = True,
          min_score: Optional[float] = None
      ) -> SearchResult:
          """
          Execute search query.
  
          Args:
              query: Search query string
              size: Number of results to return
              from_: Offset for pagination
              filters: Additional filters (field: value pairs)
              enable_translation: Whether to enable query translation
              enable_embedding: Whether to use semantic search
              enable_rerank: Whether to apply custom ranking
              min_score: Minimum score threshold
  
          Returns:
              SearchResult object
          """
          start_time = time.time()
  
          print(f"\n{'='*60}")
          print(f"[Searcher] Starting search for: '{query}'")
          print(f"{'='*60}")
  
          # Step 1: Parse query
          parsed_query = self.query_parser.parse(
              query,
              generate_vector=enable_embedding
          )
  
          # Step 2: Check if boolean expression
          query_node = None
          if self.boolean_parser.is_simple_query(parsed_query.rewritten_query):
              # Simple query
              query_text = parsed_query.rewritten_query
          else:
              # Complex boolean query
              query_node = self.boolean_parser.parse(parsed_query.rewritten_query)
              query_text = parsed_query.rewritten_query
              print(f"[Searcher] Parsed boolean expression: {query_node}")
  
          # Step 3: Build ES query
          es_query = self.query_builder.build_query(
              query_text=query_text,
              query_vector=parsed_query.query_vector if enable_embedding else None,
              query_node=query_node,
              filters=filters,
              size=size,
              from_=from_,
              enable_knn=enable_embedding and parsed_query.query_vector is not None,
              min_score=min_score
          )
  
          # Add SPU collapse if configured
          if self.config.spu_config.enabled:
              es_query = self.query_builder.add_spu_collapse(
                  es_query,
                  self.config.spu_config.spu_field,
                  self.config.spu_config.inner_hits_size
              )
  
          # Add aggregations for faceted search
          if filters:
              agg_fields = [f"{k}_keyword" for k in filters.keys() if f"{k}_keyword" in [f.name for f in self.config.fields]]
              if agg_fields:
                  es_query = self.query_builder.add_aggregations(es_query, agg_fields)
  
          print(f"[Searcher] ES Query:")
          import json
          print(json.dumps(es_query, indent=2))
  
          # Step 4: Execute search
          print(f"[Searcher] Executing ES query...")
          es_response = self.es_client.search(
              index_name=self.config.es_index_name,
              body=es_query,
              size=size,
              from_=from_
          )
  
          # Step 5: Process results
          hits = []
          if 'hits' in es_response and 'hits' in es_response['hits']:
              for hit in es_response['hits']['hits']:
                  result_doc = {
                      '_id': hit['_id'],
                      '_score': hit['_score'],
                      '_source': hit['_source']
                  }
  
                  # Apply custom ranking if enabled
                  if enable_rerank:
                      base_score = hit['_score']
                      knn_score = None
  
                      # Check if KNN was used
                      if 'knn' in es_query:
                          # KNN score would be in the combined score
                          # For simplicity, extract from score
                          knn_score = base_score * 0.2  # Approximate based on our formula
  
                      custom_score = self.ranking_engine.calculate_score(
                          hit,
                          base_score,
                          knn_score
                      )
                      result_doc['_custom_score'] = custom_score
                      result_doc['_original_score'] = base_score
  
                  hits.append(result_doc)
  
              # Re-sort by custom score if reranking enabled
              if enable_rerank:
                  hits.sort(key=lambda x: x.get('_custom_score', x['_score']), reverse=True)
  
          # Extract total and max_score
          total = es_response.get('hits', {}).get('total', {})
          if isinstance(total, dict):
              total_value = total.get('value', 0)
          else:
              total_value = total
  
          max_score = es_response.get('hits', {}).get('max_score', 0.0)
  
          # Extract aggregations
          aggregations = es_response.get('aggregations', {})
  
          # Calculate elapsed time
          elapsed_ms = int((time.time() - start_time) * 1000)
  
          # Build result
          result = SearchResult(
              hits=hits,
              total=total_value,
              max_score=max_score,
              took_ms=elapsed_ms,
              aggregations=aggregations,
              query_info=parsed_query.to_dict()
          )
  
          print(f"[Searcher] Search complete: {total_value} results in {elapsed_ms}ms")
          print(f"{'='*60}\n")
  
          return result
  
      def search_by_image(
          self,
          image_url: str,
          size: int = 10,
          filters: Optional[Dict[str, Any]] = None
      ) -> SearchResult:
          """
          Search by image similarity.
  
          Args:
              image_url: URL of query image
              size: Number of results
              filters: Additional filters
  
          Returns:
              SearchResult object
          """
          if not self.image_embedding_field:
              raise ValueError("Image embedding field not configured")
  
          # Generate image embedding
          from embeddings import CLIPImageEncoder
          image_encoder = CLIPImageEncoder()
          image_vector = image_encoder.encode_image_from_url(image_url)
  
          if image_vector is None:
              raise ValueError(f"Failed to encode image: {image_url}")
  
          # Build KNN query
          es_query = {
              "size": size,
              "knn": {
                  "field": self.image_embedding_field,
                  "query_vector": image_vector.tolist(),
                  "k": size,
                  "num_candidates": size * 10
              }
          }
  
          if filters:
              es_query["query"] = {
                  "bool": {
                      "filter": self.query_builder._build_filters(filters)
                  }
              }
  
          # Execute search
          es_response = self.es_client.search(
              index_name=self.config.es_index_name,
              body=es_query,
              size=size
          )
  
          # Process results (similar to text search)
          hits = []
          if 'hits' in es_response and 'hits' in es_response['hits']:
              for hit in es_response['hits']['hits']:
                  hits.append({
                      '_id': hit['_id'],
                      '_score': hit['_score'],
                      '_source': hit['_source']
                  })
  
          total = es_response.get('hits', {}).get('total', {})
          if isinstance(total, dict):
              total_value = total.get('value', 0)
          else:
              total_value = total
  
          return SearchResult(
              hits=hits,
              total=total_value,
              max_score=es_response.get('hits', {}).get('max_score', 0.0),
              took_ms=es_response.get('took', 0),
              query_info={'image_url': image_url, 'search_type': 'image_similarity'}
          )
  
      def get_document(self, doc_id: str) -> Optional[Dict[str, Any]]:
          """
          Get single document by ID.
  
          Args:
              doc_id: Document ID
  
          Returns:
              Document or None if not found
          """
          try:
              response = self.es_client.client.get(
                  index=self.config.es_index_name,
                  id=doc_id
              )
              return response.get('_source')
          except Exception as e:
              print(f"[Searcher] Failed to get document {doc_id}: {e}")
              return None