github.com/weaviate/weaviate@v1.24.6/usecases/modulecomponents/arguments/nearText/graphql_extract.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 nearText
    13  
    14  // ExtractNearText arguments, such as "concepts", "moveTo", "moveAwayFrom",
    15  // "limit", etc.
    16  func (g *GraphQLArgumentsProvider) extractNearTextFn(source map[string]interface{}) interface{} {
    17  	var args NearTextParams
    18  
    19  	// keywords is a required argument, so we don't need to check for its existing
    20  	keywords := source["concepts"].([]interface{})
    21  	args.Values = make([]string, len(keywords))
    22  	for i, value := range keywords {
    23  		args.Values[i] = value.(string)
    24  	}
    25  
    26  	// autocorrect is an optional arg, so it could be nil
    27  	autocorrect, ok := source["autocorrect"]
    28  	if ok {
    29  		args.Autocorrect = autocorrect.(bool)
    30  	}
    31  
    32  	// if there's text transformer present and autocorrect set to true
    33  	// perform text transformation operation
    34  	if args.Autocorrect && g.nearTextTransformer != nil {
    35  		if transformedValues, err := g.nearTextTransformer.Transform(args.Values); err == nil {
    36  			args.Values = transformedValues
    37  		}
    38  	}
    39  
    40  	// limit is an optional arg, so it could be nil
    41  	limit, ok := source["limit"]
    42  	if ok {
    43  		// the type is fixed through gql config, no need to catch incorrect type
    44  		// assumption
    45  		args.Limit = limit.(int)
    46  	}
    47  
    48  	certainty, ok := source["certainty"]
    49  	if ok {
    50  		args.Certainty = certainty.(float64)
    51  	}
    52  
    53  	distance, ok := source["distance"]
    54  	if ok {
    55  		args.Distance = distance.(float64)
    56  		args.WithDistance = true
    57  	}
    58  
    59  	// moveTo is an optional arg, so it could be nil
    60  	moveTo, ok := source["moveTo"]
    61  	if ok {
    62  		args.MoveTo = extractMovement(moveTo)
    63  	}
    64  
    65  	// network is an optional arg, so it could be nil
    66  	network, ok := source["network"]
    67  	if ok {
    68  		args.Network = network.(bool)
    69  	}
    70  
    71  	// moveAwayFrom is an optional arg, so it could be nil
    72  	moveAwayFrom, ok := source["moveAwayFrom"]
    73  	if ok {
    74  		args.MoveAwayFrom = extractMovement(moveAwayFrom)
    75  	}
    76  
    77  	// targetVectors is an optional argument, so it could be nil
    78  	targetVectors, ok := source["targetVectors"]
    79  	if ok {
    80  		targetVectorsArray := targetVectors.([]interface{})
    81  		args.TargetVectors = make([]string, len(targetVectorsArray))
    82  		for i, value := range targetVectorsArray {
    83  			args.TargetVectors[i] = value.(string)
    84  		}
    85  	}
    86  
    87  	return &args
    88  }
    89  
    90  func extractMovement(input interface{}) ExploreMove {
    91  	// the type is fixed through gql config, no need to catch incorrect type
    92  	// assumption, all fields are required so we don't need to check for their
    93  	// presence
    94  	moveToMap := input.(map[string]interface{})
    95  	res := ExploreMove{}
    96  	res.Force = float32(moveToMap["force"].(float64))
    97  
    98  	keywords, ok := moveToMap["concepts"].([]interface{})
    99  	if ok {
   100  		res.Values = make([]string, len(keywords))
   101  		for i, value := range keywords {
   102  			res.Values[i] = value.(string)
   103  		}
   104  	}
   105  
   106  	objects, ok := moveToMap["objects"].([]interface{})
   107  	if ok {
   108  		res.Objects = make([]ObjectMove, len(objects))
   109  		for i, value := range objects {
   110  			v, ok := value.(map[string]interface{})
   111  			if ok {
   112  				if v["id"] != nil {
   113  					res.Objects[i].ID = v["id"].(string)
   114  				}
   115  				if v["beacon"] != nil {
   116  					res.Objects[i].Beacon = v["beacon"].(string)
   117  				}
   118  			}
   119  		}
   120  	}
   121  
   122  	return res
   123  }