bleve_common.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package nginx_log
  2. import (
  3. "context"
  4. "fmt"
  5. "sort"
  6. "github.com/blevesearch/bleve/v2"
  7. "github.com/blevesearch/bleve/v2/search"
  8. "github.com/blevesearch/bleve/v2/search/query"
  9. )
  10. // BleveFieldExtractor defines how to extract a field value from a search hit
  11. type BleveFieldExtractor func(hit *search.DocumentMatch) string
  12. // BleveAggregationResult represents the result of field aggregation
  13. type BleveAggregationResult struct {
  14. Field string
  15. Count int
  16. TotalRequests int
  17. Percent float64
  18. }
  19. // aggregateFieldFromBleve performs field aggregation using Bleve search
  20. func (s *BleveStatsService) aggregateFieldFromBleve(ctx context.Context, baseQuery query.Query, fieldName string, extractor BleveFieldExtractor) ([]BleveAggregationResult, error) {
  21. fieldCount := make(map[string]int)
  22. totalRequests := 0
  23. // Query all entries
  24. searchReq := bleve.NewSearchRequest(baseQuery)
  25. searchReq.Size = 10000
  26. searchReq.Fields = []string{fieldName}
  27. from := 0
  28. for {
  29. searchReq.From = from
  30. searchResult, err := s.indexer.index.Search(searchReq)
  31. if err != nil {
  32. return nil, fmt.Errorf("failed to search logs: %w", err)
  33. }
  34. if len(searchResult.Hits) == 0 {
  35. break
  36. }
  37. for _, hit := range searchResult.Hits {
  38. fieldValue := extractor(hit)
  39. if fieldValue == "" {
  40. fieldValue = "Unknown"
  41. }
  42. fieldCount[fieldValue]++
  43. totalRequests++
  44. }
  45. from += len(searchResult.Hits)
  46. if uint64(from) >= searchResult.Total {
  47. break
  48. }
  49. }
  50. // Convert to slice and sort
  51. var results []BleveAggregationResult
  52. for field, count := range fieldCount {
  53. percent := 0.0
  54. if totalRequests > 0 {
  55. percent = float64(count) * 100.0 / float64(totalRequests)
  56. }
  57. results = append(results, BleveAggregationResult{
  58. Field: field,
  59. Count: count,
  60. TotalRequests: totalRequests,
  61. Percent: percent,
  62. })
  63. }
  64. // Sort by count (descending)
  65. sort.Slice(results, func(i, j int) bool {
  66. return results[i].Count > results[j].Count
  67. })
  68. return results, nil
  69. }
  70. // Standard field extractors
  71. func extractPathField(hit *search.DocumentMatch) string {
  72. if pathField, ok := hit.Fields["path"]; ok {
  73. if path, ok := pathField.(string); ok && path != "" {
  74. return path
  75. }
  76. }
  77. return ""
  78. }
  79. func extractBrowserField(hit *search.DocumentMatch) string {
  80. if browserField, ok := hit.Fields["browser"]; ok {
  81. if browser, ok := browserField.(string); ok && browser != "" {
  82. return browser
  83. }
  84. }
  85. return ""
  86. }
  87. func extractOSField(hit *search.DocumentMatch) string {
  88. if osField, ok := hit.Fields["os"]; ok {
  89. if os, ok := osField.(string); ok && os != "" {
  90. return os
  91. }
  92. }
  93. return ""
  94. }
  95. func extractDeviceField(hit *search.DocumentMatch) string {
  96. if deviceField, ok := hit.Fields["device_type"]; ok {
  97. if device, ok := deviceField.(string); ok && device != "" {
  98. return device
  99. }
  100. }
  101. return ""
  102. }