github.com/weaviate/weaviate@v1.24.6/test/acceptance/graphql_resolvers/local_get_with_group_by_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  	"testing"
    16  
    17  	graphqlhelper "github.com/weaviate/weaviate/test/helper/graphql"
    18  	"github.com/weaviate/weaviate/test/helper/journey"
    19  
    20  	"github.com/stretchr/testify/assert"
    21  	"github.com/stretchr/testify/require"
    22  	"github.com/weaviate/weaviate/test/helper"
    23  )
    24  
    25  func groupByObjects(t *testing.T) {
    26  	t.Run("group by: people by city", func(t *testing.T) {
    27  		getGroup := func(value interface{}) map[string]interface{} {
    28  			group := value.(map[string]interface{})["_additional"].(map[string]interface{})["group"].(map[string]interface{})
    29  			return group
    30  		}
    31  		getGroupHits := func(group map[string]interface{}) (string, []string) {
    32  			result := []string{}
    33  			hits := group["hits"].([]interface{})
    34  			for _, hit := range hits {
    35  				additional := hit.(map[string]interface{})["_additional"].(map[string]interface{})
    36  				result = append(result, additional["id"].(string))
    37  			}
    38  			groupedBy := group["groupedBy"].(map[string]interface{})
    39  			groupedByValue := groupedBy["value"].(string)
    40  			return groupedByValue, result
    41  		}
    42  		query := `
    43  		{
    44  			Get{
    45  				Person(
    46  					nearObject:{
    47  						id: "8615585a-2960-482d-b19d-8bee98ade52c"
    48  					}
    49  					groupBy:{
    50  						path:["livesIn"]
    51  						groups:4
    52  						objectsPerGroup: 10
    53  					}
    54  				){
    55  					_additional{
    56  						id
    57  						group{
    58  							groupedBy{value}
    59  							count
    60  							maxDistance
    61  							minDistance
    62  							hits {
    63  								_additional {
    64  									id
    65  									distance
    66  								}
    67  							}
    68  						}
    69  					}
    70  				}
    71  			}
    72  		}
    73  		`
    74  		result := graphqlhelper.AssertGraphQL(t, helper.RootAuth, query)
    75  		groups := result.Get("Get", "Person").AsSlice()
    76  
    77  		require.Len(t, groups, 4)
    78  
    79  		expectedResults := map[string][]string{}
    80  
    81  		groupedBy1 := `weaviate://localhost/City/8f5f8e44-d348-459c-88b1-c1a44bb8f8be`
    82  		expectedGroup1 := []string{
    83  			"8615585a-2960-482d-b19d-8bee98ade52c",
    84  			"3ef44474-b5e5-455d-91dc-d917b5b76165",
    85  			"15d222c9-8c36-464b-bedb-113faa1c1e4c",
    86  		}
    87  		expectedResults[groupedBy1] = expectedGroup1
    88  
    89  		groupedBy2 := `weaviate://localhost/City/9b9cbea5-e87e-4cd0-89af-e2f424fd52d6`
    90  		expectedGroup2 := []string{
    91  			"3ef44474-b5e5-455d-91dc-d917b5b76165",
    92  			"15d222c9-8c36-464b-bedb-113faa1c1e4c",
    93  		}
    94  		expectedResults[groupedBy2] = expectedGroup2
    95  
    96  		groupedBy3 := `weaviate://localhost/City/6ffb03f8-a853-4ec5-a5d8-302e45aaaf13`
    97  		expectedGroup3 := []string{
    98  			"15d222c9-8c36-464b-bedb-113faa1c1e4c",
    99  		}
   100  		expectedResults[groupedBy3] = expectedGroup3
   101  
   102  		groupedBy4 := ""
   103  		expectedGroup4 := []string{
   104  			"5d0fa6ee-21c4-4b46-a735-f0208717837d",
   105  		}
   106  		expectedResults[groupedBy4] = expectedGroup4
   107  
   108  		groupsOrder := []string{groupedBy1, groupedBy2, groupedBy4, groupedBy3}
   109  		for i, current := range groups {
   110  			group := getGroup(current)
   111  			groupedBy, ids := getGroupHits(group)
   112  			assert.Equal(t, groupsOrder[i], groupedBy)
   113  			assert.ElementsMatch(t, expectedResults[groupedBy], ids)
   114  		}
   115  	})
   116  
   117  	t.Run("group by: passages by documents", func(t *testing.T) {
   118  		journey.GroupBySingleAndMultiShardTests(t, "")
   119  	})
   120  }