{
  "schema_version": 2,
  "id": "develop/clients/go/vecsets",
  "title": "Vector set embeddings",
  "url": "https://redis.io/docs/latest/develop/clients/go/vecsets/",
  "summary": "Index and query embeddings with Redis vector sets",
  "tags": [
    "docs",
    "develop",
    "stack",
    "oss",
    "rs",
    "rc",
    "oss",
    "kubernetes",
    "clients"
  ],
  "last_updated": "2026-09-09T09:42:14+01:00",
  "page_type": "content",
  "content_hash": "3db91fea74512b9e59cda76cec3c96cb5b11bac5d9b09ec55d0a3d7ef733fa1d",
  "sections": [
    {
      "id": "initialize",
      "title": "Initialize",
      "role": "content",
      "text": "Start by [installing](https://redis.io/docs/latest/develop/clients/go#install) `go-redis` if you haven't already done so. Note that you need `go-redis`\n[v9.10.0](https://github.com/redis/go-redis/releases/tag/v9.10.0)\nor later to use vector sets.\n\nAlso, install `hugot`:\n\n[code example]\n\nIn a new Go file, add the required imports:\n\nFoundational: Import required packages for vector set operations and text embeddings\n\n**Difficulty:** Beginner\n\n**Available in:** Go\n\n##### Go\n\n[code example]\n\n\n\nThese include the `Hugot` library to generate an embedding from a section of text.\nThis example uses an instance of the `SentenceTransformer` model\n[`all-MiniLM-L6-v2`](https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2)\nfor the embeddings. This model generates vectors with 384 dimensions, regardless\nof the length of the input text, but note that the input is truncated to 256\ntokens (see\n[Word piece tokenization](https://huggingface.co/learn/nlp-course/en/chapter6/6)\nat the [Hugging Face](https://huggingface.co/) docs to learn more about the way tokens\nare related to the original text).\n\nPractical pattern: Initialize a sentence transformer model for generating text embeddings\n\n**Difficulty:** Beginner\n\n**Available in:** Go\n\n##### Go\n\n[code example]"
    },
    {
      "id": "create-the-data",
      "title": "Create the data",
      "role": "content",
      "text": "The example data is contained in a map with some brief\ndescriptions of famous people:\n\nFoundational: Create sample data with descriptions of famous people for vector set storage\n\n**Difficulty:** Beginner\n\n**Available in:** Go\n\n##### Go\n\n[code example]"
    },
    {
      "id": "add-the-data-to-a-vector-set",
      "title": "Add the data to a vector set",
      "role": "content",
      "text": "The next step is to connect to Redis and add the data to a new vector set.\n\nThe code below iterates through the `peopleData` map and adds corresponding\nelements to a vector set called `famousPeople`.\n\nUse the\n`RunPipeline()` method of `embeddingPipeline` to generate the\nembedding as an array of `float32` values, then use a loop like the one\nshown below to convert the `float32` array to a `float64` array.\nYou can then pass this array to the\n[`VAdd()`](https://redis.io/docs/latest/commands/vadd) command to set the embedding.\n\nThe call to `VAdd()` also adds the `born` and `died` values from the\noriginal map as attribute data. You can access this during a query\nor by using the [`VGetAttr()`](https://redis.io/docs/latest/commands/vgetattr) method.\n\nFoundational: Generate embeddings and add data to a vector set with VAdd including attribute metadata\n\n**Difficulty:** Intermediate\n\n**Available in:** Go\n\n##### Go\n\n[code example]"
    },
    {
      "id": "query-the-vector-set",
      "title": "Query the vector set",
      "role": "content",
      "text": "You can now query the data in the set. The basic approach is to use the\n`RunPipeline()` method to generate another embedding vector for the query text.\n(This is the same method used to add the elements to the set.) Then, pass\nthe query vector to [`VSim()`](https://redis.io/docs/latest/commands/vsim) to return elements\nof the set, ranked in order of similarity to the query.\n\nStart with a simple query for \"actors\":\n\nVector similarity search: Find semantically similar items in a vector set using VSIM command\n\n**Difficulty:** Intermediate\n\n**Available in:** Go\n\n##### Go\n\n[code example]\n\n\n\nThis returns the following list of elements (formatted slightly for clarity):\n\n[code example]\n\nThe first two people in the list are the two actors, as expected, but none of the\npeople from Linus Pauling onward was especially well-known for acting (and there certainly\nisn't any information about that in the short description text).\nAs it stands, the search attempts to rank all the elements in the set, based\non the information contained in the embedding model.\nYou can use the `Count` parameter of `VSimWithArgs()` to limit the list of elements\nto just the most relevant few items:\n\nVector similarity search with limits: Restrict results to the top K most similar items using the count parameter\n\n**Difficulty:** Intermediate\n\n**Available in:** Go\n\n##### Go\n\n[code example]\n\n\n\nThe reason for using text embeddings rather than simple text search\nis that the embeddings represent semantic information. This allows a query\nto find elements with a similar meaning even if the text is\ndifferent. For example, the word \"entertainer\" doesn't appear in any of the\ndescriptions but if you use it as a query, the actors and musicians are ranked\nhighest in the results list:\n\nSemantic search: Leverage text embeddings to find semantically similar items even when exact keywords don't match\n\n**Difficulty:** Intermediate\n\n**Available in:** Go\n\n##### Go\n\n[code example]\n\n\n\nSimilarly, if you use \"science\" as a query, you get the following results:\n\n[code example]\n\nThe scientists are ranked highest but they are then followed by the\nmathematicians. This seems reasonable given the connection between mathematics\nand science.\n\nYou can also use\n[filter expressions](https://redis.io/docs/latest/develop/data-types/vector-sets/filtered-search)\nwith `VSimWithArgs()` to restrict the search further. For example,\nrepeat the \"science\" query, but this time limit the results to people\nwho died before the year 2000:\n\nFiltered vector search: Combine vector similarity with attribute filters to narrow results based on metadata conditions\n\n**Difficulty:** Advanced\n\n**Available in:** Go\n\n##### Go\n\n[code example]\n\n\n\nNote that the boolean filter expression is applied to items in the list\nbefore the vector distance calculation is performed. Items that don't\npass the filter test are removed from the results completely, rather\nthan just reduced in rank. This can help to improve the performance of the\nsearch because there is no need to calculate the vector distance for\nelements that have already been filtered out of the search."
    },
    {
      "id": "more-information",
      "title": "More information",
      "role": "content",
      "text": "See the [vector sets](https://redis.io/docs/latest/develop/data-types/vector-sets)\ndocs for more information and code examples. See the\n[Redis for AI](https://redis.io/docs/latest/develop/ai) section for more details\nabout text embeddings and other AI techniques you can use with Redis.\n\nYou may also be interested in\n[vector search](https://redis.io/docs/latest/develop/clients/go/vecsearch).\nThis is a feature of\n[Redis Search](https://redis.io/docs/latest/develop/ai/search-and-query)\nthat lets you retrieve\n[JSON](https://redis.io/docs/latest/develop/data-types/json) and\n[hash](https://redis.io/docs/latest/develop/data-types/hashes) documents based on\nvector data stored in their fields."
    }
  ],
  "examples": [
    {
      "id": "initialize-ex0",
      "language": "bash",
      "code": "go get github.com/knights-analytics/hugot",
      "section_id": "initialize"
    },
    {
      "id": "initialize-ex1",
      "language": "go",
      "code": "package main\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\t\"strings\"\n\n\t\"github.com/knights-analytics/hugot\"\n\t\"github.com/redis/go-redis/v9\"\n)",
      "section_id": "initialize"
    },
    {
      "id": "initialize-ex2",
      "language": "go",
      "code": "// Create a new Hugot session\n\tsession, err := hugot.NewGoSession()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tdefer func() {\n\t\terr := session.Destroy()\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t}()\n\n\t// Download the model.\n\tdownloadOptions := hugot.NewDownloadOptions()\n\tdownloadOptions.OnnxFilePath = \"onnx/model.onnx\"\n\tmodelPath, err := hugot.DownloadModel(\n\t\t\"sentence-transformers/all-MiniLM-L6-v2\",\n\t\t\"./models/\",\n\t\tdownloadOptions,\n\t)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Create feature extraction pipeline configuration\n\tconfig := hugot.FeatureExtractionConfig{\n\t\tModelPath: modelPath,\n\t\tName:      \"embeddingPipeline\",\n\t}\n\n\t// Create the feature extraction pipeline\n\tembeddingPipeline, err := hugot.NewPipeline(session, config)\n\tif err != nil {\n\t\tpanic(err)\n\t}",
      "section_id": "initialize"
    },
    {
      "id": "create-the-data-ex0",
      "language": "go",
      "code": "type PersonData struct {\n\tBorn        int\n\tDied        int\n\tDescription string\n}\n\nvar peopleData = map[string]PersonData{\n\t\"Marie Curie\": {\n\t\tBorn: 1867, Died: 1934,\n\t\tDescription: `Polish-French chemist and physicist. The only person ever to win\n\t\ttwo Nobel prizes for two different sciences.\n\t\t`,\n\t},\n\t\"Linus Pauling\": {\n\t\tBorn: 1901, Died: 1994,\n\t\tDescription: `American chemist and peace activist. One of only two people to win two\n\t\tNobel prizes in different fields (chemistry and peace).\n\t\t`,\n\t},\n\t\"Freddie Mercury\": {\n\t\tBorn: 1946, Died: 1991,\n\t\tDescription: `British musician, best known as the lead singer of the rock band\n\t\tQueen.\n\t\t`,\n\t},\n\t\"Marie Fredriksson\": {\n\t\tBorn: 1958, Died: 2019,\n\t\tDescription: `Swedish multi-instrumentalist, mainly known as the lead singer and\n\t\tkeyboardist of the band Roxette.\n\t\t`,\n\t},\n\t\"Paul Erdos\": {\n\t\tBorn: 1913, Died: 1996,\n\t\tDescription: `Hungarian mathematician, known for his eccentric personality almost\n\t\tas much as his contributions to many different fields of mathematics.\n\t\t`,\n\t},\n\t\"Maryam Mirzakhani\": {\n\t\tBorn: 1977, Died: 2017,\n\t\tDescription: `Iranian mathematician. The first woman ever to win the Fields medal\n\t\tfor her contributions to mathematics.\n\t\t`,\n\t},\n\t\"Masako Natsume\": {\n\t\tBorn: 1957, Died: 1985,\n\t\tDescription: `Japanese actress. She was very famous in Japan but was primarily\n\t\tknown elsewhere in the world for her portrayal of Tripitaka in the\n\t\tTV series Monkey.\n\t\t`,\n\t},\n\t\"Chaim Topol\": {\n\t\tBorn: 1935, Died: 2023,\n\t\tDescription: `Israeli actor and singer, usually credited simply as 'Topol'. He was\n\t\tbest known for his many appearances as Tevye in the musical Fiddler\n\t\ton the Roof.\n\t\t`,\n\t},\n}",
      "section_id": "create-the-data"
    },
    {
      "id": "add-the-data-to-a-vector-set-ex0",
      "language": "go",
      "code": "for name, details := range peopleData {\n\t\t// Generate embeddings using Hugot\n\t\tresult, err := embeddingPipeline.RunPipeline([]string{details.Description})\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\n\t\t// Convert embedding to float64 slice\n\t\tembFloat32 := result.Embeddings[0]\n\t\tembFloat64 := make([]float64, len(embFloat32))\n\t\tfor i, v := range embFloat32 {\n\t\t\tembFloat64[i] = float64(v)\n\t\t}\n\n\t\t// Add vector to vector set\n\t\t_, err = rdb.VAdd(ctx, \"famousPeople\", name, &redis.VectorValues{Val: embFloat64}).Result()\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\n\t\t// Set attributes for the element\n\t\t_, err = rdb.VSetAttr(ctx, \"famousPeople\", name, map[string]interface{}{\n\t\t\t\"born\": details.Born,\n\t\t\t\"died\": details.Died,\n\t\t}).Result()\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t}",
      "section_id": "add-the-data-to-a-vector-set"
    },
    {
      "id": "query-the-vector-set-ex0",
      "language": "go",
      "code": "queryValue := \"actors\"\n\n\tqueryResult, err := embeddingPipeline.RunPipeline([]string{queryValue})\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Convert embedding to float64 slice\n\tqueryFloat32 := queryResult.Embeddings[0]\n\tqueryFloat64 := make([]float64, len(queryFloat32))\n\tfor i, v := range queryFloat32 {\n\t\tqueryFloat64[i] = float64(v)\n\t}\n\n\tactorsResults, err := rdb.VSim(ctx, \"famousPeople\", &redis.VectorValues{Val: queryFloat64}).Result()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Printf(\"'actors': %v\\n\", strings.Join(actorsResults, \", \"))\n\t// >>> 'actors': Masako Natsume, Chaim Topol, Linus Pauling,\n\t// Marie Fredriksson, Maryam Mirzakhani, Marie Curie, Freddie Mercury,\n\t// Paul Erdos",
      "section_id": "query-the-vector-set"
    },
    {
      "id": "query-the-vector-set-ex1",
      "language": "plaintext",
      "code": "'actors': Masako Natsume, Chaim Topol, Linus Pauling, Marie Fredriksson,\n    Maryam Mirzakhani, Marie Curie, Freddie Mercury, Paul Erdos",
      "section_id": "query-the-vector-set"
    },
    {
      "id": "query-the-vector-set-ex2",
      "language": "go",
      "code": "queryValue = \"actors\"\n\n\tqueryResult, err = embeddingPipeline.RunPipeline([]string{queryValue})\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Convert embedding to float64 slice\n\tqueryFloat32 = queryResult.Embeddings[0]\n\tqueryFloat64 = make([]float64, len(queryFloat32))\n\tfor i, v := range queryFloat32 {\n\t\tqueryFloat64[i] = float64(v)\n\t}\n\n\ttwoActorsResults, err := rdb.VSimWithArgs(ctx, \"famousPeople\",\n\t\t&redis.VectorValues{Val: queryFloat64},\n\t\t&redis.VSimArgs{Count: 2}).Result()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Printf(\"'actors (2)': %v\\n\", strings.Join(twoActorsResults, \", \"))\n\t// >>> 'actors (2)': Masako Natsume, Chaim Topol",
      "section_id": "query-the-vector-set"
    },
    {
      "id": "query-the-vector-set-ex3",
      "language": "go",
      "code": "queryValue = \"entertainer\"\n\n\tqueryResult, err = embeddingPipeline.RunPipeline([]string{queryValue})\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Convert embedding to float64 slice\n\tqueryFloat32 = queryResult.Embeddings[0]\n\tqueryFloat64 = make([]float64, len(queryFloat32))\n\tfor i, v := range queryFloat32 {\n\t\tqueryFloat64[i] = float64(v)\n\t}\n\n\tentertainerResults, err := rdb.VSim(ctx, \"famousPeople\", &redis.VectorValues{Val: queryFloat64}).Result()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Printf(\"'entertainer': %v\\n\", strings.Join(entertainerResults, \", \"))\n\t// >>> 'entertainer': Chaim Topol, Freddie Mercury, Marie Fredriksson,\n\t// Linus Pauling, Masako Natsume, Paul Erdos,\n\t// Maryam Mirzakhani, Marie Curie",
      "section_id": "query-the-vector-set"
    },
    {
      "id": "query-the-vector-set-ex4",
      "language": "plaintext",
      "code": "'science': Marie Curie, Linus Pauling, Maryam Mirzakhani, Paul Erdos,\nMarie Fredriksson, Freddie Mercury, Masako Natsume, Chaim Topol",
      "section_id": "query-the-vector-set"
    },
    {
      "id": "query-the-vector-set-ex5",
      "language": "go",
      "code": "queryValue = \"science\"\n\n\tqueryResult, err = embeddingPipeline.RunPipeline([]string{queryValue})\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Convert embedding to float64 slice\n\tqueryFloat32 = queryResult.Embeddings[0]\n\tqueryFloat64 = make([]float64, len(queryFloat32))\n\tfor i, v := range queryFloat32 {\n\t\tqueryFloat64[i] = float64(v)\n\t}\n\n\tscience2000Results, err := rdb.VSimWithArgs(ctx, \"famousPeople\",\n\t\t&redis.VectorValues{Val: queryFloat64},\n\t\t&redis.VSimArgs{Filter: \".died < 2000\"}).Result()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Printf(\"'science2000': %v\\n\", strings.Join(science2000Results, \", \"))\n\t// >>> 'science2000': Marie Curie, Linus Pauling, Paul Erdos, Freddie Mercury,\n\t// Masako Natsume",
      "section_id": "query-the-vector-set"
    }
  ]
}
