Search searches for endpoints matching the query.
(query string)
| 329 | |
| 330 | // Search searches for endpoints matching the query. |
| 331 | func (idx *OpenAPIIndex) Search(query string) []*EndpointInfo { |
| 332 | queryKeywords := extractKeywords(query) |
| 333 | if len(queryKeywords) == 0 { |
| 334 | return nil |
| 335 | } |
| 336 | |
| 337 | // Score each endpoint by number of matching keywords |
| 338 | scores := make(map[*EndpointInfo]int) |
| 339 | for _, kw := range queryKeywords { |
| 340 | for _, ep := range idx.keywords[kw] { |
| 341 | scores[ep]++ |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | // Also do substring matching on operationID, summary, method, and service |
| 346 | queryLower := strings.ToLower(query) |
| 347 | for i := range idx.endpoints { |
| 348 | ep := &idx.endpoints[i] |
| 349 | methodLower := strings.ToLower(ep.Method) |
| 350 | serviceLower := strings.ToLower(ep.Service) |
| 351 | matched := false |
| 352 | |
| 353 | // Exact method name match gets highest boost |
| 354 | if methodLower == queryLower { |
| 355 | scores[ep] += 10 |
| 356 | matched = true |
| 357 | } else if strings.Contains(methodLower, queryLower) { |
| 358 | scores[ep] += 5 |
| 359 | matched = true |
| 360 | } |
| 361 | |
| 362 | // Service name match (e.g., "sql" matches "SQLService") |
| 363 | if strings.Contains(serviceLower, queryLower) { |
| 364 | scores[ep] += 4 |
| 365 | matched = true |
| 366 | } |
| 367 | |
| 368 | // Substring match in operationID |
| 369 | if strings.Contains(strings.ToLower(ep.OperationID), queryLower) { |
| 370 | scores[ep] += 3 |
| 371 | matched = true |
| 372 | } |
| 373 | |
| 374 | // Substring match in summary |
| 375 | if strings.Contains(strings.ToLower(ep.Summary), queryLower) { |
| 376 | scores[ep] += 2 |
| 377 | matched = true |
| 378 | } |
| 379 | |
| 380 | // Boost primary CRUD operations only if there's already a match |
| 381 | if matched && isPrimaryCRUD(ep.Method) { |
| 382 | scores[ep] += 2 |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | // Sort by score descending |
| 387 | type scored struct { |
| 388 | ep *EndpointInfo |