github.com/weaviate/weaviate@v1.24.6/test/acceptance/graphql_resolvers/local_get_with_additional_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  	graphqlhelper "github.com/weaviate/weaviate/test/helper/graphql"
    19  
    20  	"github.com/stretchr/testify/assert"
    21  	"github.com/stretchr/testify/require"
    22  	"github.com/weaviate/weaviate/test/helper"
    23  )
    24  
    25  func gettingObjectsWithAdditionalProps(t *testing.T) {
    26  	t.Run("with vector set", func(t *testing.T) {
    27  		query := `
    28  		{
    29  			Get {
    30  				Company {
    31  					_additional {
    32  						vector
    33  					}
    34  					name
    35  				}
    36  			}
    37  		}
    38  		`
    39  		result := graphqlhelper.AssertGraphQL(t, helper.RootAuth, query)
    40  		companies := result.Get("Get", "Company").AsSlice()
    41  
    42  		require.Greater(t, len(companies), 0)
    43  		for _, comp := range companies {
    44  			vec, ok := comp.(map[string]interface{})["_additional"].(map[string]interface{})["vector"]
    45  			require.True(t, ok)
    46  
    47  			vecSlice, ok := vec.([]interface{})
    48  			require.True(t, ok)
    49  			require.Greater(t, len(vecSlice), 0)
    50  
    51  			asFloat, err := vecSlice[0].(json.Number).Float64()
    52  			require.Nil(t, err)
    53  			assert.True(t, asFloat >= -1)
    54  			assert.True(t, asFloat <= 1)
    55  		}
    56  	})
    57  
    58  	t.Run("with interpretation set", func(t *testing.T) {
    59  		query := `
    60  		{
    61  			Get {
    62  				Company {
    63  					_additional {
    64  						interpretation{
    65  							source {
    66  								concept
    67  							}
    68  						}
    69  					}
    70  					name
    71  				}
    72  			}
    73  		}
    74  		`
    75  		result := graphqlhelper.AssertGraphQL(t, helper.RootAuth, query)
    76  		companies := result.Get("Get", "Company").AsSlice()
    77  
    78  		expected := []interface{}{
    79  			map[string]interface{}{
    80  				"name": "Microsoft Inc.",
    81  				"_additional": map[string]interface{}{
    82  					"interpretation": map[string]interface{}{
    83  						"source": []interface{}{
    84  							map[string]interface{}{
    85  								"concept": "microsoft",
    86  							},
    87  							map[string]interface{}{
    88  								"concept": "inc",
    89  							},
    90  						},
    91  					},
    92  				},
    93  			},
    94  			map[string]interface{}{
    95  				"name": "Microsoft Incorporated",
    96  				"_additional": map[string]interface{}{
    97  					"interpretation": map[string]interface{}{
    98  						"source": []interface{}{
    99  							map[string]interface{}{
   100  								"concept": "microsoft",
   101  							},
   102  							map[string]interface{}{
   103  								"concept": "incorporated",
   104  							},
   105  						},
   106  					},
   107  				},
   108  			},
   109  			map[string]interface{}{
   110  				"name": "Microsoft",
   111  				"_additional": map[string]interface{}{
   112  					"interpretation": map[string]interface{}{
   113  						"source": []interface{}{
   114  							map[string]interface{}{
   115  								"concept": "microsoft",
   116  							},
   117  						},
   118  					},
   119  				},
   120  			},
   121  			map[string]interface{}{
   122  				"name": "Apple Inc.",
   123  				"_additional": map[string]interface{}{
   124  					"interpretation": map[string]interface{}{
   125  						"source": []interface{}{
   126  							map[string]interface{}{
   127  								"concept": "apple",
   128  							},
   129  							map[string]interface{}{
   130  								"concept": "inc",
   131  							},
   132  						},
   133  					},
   134  				},
   135  			},
   136  			map[string]interface{}{
   137  				"name": "Apple Incorporated",
   138  				"_additional": map[string]interface{}{
   139  					"interpretation": map[string]interface{}{
   140  						"source": []interface{}{
   141  							map[string]interface{}{
   142  								"concept": "apple",
   143  							},
   144  							map[string]interface{}{
   145  								"concept": "incorporated",
   146  							},
   147  						},
   148  					},
   149  				},
   150  			},
   151  			map[string]interface{}{
   152  				"name": "Apple",
   153  				"_additional": map[string]interface{}{
   154  					"interpretation": map[string]interface{}{
   155  						"source": []interface{}{
   156  							map[string]interface{}{
   157  								"concept": "apple",
   158  							},
   159  						},
   160  					},
   161  				},
   162  			},
   163  			map[string]interface{}{
   164  				"name": "Google Inc.",
   165  				"_additional": map[string]interface{}{
   166  					"interpretation": map[string]interface{}{
   167  						"source": []interface{}{
   168  							map[string]interface{}{
   169  								"concept": "google",
   170  							},
   171  							map[string]interface{}{
   172  								"concept": "inc",
   173  							},
   174  						},
   175  					},
   176  				},
   177  			},
   178  			map[string]interface{}{
   179  				"name": "Google Incorporated",
   180  				"_additional": map[string]interface{}{
   181  					"interpretation": map[string]interface{}{
   182  						"source": []interface{}{
   183  							map[string]interface{}{
   184  								"concept": "google",
   185  							},
   186  							map[string]interface{}{
   187  								"concept": "incorporated",
   188  							},
   189  						},
   190  					},
   191  				},
   192  			},
   193  			map[string]interface{}{
   194  				"name": "Google",
   195  				"_additional": map[string]interface{}{
   196  					"interpretation": map[string]interface{}{
   197  						"source": []interface{}{
   198  							map[string]interface{}{
   199  								"concept": "google",
   200  							},
   201  						},
   202  					},
   203  				},
   204  			},
   205  		}
   206  
   207  		assert.ElementsMatch(t, expected, companies)
   208  	})
   209  
   210  	t.Run("with _additional nearestNeighbors set", func(t *testing.T) {
   211  		query := `
   212  		{
   213  			Get {
   214  				Company {
   215  					_additional {
   216  						nearestNeighbors{
   217  							neighbors {
   218  								concept
   219  								distance
   220  							}
   221  						}
   222  					}
   223  					name
   224  				}
   225  			}
   226  		}
   227  		`
   228  		result := graphqlhelper.AssertGraphQL(t, helper.RootAuth, query)
   229  		companies := result.Get("Get", "Company").AsSlice()
   230  
   231  		extractNeighbors := func(in interface{}) []interface{} {
   232  			return in.(map[string]interface{})["_additional"].(map[string]interface{})["nearestNeighbors"].(map[string]interface{})["neighbors"].([]interface{})
   233  		}
   234  
   235  		neighbors0 := extractNeighbors(companies[0])
   236  		neighbors1 := extractNeighbors(companies[1])
   237  		neighbors2 := extractNeighbors(companies[2])
   238  
   239  		validateNeighbors(t, neighbors0, neighbors1, neighbors2)
   240  	})
   241  
   242  	t.Run("with _additional featureProjection set", func(t *testing.T) {
   243  		query := `
   244  		{
   245  			Get {
   246  				Company {
   247  					_additional {
   248  						featureProjection(dimensions:3){
   249  							vector
   250  						}
   251  					}
   252  					name
   253  				}
   254  			}
   255  		}
   256  		`
   257  		result := graphqlhelper.AssertGraphQL(t, helper.RootAuth, query)
   258  		companies := result.Get("Get", "Company").AsSlice()
   259  
   260  		extractProjections := func(in interface{}) []interface{} {
   261  			return in.(map[string]interface{})["_additional"].(map[string]interface{})["featureProjection"].(map[string]interface{})["vector"].([]interface{})
   262  		}
   263  
   264  		projections0 := extractProjections(companies[0])
   265  		projections1 := extractProjections(companies[1])
   266  		projections2 := extractProjections(companies[2])
   267  
   268  		validateProjections(t, 3, projections0, projections1, projections2)
   269  	})
   270  
   271  	t.Run("with _additional vector set in reference", func(t *testing.T) {
   272  		query := `
   273  		{
   274  			Get {
   275  				City {
   276  					_additional {
   277  						vector
   278  					}
   279  					inCountry {
   280  						... on Country {
   281  							_additional {
   282  								vector
   283  							}
   284  						}
   285  					}
   286  				}
   287  			}
   288  		}
   289  		`
   290  		result := graphqlhelper.AssertGraphQL(t, helper.RootAuth, query)
   291  		cities := result.Get("Get", "City").AsSlice()
   292  
   293  		vector := cities[0].(map[string]interface{})["inCountry"].([]interface{})[0].(map[string]interface{})["_additional"].(map[string]interface{})["vector"]
   294  
   295  		assert.NotNil(t, vector)
   296  	})
   297  
   298  	t.Run("with _additional creationTimeUnix and lastUpdateTimeUnix set in reference", func(t *testing.T) {
   299  		query := `
   300  		{
   301  			Get {
   302  				City {
   303  					inCountry {
   304  						... on Country {
   305  							_additional {
   306  								creationTimeUnix
   307  								lastUpdateTimeUnix
   308  							}
   309  						}
   310  					}
   311  				}
   312  			}
   313  		}
   314  		`
   315  		result := graphqlhelper.AssertGraphQL(t, helper.RootAuth, query)
   316  		cities := result.Get("Get", "City").AsSlice()
   317  
   318  		created := cities[0].(map[string]interface{})["inCountry"].([]interface{})[0].(map[string]interface{})["_additional"].(map[string]interface{})["creationTimeUnix"]
   319  		updated := cities[0].(map[string]interface{})["inCountry"].([]interface{})[0].(map[string]interface{})["_additional"].(map[string]interface{})["lastUpdateTimeUnix"]
   320  
   321  		assert.NotNil(t, created)
   322  		assert.NotNil(t, updated)
   323  	})
   324  }
   325  
   326  func validateNeighbors(t *testing.T, neighborsGroups ...[]interface{}) {
   327  	for i, group := range neighborsGroups {
   328  		if len(group) == 0 {
   329  			t.Fatalf("group %d: length of neighbors is 0", i)
   330  		}
   331  
   332  		for j, neighbor := range group {
   333  			asMap := neighbor.(map[string]interface{})
   334  			if len(asMap["concept"].(string)) == 0 {
   335  				t.Fatalf("group %d: element %d: concept has length 0", i, j)
   336  			}
   337  		}
   338  	}
   339  }
   340  
   341  func validateProjections(t *testing.T, dims int, vectors ...[]interface{}) {
   342  	for i := range vectors {
   343  		if len(vectors[i]) != dims {
   344  			t.Fatalf("expected feature projection vector to have length 3, got: %d", len(vectors[i]))
   345  		}
   346  	}
   347  }