github.com/weaviate/weaviate@v1.24.6/adapters/handlers/graphql/local/explore/concepts.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 explore
    13  
    14  import (
    15  	"fmt"
    16  
    17  	"github.com/tailor-inc/graphql"
    18  	"github.com/weaviate/weaviate/adapters/handlers/graphql/descriptions"
    19  	"github.com/weaviate/weaviate/entities/models"
    20  	"github.com/weaviate/weaviate/entities/search"
    21  )
    22  
    23  type ModulesProvider interface {
    24  	ExploreArguments(schema *models.Schema) map[string]*graphql.ArgumentConfig
    25  	CrossClassExtractSearchParams(arguments map[string]interface{}) map[string]interface{}
    26  }
    27  
    28  // Build builds the object containing the Local->Explore Fields, such as Objects
    29  func Build(schema *models.Schema, modulesProvider ModulesProvider) *graphql.Field {
    30  	field := &graphql.Field{
    31  		Name:        "Explore",
    32  		Description: descriptions.LocalExplore,
    33  		Type:        graphql.NewList(exploreObject()),
    34  		Resolve:     newResolver(modulesProvider).resolve,
    35  		Args: graphql.FieldConfigArgument{
    36  			"offset": &graphql.ArgumentConfig{
    37  				Type:        graphql.Int,
    38  				Description: descriptions.Offset,
    39  			},
    40  			"limit": &graphql.ArgumentConfig{
    41  				Type:        graphql.Int,
    42  				Description: descriptions.Limit,
    43  			},
    44  
    45  			"nearVector": nearVectorArgument(),
    46  			"nearObject": nearObjectArgument(),
    47  		},
    48  	}
    49  
    50  	if modulesProvider != nil {
    51  		for name, argument := range modulesProvider.ExploreArguments(schema) {
    52  			field.Args[name] = argument
    53  		}
    54  	}
    55  
    56  	return field
    57  }
    58  
    59  func exploreObject() *graphql.Object {
    60  	getLocalExploreFields := graphql.Fields{
    61  		"className": &graphql.Field{
    62  			Name:        "ExploreClassName",
    63  			Description: descriptions.ClassName,
    64  			Type:        graphql.String,
    65  			Resolve: func(p graphql.ResolveParams) (interface{}, error) {
    66  				vsr, ok := p.Source.(search.Result)
    67  				if !ok {
    68  					return nil, fmt.Errorf("unknown type %T in Explore..className resolver", p.Source)
    69  				}
    70  
    71  				return vsr.ClassName, nil
    72  			},
    73  		},
    74  
    75  		"beacon": &graphql.Field{
    76  			Name:        "ExploreBeacon",
    77  			Description: descriptions.Beacon,
    78  			Type:        graphql.String,
    79  			Resolve: func(p graphql.ResolveParams) (interface{}, error) {
    80  				vsr, ok := p.Source.(search.Result)
    81  				if !ok {
    82  					return nil, fmt.Errorf("unknown type %T in Explore..className resolver", p.Source)
    83  				}
    84  
    85  				return vsr.Beacon, nil
    86  			},
    87  		},
    88  
    89  		"certainty": &graphql.Field{
    90  			Name:        "ExploreCertainty",
    91  			Description: descriptions.Certainty,
    92  			Type:        graphql.Float,
    93  			Resolve: func(p graphql.ResolveParams) (interface{}, error) {
    94  				vsr, ok := p.Source.(search.Result)
    95  				if !ok {
    96  					return nil, fmt.Errorf("unknown type %T in Explore..className resolver", p.Source)
    97  				}
    98  
    99  				return vsr.Certainty, nil
   100  			},
   101  		},
   102  
   103  		"distance": &graphql.Field{
   104  			Name:        "ExploreDistance",
   105  			Description: descriptions.Distance,
   106  			Type:        graphql.Float,
   107  			Resolve: func(p graphql.ResolveParams) (interface{}, error) {
   108  				vsr, ok := p.Source.(search.Result)
   109  				if !ok {
   110  					return nil, fmt.Errorf("unknown type %T in Explore..className resolver", p.Source)
   111  				}
   112  
   113  				return vsr.Dist, nil
   114  			},
   115  		},
   116  	}
   117  
   118  	getLocalExploreFieldsObject := graphql.ObjectConfig{
   119  		Name:        "ExploreObj",
   120  		Fields:      getLocalExploreFields,
   121  		Description: descriptions.LocalExplore,
   122  	}
   123  
   124  	return graphql.NewObject(getLocalExploreFieldsObject)
   125  }
   126  
   127  func nearVectorArgument() *graphql.ArgumentConfig {
   128  	return &graphql.ArgumentConfig{
   129  		// Description: descriptions.GetExplore,
   130  		Type: graphql.NewInputObject(
   131  			graphql.InputObjectConfig{
   132  				Name:   "ExploreNearVectorInpObj",
   133  				Fields: nearVectorFields(),
   134  			},
   135  		),
   136  	}
   137  }
   138  
   139  func nearVectorFields() graphql.InputObjectConfigFieldMap {
   140  	return graphql.InputObjectConfigFieldMap{
   141  		"vector": &graphql.InputObjectFieldConfig{
   142  			Description: descriptions.Certainty,
   143  			Type:        graphql.NewNonNull(graphql.NewList(graphql.Float)),
   144  		},
   145  		"certainty": &graphql.InputObjectFieldConfig{
   146  			Description: descriptions.Certainty,
   147  			Type:        graphql.Float,
   148  		},
   149  		"distance": &graphql.InputObjectFieldConfig{
   150  			Description: descriptions.Distance,
   151  			Type:        graphql.Float,
   152  		},
   153  		"targetVectors": &graphql.InputObjectFieldConfig{
   154  			Description: "Target vectors",
   155  			Type:        graphql.NewList(graphql.String),
   156  		},
   157  	}
   158  }
   159  
   160  func nearObjectArgument() *graphql.ArgumentConfig {
   161  	return &graphql.ArgumentConfig{
   162  		Type: graphql.NewInputObject(
   163  			graphql.InputObjectConfig{
   164  				Name:   "ExploreNearObjectInpObj",
   165  				Fields: nearObjectFields(),
   166  			},
   167  		),
   168  	}
   169  }
   170  
   171  func nearObjectFields() graphql.InputObjectConfigFieldMap {
   172  	return graphql.InputObjectConfigFieldMap{
   173  		"id": &graphql.InputObjectFieldConfig{
   174  			Description: descriptions.ID,
   175  			Type:        graphql.String,
   176  		},
   177  		"beacon": &graphql.InputObjectFieldConfig{
   178  			Description: descriptions.Beacon,
   179  			Type:        graphql.String,
   180  		},
   181  		"certainty": &graphql.InputObjectFieldConfig{
   182  			Description: descriptions.Certainty,
   183  			Type:        graphql.Float,
   184  		},
   185  		"distance": &graphql.InputObjectFieldConfig{
   186  			Description: descriptions.Distance,
   187  			Type:        graphql.Float,
   188  		},
   189  		"targetVectors": &graphql.InputObjectFieldConfig{
   190  			Description: "Target vectors",
   191  			Type:        graphql.NewList(graphql.String),
   192  		},
   193  	}
   194  }