github.com/weaviate/weaviate@v1.24.6/usecases/traverser/traverser.go (about)

     1  //                           _       _
     2  // __      _____  __ ___   ___  __ _| |_ ___
     3  // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
     4  //  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
     5  //   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
     6  //
     7  //  Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
     8  //
     9  //  CONTACT: hello@weaviate.io
    10  //
    11  
    12  package traverser
    13  
    14  import (
    15  	"context"
    16  
    17  	"github.com/go-openapi/strfmt"
    18  	"github.com/sirupsen/logrus"
    19  	"github.com/weaviate/weaviate/entities/additional"
    20  	"github.com/weaviate/weaviate/entities/aggregation"
    21  	"github.com/weaviate/weaviate/entities/dto"
    22  	"github.com/weaviate/weaviate/entities/models"
    23  	"github.com/weaviate/weaviate/entities/search"
    24  	"github.com/weaviate/weaviate/usecases/config"
    25  	"github.com/weaviate/weaviate/usecases/ratelimiter"
    26  	"github.com/weaviate/weaviate/usecases/schema"
    27  )
    28  
    29  type locks interface {
    30  	LockConnector() (func() error, error)
    31  	LockSchema() (func() error, error)
    32  }
    33  
    34  type authorizer interface {
    35  	Authorize(principal *models.Principal, verb, resource string) error
    36  }
    37  
    38  // Traverser can be used to dynamically traverse the knowledge graph
    39  type Traverser struct {
    40  	config                  *config.WeaviateConfig
    41  	locks                   locks
    42  	logger                  logrus.FieldLogger
    43  	authorizer              authorizer
    44  	vectorSearcher          VectorSearcher
    45  	explorer                explorer
    46  	schemaGetter            schema.SchemaGetter
    47  	nearParamsVector        *nearParamsVector
    48  	targetVectorParamHelper *TargetVectorParamHelper
    49  	metrics                 *Metrics
    50  	ratelimiter             *ratelimiter.Limiter
    51  }
    52  
    53  type VectorSearcher interface {
    54  	Aggregate(ctx context.Context, params aggregation.Params) (*aggregation.Result, error)
    55  	Object(ctx context.Context, className string, id strfmt.UUID,
    56  		props search.SelectProperties, additional additional.Properties,
    57  		properties *additional.ReplicationProperties, tenant string) (*search.Result, error)
    58  	ObjectsByID(ctx context.Context, id strfmt.UUID, props search.SelectProperties,
    59  		additional additional.Properties, tenant string) (search.Results, error)
    60  }
    61  
    62  type explorer interface {
    63  	GetClass(ctx context.Context, params dto.GetParams) ([]interface{}, error)
    64  	CrossClassVectorSearch(ctx context.Context, params ExploreParams) ([]search.Result, error)
    65  }
    66  
    67  // NewTraverser to traverse the knowledge graph
    68  func NewTraverser(config *config.WeaviateConfig, locks locks,
    69  	logger logrus.FieldLogger, authorizer authorizer,
    70  	vectorSearcher VectorSearcher,
    71  	explorer explorer, schemaGetter schema.SchemaGetter,
    72  	modulesProvider ModulesProvider,
    73  	metrics *Metrics, maxGetRequests int,
    74  ) *Traverser {
    75  	return &Traverser{
    76  		config:                  config,
    77  		locks:                   locks,
    78  		logger:                  logger,
    79  		authorizer:              authorizer,
    80  		vectorSearcher:          vectorSearcher,
    81  		explorer:                explorer,
    82  		schemaGetter:            schemaGetter,
    83  		nearParamsVector:        newNearParamsVector(modulesProvider, vectorSearcher),
    84  		targetVectorParamHelper: NewTargetParamHelper(),
    85  		metrics:                 metrics,
    86  		ratelimiter:             ratelimiter.New(maxGetRequests),
    87  	}
    88  }
    89  
    90  // TraverserRepo describes the dependencies of the Traverser UC to the
    91  // connected database
    92  type TraverserRepo interface {
    93  	GetClass(context.Context, *dto.GetParams) (interface{}, error)
    94  	Aggregate(context.Context, *aggregation.Params) (interface{}, error)
    95  }
    96  
    97  // SearchResult is a single search result. See wrapping Search Results for the Type
    98  type SearchResult struct {
    99  	Name      string
   100  	Certainty float32
   101  }
   102  
   103  // SearchResults is grouping of SearchResults for a SchemaSearch
   104  type SearchResults struct {
   105  	Type    SearchType
   106  	Results []SearchResult
   107  }
   108  
   109  // Len of the result set
   110  func (r SearchResults) Len() int {
   111  	return len(r.Results)
   112  }