{
  "schema_version": 3,
  "id": "develop/clients/ruby/vecsets",
  "title": "Vector set embeddings",
  "url": "https://redis.io/docs/latest/develop/clients/ruby/vecsets/",
  "summary": "Index and query embeddings with Redis vector sets",
  "tags": [
    "docs",
    "develop",
    "stack",
    "oss",
    "rs",
    "rc",
    "oss",
    "kubernetes",
    "clients"
  ],
  "last_updated": "2026-11-25T09:38:55Z",
  "page_type": "content",
  "content_hash": "cd996b06fdfc750294b301dfd7458a8aa8679814b9cec7a015088e50eddec715",
  "sections": [
    {
      "id": "initialize",
      "title": "Initialize",
      "role": "content",
      "text": "Start by installing `redis-rb` and `informers`. In your `Gemfile`:\n\n[code example]\n\nThen require them:\n\nFoundational: Import required libraries for vector sets, embeddings, and Redis operations\n\n**Difficulty:** Beginner\n\n**Available in:** Ruby\n\n##### Ruby\n\n[code example]\n\n\n\n`informers` is a Ruby port of Hugging Face transformers that runs the\nONNX-exported [`all-MiniLM-L6-v2`](https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2)\nmodel locally through `onnxruntime`. This model generates vectors with 384\ndimensions, regardless of the length of the input text, but note that the\ninput is truncated to 256 tokens (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\nway tokens are related to the original text).\n\nFoundational: Initialize an embedding model to generate vector embeddings from text\n\n**Difficulty:** Beginner\n\n**Available in:** Ruby\n\n##### Ruby\n\n[code example]"
    },
    {
      "id": "create-the-data",
      "title": "Create the data",
      "role": "content",
      "text": "The example data is a hash with some brief descriptions of famous people:\n\nFoundational: Define sample data with text descriptions for vector embedding and storage\n\n**Difficulty:** Beginner\n\n**Available in:** Ruby\n\n##### Ruby\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 hash and adds a corresponding element to\na vector set called `famousPeople` for each entry.\n\nCall the model with `pooling: 'mean', normalize: true` to generate the\nembedding as an `Array<Float>` that matches the pooling and normalization\n`sentence-transformers` applies by default. Pass the array directly as the\nvector argument to [`vadd()`](https://redis.io/docs/latest/commands/vadd), along with the\n`born` and `died` values as attribute data. You can access this attribute\ndata during a query or by using the\n[`vgetattr()`](https://redis.io/docs/latest/commands/vgetattr) method.\n\nFoundational: Add vector embeddings and attributes to a vector set using VADD command\n\n**Difficulty:** Beginner\n\n**Available in:** Ruby\n\n##### Ruby\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 call the\nmodel again to generate another embedding vector for the query text, then\npass the query vector to [`vsim()`](https://redis.io/docs/latest/commands/vsim) to return\nelements of 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:** Ruby\n\n##### Ruby\n\n[code example]\n\n\n\nThis returns the following list of elements:\n\n[code example]\n\nThe first two people in the list are the two actors, as expected, but none of\nthe people from Linus Pauling onward was especially well-known for acting\n(and there certainly isn't any information about that in the short\ndescription text). As it stands, the search attempts to rank all the\nelements in the set, based on the information contained in the embedding\nmodel. You can use the `count` parameter of `vsim()` to limit the list of\nelements to 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:** Ruby\n\n##### Ruby\n\n[code example]\n\n\n\nThe reason for using text embeddings rather than simple text search is that\nthe embeddings represent semantic information. This allows a query to find\nelements with a similar meaning even if the text is different. For example,\nthe word \"entertainer\" doesn't appear in any of the descriptions but if you\nuse it as a query, the actors and musicians are ranked highest in the results\nlist:\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:** Ruby\n\n##### Ruby\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\nmathematics and science.\n\nYou can also use\n[filter expressions](https://redis.io/docs/latest/develop/data-types/vector-sets/filtered-search)\nwith `vsim()` to restrict the search further. For example, repeat the\n\"science\" query, but this time limit the results to people who died before\nthe 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:** Ruby\n\n##### Ruby\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 pass\nthe filter test are removed from the results completely, rather than just\nreduced in rank. This can help to improve the performance of the search\nbecause there is no need to calculate the vector distance for elements that\nhave 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 about\ntext 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/ruby/vecsearch). This is a\nfeature of [Redis Search](https://redis.io/docs/latest/develop/ai/search-and-query)\nthat lets you retrieve [JSON](https://redis.io/docs/latest/develop/data-types/json)\nand [hash](https://redis.io/docs/latest/develop/data-types/hashes) documents based on vector\ndata stored in their fields."
    }
  ],
  "examples": [
    {
      "id": "initialize-ex0",
      "language": "ruby",
      "code": "gem 'redis', '>= 6.1'\ngem 'informers'",
      "section_id": "initialize"
    },
    {
      "id": "initialize-ex1",
      "language": "ruby",
      "code": "require 'redis'\nrequire 'informers'",
      "section_id": "initialize"
    },
    {
      "id": "initialize-ex2",
      "language": "ruby",
      "code": "# `informers` is a Ruby port of Hugging Face transformers that runs the\n# ONNX-exported `all-MiniLM-L6-v2` encoder locally through `onnxruntime`.\n# `Informers.pipeline(\"embedding\", ...)` returns a callable that maps a\n# string to a 384-element `Array<Float>`.\nmodel = Informers.pipeline('embedding', 'sentence-transformers/all-MiniLM-L6-v2')",
      "section_id": "initialize"
    },
    {
      "id": "create-the-data-ex0",
      "language": "ruby",
      "code": "people_data = {\n  'Marie Curie' => {\n    born: 1867, died: 1934,\n    description: 'Polish-French chemist and physicist. The only person ' \\\n      'ever to win two Nobel prizes for two different sciences.'\n  },\n  'Linus Pauling' => {\n    born: 1901, died: 1994,\n    description: 'American chemist and peace activist. One of only two ' \\\n      'people to win two Nobel prizes in different fields ' \\\n      '(chemistry and peace).'\n  },\n  'Freddie Mercury' => {\n    born: 1946, died: 1991,\n    description: 'British musician, best known as the lead singer of the ' \\\n      'rock band Queen.'\n  },\n  'Marie Fredriksson' => {\n    born: 1958, died: 2019,\n    description: 'Swedish multi-instrumentalist, mainly known as the lead ' \\\n      'singer and keyboardist of the band Roxette.'\n  },\n  'Paul Erdos' => {\n    born: 1913, died: 1996,\n    description: 'Hungarian mathematician, known for his eccentric ' \\\n      'personality almost as much as his contributions to many different ' \\\n      'fields of mathematics.'\n  },\n  'Maryam Mirzakhani' => {\n    born: 1977, died: 2017,\n    description: 'Iranian mathematician. The first woman ever to win the ' \\\n      'Fields medal for her contributions to mathematics.'\n  },\n  'Masako Natsume' => {\n    born: 1957, died: 1985,\n    description: 'Japanese actress. She was very famous in Japan but was ' \\\n      'primarily known elsewhere in the world for her portrayal of ' \\\n      'Tripitaka in the TV series Monkey.'\n  },\n  'Chaim Topol' => {\n    born: 1935, died: 2023,\n    description: \"Israeli actor and singer, usually credited simply as \" \\\n      \"'Topol'. He was best known for his many appearances as Tevye in \" \\\n      'the musical Fiddler on the Roof.'\n  }\n}",
      "section_id": "create-the-data"
    },
    {
      "id": "add-the-data-to-a-vector-set-ex0",
      "language": "ruby",
      "code": "people_data.each do |name, details|\n  embedding = model.(details[:description], pooling: 'mean', normalize: true)\n\n  r.vadd(\n    'famousPeople',\n    embedding,\n    name,\n    attributes: { born: details[:born], died: details[:died] }\n  )\nend",
      "section_id": "add-the-data-to-a-vector-set"
    },
    {
      "id": "query-the-vector-set-ex0",
      "language": "ruby",
      "code": "query_value = 'actors'\n\nactors_results = r.vsim(\n  'famousPeople',\n  vector: model.(query_value, pooling: 'mean', normalize: true)\n)\n\nputs \"'actors': #{actors_results}\"\n# >>> 'actors': [\"Masako Natsume\", \"Chaim Topol\", \"Linus Pauling\",\n# \"Marie Fredriksson\", \"Maryam Mirzakhani\", \"Marie Curie\",\n# \"Freddie Mercury\", \"Paul Erdos\"]",
      "section_id": "query-the-vector-set"
    },
    {
      "id": "query-the-vector-set-ex1",
      "language": "plaintext",
      "code": "'actors': [\"Masako Natsume\", \"Chaim Topol\", \"Linus Pauling\",\n\"Marie Fredriksson\", \"Maryam Mirzakhani\", \"Marie Curie\",\n\"Freddie Mercury\", \"Paul Erdos\"]",
      "section_id": "query-the-vector-set"
    },
    {
      "id": "query-the-vector-set-ex2",
      "language": "ruby",
      "code": "query_value = 'actors'\n\ntwo_actors_results = r.vsim(\n  'famousPeople',\n  vector: model.(query_value, pooling: 'mean', normalize: true),\n  count: 2\n)\n\nputs \"'actors (2)': #{two_actors_results}\"\n# >>> 'actors (2)': [\"Masako Natsume\", \"Chaim Topol\"]",
      "section_id": "query-the-vector-set"
    },
    {
      "id": "query-the-vector-set-ex3",
      "language": "ruby",
      "code": "query_value = 'entertainer'\n\nentertainer_results = r.vsim(\n  'famousPeople',\n  vector: model.(query_value, pooling: 'mean', normalize: true)\n)\n\nputs \"'entertainer': #{entertainer_results}\"\n# >>> 'entertainer': [\"Chaim Topol\", \"Freddie Mercury\",\n# \"Marie Fredriksson\", \"Linus Pauling\", \"Masako Natsume\", \"Paul Erdos\",\n# \"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\",\n\"Paul Erdos\", \"Marie Fredriksson\", \"Freddie Mercury\", \"Masako Natsume\",\n\"Chaim Topol\"]",
      "section_id": "query-the-vector-set"
    },
    {
      "id": "query-the-vector-set-ex5",
      "language": "ruby",
      "code": "query_value = 'science'\n\nscience2000_results = r.vsim(\n  'famousPeople',\n  vector: model.(query_value, pooling: 'mean', normalize: true),\n  filter: '.died < 2000'\n)\n\nputs \"'science2000': #{science2000_results}\"\n# >>> 'science2000': [\"Marie Curie\", \"Linus Pauling\", \"Paul Erdos\",\n# \"Freddie Mercury\", \"Masako Natsume\"]",
      "section_id": "query-the-vector-set"
    }
  ]
}
