Search PoC: Add logging (#94567)

pull/93476/head^2
owensmallwood 9 months ago committed by GitHub
parent ce857c2680
commit d96baaa878
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 19
      pkg/storage/unified/resource/index.go

@ -4,7 +4,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log" golog "log"
"os" "os"
"strings" "strings"
@ -12,6 +12,7 @@ import (
"github.com/blevesearch/bleve/v2/analysis/lang/en" "github.com/blevesearch/bleve/v2/analysis/lang/en"
"github.com/blevesearch/bleve/v2/mapping" "github.com/blevesearch/bleve/v2/mapping"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/grafana/grafana/pkg/infra/log"
"golang.org/x/exp/slices" "golang.org/x/exp/slices"
) )
@ -25,6 +26,7 @@ type Index struct {
shards map[string]Shard shards map[string]Shard
opts Opts opts Opts
s *server s *server
log log.Logger
} }
func NewIndex(s *server, opts Opts) *Index { func NewIndex(s *server, opts Opts) *Index {
@ -32,6 +34,7 @@ func NewIndex(s *server, opts Opts) *Index {
s: s, s: s,
opts: opts, opts: opts,
shards: make(map[string]Shard), shards: make(map[string]Shard),
log: log.New("unifiedstorage.search.index"),
} }
return idx return idx
} }
@ -44,6 +47,7 @@ func (i *Index) Init(ctx context.Context) error {
if err != nil { if err != nil {
return err return err
} }
i.log.Info("initial indexing resources", "count", len(list.Items))
for _, obj := range list.Items { for _, obj := range list.Items {
res, err := getResource(obj.Value) res, err := getResource(obj.Value)
@ -56,6 +60,8 @@ func (i *Index) Init(ctx context.Context) error {
return err return err
} }
i.log.Info("indexing resource for tenant", "res", res, "tenant", tenant(res))
var jsonDoc interface{} var jsonDoc interface{}
err = json.Unmarshal(obj.Value, &jsonDoc) err = json.Unmarshal(obj.Value, &jsonDoc)
if err != nil { if err != nil {
@ -85,6 +91,7 @@ func (i *Index) Index(ctx context.Context, data *Data) error {
return err return err
} }
tenant := tenant(res) tenant := tenant(res)
i.log.Info("indexing resource for tenant", "res", res, "tenant", tenant)
shard, err := i.getShard(tenant) shard, err := i.getShard(tenant)
if err != nil { if err != nil {
return err return err
@ -121,6 +128,11 @@ func (i *Index) Search(ctx context.Context, tenant string, query string, limit i
if err != nil { if err != nil {
return nil, err return nil, err
} }
docCount, err := shard.index.DocCount()
if err != nil {
return nil, err
}
i.log.Info("got index for tenant", "tenant", tenant, "docCount", docCount)
// use 10 as a default limit for now // use 10 as a default limit for now
if limit <= 0 { if limit <= 0 {
@ -133,12 +145,15 @@ func (i *Index) Search(ctx context.Context, tenant string, query string, limit i
req.Fields = []string{"*"} // return all indexed fields in search results req.Fields = []string{"*"} // return all indexed fields in search results
i.log.Info("searching index", "query", query, "tenant", tenant)
res, err := shard.index.Search(req) res, err := shard.index.Search(req)
if err != nil { if err != nil {
return nil, err return nil, err
} }
hits := res.Hits hits := res.Hits
i.log.Info("got search results", "hits", hits)
results := make([]SearchSummary, len(hits)) results := make([]SearchSummary, len(hits))
for resKey, hit := range hits { for resKey, hit := range hits {
searchSummary := SearchSummary{} searchSummary := SearchSummary{}
@ -203,7 +218,7 @@ func createFileIndex() (bleve.Index, string, error) {
indexPath := fmt.Sprintf("%s%s.bleve", os.TempDir(), uuid.New().String()) indexPath := fmt.Sprintf("%s%s.bleve", os.TempDir(), uuid.New().String())
index, err := bleve.New(indexPath, createIndexMappings()) index, err := bleve.New(indexPath, createIndexMappings())
if err != nil { if err != nil {
log.Fatalf("Failed to create index: %v", err) golog.Fatalf("Failed to create index: %v", err)
} }
return index, indexPath, err return index, indexPath, err
} }

Loading…
Cancel
Save