github.com/weaviate/weaviate@v1.24.6/entities/schema/vector_index_config.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 schema
    13  
    14  import (
    15  	"fmt"
    16  
    17  	"github.com/pkg/errors"
    18  	"github.com/weaviate/weaviate/entities/models"
    19  )
    20  
    21  type VectorIndexConfig interface {
    22  	IndexType() string
    23  	DistanceName() string
    24  }
    25  
    26  func TypeAssertVectorIndex(class *models.Class, targetVectors []string) (VectorIndexConfig, error) {
    27  	if len(class.VectorConfig) == 0 {
    28  		vectorIndexConfig, ok := class.VectorIndexConfig.(VectorIndexConfig)
    29  		if !ok {
    30  			return nil, fmt.Errorf("class '%s' vector index: config is not schema.VectorIndexConfig: %T",
    31  				class.Class, class.VectorIndexConfig)
    32  		}
    33  		return vectorIndexConfig, nil
    34  	} else if len(class.VectorConfig) == 1 {
    35  		var vectorConfig models.VectorConfig
    36  		for _, v := range class.VectorConfig {
    37  			vectorConfig = v
    38  			break
    39  		}
    40  		vectorIndexConfig, ok := vectorConfig.VectorIndexConfig.(VectorIndexConfig)
    41  		if !ok {
    42  			return nil, fmt.Errorf("class '%s' vector index: config is not schema.VectorIndexConfig: %T",
    43  				class.Class, class.VectorIndexConfig)
    44  		}
    45  		return vectorIndexConfig, nil
    46  	} else {
    47  		if len(targetVectors) != 1 {
    48  			return nil, errors.Errorf("multiple vector configs found for class '%s', but no target vector specified", class.Class)
    49  		}
    50  		vectorConfig, ok := class.VectorConfig[targetVectors[0]]
    51  		if !ok {
    52  			return nil, errors.Errorf("vector config not found for target vector: %s", targetVectors[0])
    53  		}
    54  		vectorIndexConfig, ok := vectorConfig.VectorIndexConfig.(VectorIndexConfig)
    55  		if !ok {
    56  			return nil, fmt.Errorf("targetVector '%s' vector index: config is not schema.VectorIndexConfig: %T",
    57  				targetVectors[0], class.VectorIndexConfig)
    58  		}
    59  		return vectorIndexConfig, nil
    60  	}
    61  }