github.com/weaviate/weaviate@v1.24.6/test/acceptance/vector_distances/explore_test.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 test
    13  
    14  import (
    15  	"encoding/json"
    16  	"testing"
    17  
    18  	"github.com/stretchr/testify/assert"
    19  	"github.com/weaviate/weaviate/entities/models"
    20  	"github.com/weaviate/weaviate/entities/schema"
    21  )
    22  
    23  func testExplore(t *testing.T) {
    24  	className := "L2Squared_Class_2"
    25  	defer deleteObjectClass(t, className)
    26  
    27  	t.Run("create second L2 class", func(t *testing.T) {
    28  		createObjectClass(t, &models.Class{
    29  			Class:      className,
    30  			Vectorizer: "none",
    31  			Properties: []*models.Property{
    32  				{
    33  					Name:         "name",
    34  					DataType:     schema.DataTypeText.PropString(),
    35  					Tokenization: models.PropertyTokenizationWhitespace,
    36  				},
    37  			},
    38  			VectorIndexConfig: map[string]interface{}{
    39  				"distance": "l2-squared",
    40  			},
    41  		})
    42  	})
    43  
    44  	t.Run("create objects for the new class", func(t *testing.T) {
    45  		createObject(t, &models.Object{
    46  			Class: className,
    47  			Properties: map[string]interface{}{
    48  				"name": "object_1",
    49  			},
    50  			Vector: []float32{
    51  				6, 7, 8,
    52  			},
    53  		})
    54  
    55  		createObject(t, &models.Object{
    56  			Class: className,
    57  			Properties: map[string]interface{}{
    58  				"name": "object_2",
    59  			},
    60  			Vector: []float32{
    61  				1, 2, 3,
    62  			},
    63  		})
    64  	})
    65  
    66  	t.Run("run Explore and assert results", func(t *testing.T) {
    67  		res := AssertGraphQL(t, nil, `
    68  		{
    69  			Explore(nearVector: {vector: [3, 4, 5], distance: 365}) {
    70  				distance
    71  				className
    72  			}
    73  		}
    74  		`)
    75  
    76  		explore := res.Get("Explore").AsSlice()
    77  
    78  		expected := []struct {
    79  			className string
    80  			dist      json.Number
    81  		}{
    82  			{className: "L2Squared_Class_2", dist: "12"},
    83  			{className: "L2Squared_Class_2", dist: "27"},
    84  			{className: "L2Squared_Class", dist: "50"},
    85  			{className: "L2Squared_Class", dist: "147"},
    86  			{className: "L2Squared_Class", dist: "365"},
    87  		}
    88  
    89  		for i, item := range explore {
    90  			m, ok := item.(map[string]interface{})
    91  			assert.True(t, ok)
    92  			assert.Equal(t, expected[i].dist, m["distance"])
    93  			assert.Equal(t, expected[i].className, m["className"])
    94  		}
    95  	})
    96  
    97  	t.Run("run Explore with certainty arg and expect failure", func(t *testing.T) {
    98  		res := ErrorGraphQL(t, nil, `
    99  		{
   100  			Explore(nearVector: {vector: [3, 4, 5], certainty: 0.4}) {
   101  				distance
   102  				className
   103  			}
   104  		}
   105  		`)
   106  
   107  		assert.Len(t, res, 1)
   108  		expectedErr := "can't compute and return certainty when vector index is configured with l2-squared distance"
   109  		errorMsg := res[0].Message
   110  		assert.Equal(t, expectedErr, errorMsg)
   111  	})
   112  
   113  	t.Run("run Explore with certainty prop and expect failure", func(t *testing.T) {
   114  		res := ErrorGraphQL(t, nil, `
   115  		{
   116  			Explore(nearVector: {vector: [3, 4, 5], distance: 0.4}) {
   117  				certainty
   118  				className
   119  			}
   120  		}
   121  		`)
   122  
   123  		assert.Len(t, res, 1)
   124  		expectedErr := "can't compute and return certainty when vector index is configured with l2-squared distance"
   125  		errorMsg := res[0].Message
   126  		assert.Equal(t, expectedErr, errorMsg)
   127  	})
   128  }