...

Source file src/github.com/redis/go-redis/v9/doctests/vec_set_test.go

Documentation: github.com/redis/go-redis/v9/doctests

     1  // EXAMPLE: vecset_tutorial
     2  // HIDE_START
     3  package example_commands_test
     4  
     5  import (
     6  	"context"
     7  	"fmt"
     8  	"sort"
     9  
    10  	"github.com/redis/go-redis/v9"
    11  )
    12  
    13  // HIDE_END
    14  
    15  func ExampleClient_vectorset() {
    16  	ctx := context.Background()
    17  
    18  	rdb := redis.NewClient(&redis.Options{
    19  		Addr:     "localhost:6379",
    20  		Password: "", // no password set
    21  		DB:       0,  // use default DB
    22  	})
    23  
    24  	defer rdb.Close()
    25  	// REMOVE_START
    26  	rdb.Del(ctx, "points", "quantSetQ8", "quantSetNoQ", "quantSetBin", "setNotReduced", "setReduced")
    27  	// REMOVE_END
    28  
    29  	// STEP_START vadd
    30  	res1, err := rdb.VAdd(ctx, "points", "pt:A",
    31  		&redis.VectorValues{Val: []float64{1.0, 1.0}},
    32  	).Result()
    33  
    34  	if err != nil {
    35  		panic(err)
    36  	}
    37  
    38  	fmt.Println(res1) // >>> true
    39  
    40  	res2, err := rdb.VAdd(ctx, "points", "pt:B",
    41  		&redis.VectorValues{Val: []float64{-1.0, -1.0}},
    42  	).Result()
    43  
    44  	if err != nil {
    45  		panic(err)
    46  	}
    47  
    48  	fmt.Println(res2) // >>> true
    49  
    50  	res3, err := rdb.VAdd(ctx, "points", "pt:C",
    51  		&redis.VectorValues{Val: []float64{-1.0, 1.0}},
    52  	).Result()
    53  
    54  	if err != nil {
    55  		panic(err)
    56  	}
    57  
    58  	fmt.Println(res3) // >>> true
    59  
    60  	res4, err := rdb.VAdd(ctx, "points", "pt:D",
    61  		&redis.VectorValues{Val: []float64{1.0, -1.0}},
    62  	).Result()
    63  
    64  	if err != nil {
    65  		panic(err)
    66  	}
    67  
    68  	fmt.Println(res4) // >>> true
    69  
    70  	res5, err := rdb.VAdd(ctx, "points", "pt:E",
    71  		&redis.VectorValues{Val: []float64{1.0, 0.0}},
    72  	).Result()
    73  
    74  	if err != nil {
    75  		panic(err)
    76  	}
    77  
    78  	fmt.Println(res5) // >>> true
    79  
    80  	res6, err := rdb.Type(ctx, "points").Result()
    81  
    82  	if err != nil {
    83  		panic(err)
    84  	}
    85  
    86  	fmt.Println(res6) // >>> vectorset
    87  	// STEP_END
    88  
    89  	// STEP_START vcardvdim
    90  	res7, err := rdb.VCard(ctx, "points").Result()
    91  
    92  	if err != nil {
    93  		panic(err)
    94  	}
    95  
    96  	fmt.Println(res7) // >>> 5
    97  
    98  	res8, err := rdb.VDim(ctx, "points").Result()
    99  
   100  	if err != nil {
   101  		panic(err)
   102  	}
   103  
   104  	fmt.Println(res8) // >>> 2
   105  	// STEP_END
   106  
   107  	// STEP_START vemb
   108  	res9, err := rdb.VEmb(ctx, "points", "pt:A", false).Result()
   109  
   110  	if err != nil {
   111  		panic(err)
   112  	}
   113  
   114  	fmt.Println(res9) // >>> [0.9999999403953552 0.9999999403953552]
   115  
   116  	res10, err := rdb.VEmb(ctx, "points", "pt:B", false).Result()
   117  
   118  	if err != nil {
   119  		panic(err)
   120  	}
   121  
   122  	fmt.Println(res10) // >>> [-0.9999999403953552 -0.9999999403953552]
   123  
   124  	res11, err := rdb.VEmb(ctx, "points", "pt:C", false).Result()
   125  
   126  	if err != nil {
   127  		panic(err)
   128  	}
   129  
   130  	fmt.Println(res11) // >>> [-0.9999999403953552 0.9999999403953552]
   131  
   132  	res12, err := rdb.VEmb(ctx, "points", "pt:D", false).Result()
   133  
   134  	if err != nil {
   135  		panic(err)
   136  	}
   137  
   138  	fmt.Println(res12) // >>> [0.9999999403953552 -0.9999999403953552]
   139  
   140  	res13, err := rdb.VEmb(ctx, "points", "pt:E", false).Result()
   141  
   142  	if err != nil {
   143  		panic(err)
   144  	}
   145  
   146  	fmt.Println(res13) // >>> [1 0]
   147  	// STEP_END
   148  
   149  	// STEP_START attr
   150  	attrs := map[string]interface{}{
   151  		"name":        "Point A",
   152  		"description": "First point added",
   153  	}
   154  
   155  	res14, err := rdb.VSetAttr(ctx, "points", "pt:A", attrs).Result()
   156  
   157  	if err != nil {
   158  		panic(err)
   159  	}
   160  
   161  	fmt.Println(res14) // >>> true
   162  
   163  	res15, err := rdb.VGetAttr(ctx, "points", "pt:A").Result()
   164  
   165  	if err != nil {
   166  		panic(err)
   167  	}
   168  
   169  	fmt.Println(res15)
   170  	// >>> {"description":"First point added","name":"Point A"}
   171  
   172  	res16, err := rdb.VClearAttributes(ctx, "points", "pt:A").Result()
   173  
   174  	if err != nil {
   175  		panic(err)
   176  	}
   177  
   178  	fmt.Println(res16) // >>> true
   179  
   180  	// `VGetAttr()` returns an error if the attribute doesn't exist.
   181  	_, err = rdb.VGetAttr(ctx, "points", "pt:A").Result()
   182  
   183  	if err != nil {
   184  		fmt.Println(err)
   185  	}
   186  	// STEP_END
   187  
   188  	// STEP_START vrem
   189  	res18, err := rdb.VAdd(ctx, "points", "pt:F",
   190  		&redis.VectorValues{Val: []float64{0.0, 0.0}},
   191  	).Result()
   192  
   193  	if err != nil {
   194  		panic(err)
   195  	}
   196  
   197  	fmt.Println(res18) // >>> true
   198  
   199  	res19, err := rdb.VCard(ctx, "points").Result()
   200  
   201  	if err != nil {
   202  		panic(err)
   203  	}
   204  
   205  	fmt.Println(res19) // >>> 6
   206  
   207  	res20, err := rdb.VRem(ctx, "points", "pt:F").Result()
   208  
   209  	if err != nil {
   210  		panic(err)
   211  	}
   212  
   213  	fmt.Println(res20) // >>> true
   214  
   215  	res21, err := rdb.VCard(ctx, "points").Result()
   216  
   217  	if err != nil {
   218  		panic(err)
   219  	}
   220  
   221  	fmt.Println(res21) // >>> 5
   222  	// STEP_END
   223  
   224  	// STEP_START vsim_basic
   225  	res22, err := rdb.VSim(ctx, "points",
   226  		&redis.VectorValues{Val: []float64{0.9, 0.1}},
   227  	).Result()
   228  
   229  	if err != nil {
   230  		panic(err)
   231  	}
   232  
   233  	fmt.Println(res22) // >>> [pt:E pt:A pt:D pt:C pt:B]
   234  	// STEP_END
   235  
   236  	// STEP_START vsim_options
   237  	res23, err := rdb.VSimWithArgsWithScores(
   238  		ctx,
   239  		"points",
   240  		&redis.VectorRef{Name: "pt:A"},
   241  		&redis.VSimArgs{Count: 4},
   242  	).Result()
   243  
   244  	if err != nil {
   245  		panic(err)
   246  	}
   247  
   248  	sort.Slice(res23, func(i, j int) bool {
   249  		return res23[i].Name < res23[j].Name
   250  	})
   251  
   252  	fmt.Println(res23)
   253  	// >>> [{pt:A 1} {pt:C 0.5} {pt:D 0.5} {pt:E 0.8535534143447876}]
   254  	// STEP_END
   255  
   256  	// STEP_START vsim_filter
   257  	// Set attributes for filtering
   258  	res24, err := rdb.VSetAttr(ctx, "points", "pt:A",
   259  		map[string]interface{}{
   260  			"size":  "large",
   261  			"price": 18.99,
   262  		},
   263  	).Result()
   264  
   265  	if err != nil {
   266  		panic(err)
   267  	}
   268  
   269  	fmt.Println(res24) // >>> true
   270  
   271  	res25, err := rdb.VSetAttr(ctx, "points", "pt:B",
   272  		map[string]interface{}{
   273  			"size":  "large",
   274  			"price": 35.99,
   275  		},
   276  	).Result()
   277  
   278  	if err != nil {
   279  		panic(err)
   280  	}
   281  
   282  	fmt.Println(res25) // >>> true
   283  
   284  	res26, err := rdb.VSetAttr(ctx, "points", "pt:C",
   285  		map[string]interface{}{
   286  			"size":  "large",
   287  			"price": 25.99,
   288  		},
   289  	).Result()
   290  
   291  	if err != nil {
   292  		panic(err)
   293  	}
   294  
   295  	fmt.Println(res26) // >>> true
   296  
   297  	res27, err := rdb.VSetAttr(ctx, "points", "pt:D",
   298  		map[string]interface{}{
   299  			"size":  "small",
   300  			"price": 21.00,
   301  		},
   302  	).Result()
   303  
   304  	if err != nil {
   305  		panic(err)
   306  	}
   307  
   308  	fmt.Println(res27) // >>> true
   309  
   310  	res28, err := rdb.VSetAttr(ctx, "points", "pt:E",
   311  		map[string]interface{}{
   312  			"size":  "small",
   313  			"price": 17.75,
   314  		},
   315  	).Result()
   316  
   317  	if err != nil {
   318  		panic(err)
   319  	}
   320  
   321  	fmt.Println(res28) // >>> true
   322  
   323  	// Return elements in order of distance from point A whose
   324  	// `size` attribute is `large`.
   325  	res29, err := rdb.VSimWithArgs(ctx, "points",
   326  		&redis.VectorRef{Name: "pt:A"},
   327  		&redis.VSimArgs{Filter: `.size == "large"`},
   328  	).Result()
   329  
   330  	if err != nil {
   331  		panic(err)
   332  	}
   333  
   334  	fmt.Println(res29) // >>> [pt:A pt:C pt:B]
   335  
   336  	// Return elements in order of distance from point A whose size is
   337  	// `large` and whose price is greater than 20.00.
   338  	res30, err := rdb.VSimWithArgs(ctx, "points",
   339  		&redis.VectorRef{Name: "pt:A"},
   340  		&redis.VSimArgs{Filter: `.size == "large" && .price > 20.00`},
   341  	).Result()
   342  
   343  	if err != nil {
   344  		panic(err)
   345  	}
   346  
   347  	fmt.Println(res30) // >>> [pt:C pt:B]
   348  	// STEP_END
   349  
   350  	// Output:
   351  	// true
   352  	// true
   353  	// true
   354  	// true
   355  	// true
   356  	// vectorset
   357  	// 5
   358  	// 2
   359  	// [0.9999999403953552 0.9999999403953552]
   360  	// [-0.9999999403953552 -0.9999999403953552]
   361  	// [-0.9999999403953552 0.9999999403953552]
   362  	// [0.9999999403953552 -0.9999999403953552]
   363  	// [1 0]
   364  	// true
   365  	// {"description":"First point added","name":"Point A"}
   366  	// true
   367  	// redis: nil
   368  	// true
   369  	// 6
   370  	// true
   371  	// 5
   372  	// [pt:E pt:A pt:D pt:C pt:B]
   373  	// [{pt:A 1} {pt:C 0.5} {pt:D 0.5} {pt:E 0.8535534143447876}]
   374  	// true
   375  	// true
   376  	// true
   377  	// true
   378  	// true
   379  	// [pt:A pt:C pt:B]
   380  	// [pt:C pt:B]
   381  }
   382  
   383  func ExampleClient_vectorset_quantization() {
   384  	ctx := context.Background()
   385  
   386  	rdb := redis.NewClient(&redis.Options{
   387  		Addr:     "localhost:6379",
   388  		Password: "", // no password set
   389  		DB:       0,  // use default DB
   390  	})
   391  
   392  	defer rdb.Close()
   393  	// REMOVE_START
   394  	rdb.Del(ctx, "quantSetQ8", "quantSetNoQ", "quantSetBin")
   395  	// REMOVE_END
   396  
   397  	// STEP_START add_quant
   398  	// Add with Q8 quantization
   399  	vecQ := &redis.VectorValues{Val: []float64{1.262185, 1.958231}}
   400  
   401  	res1, err := rdb.VAddWithArgs(ctx, "quantSetQ8", "quantElement", vecQ,
   402  		&redis.VAddArgs{
   403  			Q8: true,
   404  		},
   405  	).Result()
   406  
   407  	if err != nil {
   408  		panic(err)
   409  	}
   410  
   411  	fmt.Println(res1) // >>> true
   412  
   413  	embQ8, err := rdb.VEmb(ctx, "quantSetQ8", "quantElement", false).Result()
   414  
   415  	if err != nil {
   416  		panic(err)
   417  	}
   418  
   419  	fmt.Printf("Q8 embedding: %v\n", embQ8)
   420  	// >>> Q8 embedding: [1.2621850967407227 1.9582309722900391]
   421  
   422  	// Add with NOQUANT option
   423  	res2, err := rdb.VAddWithArgs(ctx, "quantSetNoQ", "quantElement", vecQ,
   424  		&redis.VAddArgs{
   425  			NoQuant: true,
   426  		},
   427  	).Result()
   428  
   429  	if err != nil {
   430  		panic(err)
   431  	}
   432  
   433  	fmt.Println(res2) // >>> true
   434  
   435  	embNoQ, err := rdb.VEmb(ctx, "quantSetNoQ", "quantElement", false).Result()
   436  
   437  	if err != nil {
   438  		panic(err)
   439  	}
   440  
   441  	fmt.Printf("NOQUANT embedding: %v\n", embNoQ)
   442  	// >>> NOQUANT embedding: [1.262185 1.958231]
   443  
   444  	// Add with BIN quantization
   445  	res3, err := rdb.VAddWithArgs(ctx, "quantSetBin", "quantElement", vecQ,
   446  		&redis.VAddArgs{
   447  			Bin: true,
   448  		},
   449  	).Result()
   450  
   451  	if err != nil {
   452  		panic(err)
   453  	}
   454  
   455  	fmt.Println(res3) // >>> true
   456  
   457  	embBin, err := rdb.VEmb(ctx, "quantSetBin", "quantElement", false).Result()
   458  
   459  	if err != nil {
   460  		panic(err)
   461  	}
   462  
   463  	fmt.Printf("BIN embedding: %v\n", embBin)
   464  	// >>> BIN embedding: [1 1]
   465  	// STEP_END
   466  
   467  	// Output:
   468  	// true
   469  	// Q8 embedding: [1.2643694877624512 1.958230972290039]
   470  	// true
   471  	// NOQUANT embedding: [1.262184977531433 1.958230972290039]
   472  	// true
   473  	// BIN embedding: [1 1]
   474  }
   475  
   476  func ExampleClient_vectorset_dimension_reduction() {
   477  	ctx := context.Background()
   478  
   479  	rdb := redis.NewClient(&redis.Options{
   480  		Addr:     "localhost:6379",
   481  		Password: "", // no password set
   482  		DB:       0,  // use default DB
   483  	})
   484  
   485  	defer rdb.Close()
   486  	// REMOVE_START
   487  	rdb.Del(ctx, "setNotReduced", "setReduced")
   488  	// REMOVE_END
   489  
   490  	// STEP_START add_reduce
   491  	// Create a vector with 300 dimensions
   492  	values := make([]float64, 300)
   493  
   494  	for i := 0; i < 300; i++ {
   495  		values[i] = float64(i) / 299
   496  	}
   497  
   498  	vecLarge := &redis.VectorValues{Val: values}
   499  
   500  	// Add without reduction
   501  	res1, err := rdb.VAdd(ctx, "setNotReduced", "element", vecLarge).Result()
   502  
   503  	if err != nil {
   504  		panic(err)
   505  	}
   506  
   507  	fmt.Println(res1) // >>> true
   508  
   509  	dim1, err := rdb.VDim(ctx, "setNotReduced").Result()
   510  
   511  	if err != nil {
   512  		panic(err)
   513  	}
   514  
   515  	fmt.Printf("Dimension without reduction: %d\n", dim1)
   516  	// >>> Dimension without reduction: 300
   517  
   518  	// Add with reduction to 100 dimensions
   519  	res2, err := rdb.VAddWithArgs(ctx, "setReduced", "element", vecLarge,
   520  		&redis.VAddArgs{
   521  			Reduce: 100,
   522  		},
   523  	).Result()
   524  
   525  	if err != nil {
   526  		panic(err)
   527  	}
   528  
   529  	fmt.Println(res2) // >>> true
   530  
   531  	dim2, err := rdb.VDim(ctx, "setReduced").Result()
   532  
   533  	if err != nil {
   534  		panic(err)
   535  	}
   536  
   537  	fmt.Printf("Dimension after reduction: %d\n", dim2)
   538  	// >>> Dimension after reduction: 100
   539  	// STEP_END
   540  
   541  	// Output:
   542  	// true
   543  	// Dimension without reduction: 300
   544  	// true
   545  	// Dimension after reduction: 100
   546  }
   547  

View as plain text