{
  "schema_version": 2,
  "id": "develop/clients/php/vecsets",
  "title": "Vector set embeddings",
  "url": "https://redis.io/docs/latest/develop/clients/php/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:49:12+01:00",
  "page_type": "content",
  "content_hash": "159d893bd07b661f434a33d4ce6b49c69275384723cf8184812b33950b09a74e",
  "sections": [
    {
      "id": "initialize",
      "title": "Initialize",
      "role": "content",
      "text": "Install `Predis` and `TransformersPHP` with Composer:\n\n[code example]\n\nIn a new PHP file, import the required classes and function:\n\nFoundational: Import required libraries for vector sets, embeddings, and Redis operations\n\n**Difficulty:** Beginner\n\n**Available in:** PHP\n\n##### PHP\n\n[code example]\n\n\n\nThe `pipeline()` function below creates an embedding generator for the\n[`all-MiniLM-L6-v2`](https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2)\nmodel. This model generates vectors with 384 dimensions, regardless of the\nlength of the input text:\n\nFoundational: Initialize a TransformersPHP embedding pipeline to generate vectors from text\n\n**Difficulty:** Beginner\n\n**Available in:** PHP\n\n##### PHP\n\n[code example]"
    },
    {
      "id": "create-the-data",
      "title": "Create the data",
      "role": "content",
      "text": "The example data is an array containing brief descriptions of famous people:\n\nFoundational: Define sample data with text descriptions and metadata for vector embedding and storage\n\n**Difficulty:** Beginner\n\n**Available in:** PHP\n\n##### PHP\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 array, uses the embedding pipeline to\ngenerate a `float` vector from each description, and then adds the result to a\nvector set called `famousPeople` with [`vadd()`](https://redis.io/docs/latest/commands/vadd).\nIt then stores the `born` and `died` values as element attributes using\n[`vsetattr()`](https://redis.io/docs/latest/commands/vsetattr), so you can use the metadata\nlater during queries.\n\nFoundational: Add embedding vectors to a vector set and attach metadata attributes for later filtering\n\n**Difficulty:** Beginner\n\n**Available in:** PHP\n\n##### PHP\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 generate\nanother embedding vector from the query text and pass it to\n[`vsim()`](https://redis.io/docs/latest/commands/vsim), which returns elements ranked in\norder of similarity to that query vector.\n\nStart with a simple query for \"actors\":\n\nVector similarity search: Find semantically similar items in a vector set using VSIM\n\n**Difficulty:** Intermediate\n\n**Available in:** PHP\n\n##### PHP\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 the\nremaining results are less directly related. By default, the search attempts\nto rank all the elements in the set. You can use the `count` parameter of\n`vsim()` to limit the list to the most relevant few results:\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:** PHP\n\n##### PHP\n\n[code example]\n\n\n\nThe reason for using text embeddings rather than simple text search is that\nthe embeddings capture semantic information. This allows a query to find\nelements with a similar meaning even if the text is different. For example, the\nword \"entertainer\" doesn't appear in any of the descriptions but if you use it\nas a query, the actors and musicians rank highly in the results:\n\nSemantic search: Use text embeddings to find related concepts even when exact keywords do not appear in the source text\n\n**Difficulty:** Intermediate\n\n**Available in:** PHP\n\n##### PHP\n\n[code example]\n\n\n\nSimilarly, if you use \"science\" as a query, you get the scientists first,\nfollowed by the mathematicians:\n\n[code example]\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 the\nyear 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:** PHP\n\n##### PHP\n\n[code example]"
    },
    {
      "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/php/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": "composer require predis/predis codewithkyrian/transformers",
      "section_id": "initialize"
    },
    {
      "id": "initialize-ex1",
      "language": "php",
      "code": "<?php\n\nrequire 'vendor/autoload.php';\n\nuse function Codewithkyrian\\Transformers\\Pipelines\\pipeline;\n\nuse Predis\\Client as PredisClient;",
      "section_id": "initialize"
    },
    {
      "id": "initialize-ex2",
      "language": "php",
      "code": "$extractor = pipeline('embeddings', 'Xenova/all-MiniLM-L6-v2');",
      "section_id": "initialize"
    },
    {
      "id": "create-the-data-ex0",
      "language": "php",
      "code": "$peopleData = [\n    'Marie Curie' => [\n        'born' => 1867,\n        'died' => 1934,\n        'description' => 'Polish-French chemist and physicist. The only person ever to win two Nobel prizes for two different sciences.',\n    ],\n    'Linus Pauling' => [\n        'born' => 1901,\n        'died' => 1994,\n        'description' => 'American chemist and peace activist. One of only two people to win two Nobel prizes in different fields (chemistry and peace).',\n    ],\n    'Freddie Mercury' => [\n        'born' => 1946,\n        'died' => 1991,\n        'description' => 'British musician, best known as the lead singer of the rock band Queen.',\n    ],\n    'Marie Fredriksson' => [\n        'born' => 1958,\n        'died' => 2019,\n        'description' => 'Swedish multi-instrumentalist, mainly known as the lead singer and keyboardist of the band Roxette.',\n    ],\n    'Paul Erdos' => [\n        'born' => 1913,\n        'died' => 1996,\n        'description' => 'Hungarian mathematician, known for his eccentric personality almost as much as his contributions to many different fields of mathematics.',\n    ],\n    'Maryam Mirzakhani' => [\n        'born' => 1977,\n        'died' => 2017,\n        'description' => 'Iranian mathematician. The first woman ever to win the Fields medal for her contributions to mathematics.',\n    ],\n    'Masako Natsume' => [\n        'born' => 1957,\n        'died' => 1985,\n        'description' => 'Japanese actress. She was very famous in Japan but was primarily known elsewhere in the world for her portrayal of Tripitaka in the TV series Monkey.',\n    ],\n    'Chaim Topol' => [\n        'born' => 1935,\n        'died' => 2023,\n        'description' => \"Israeli actor and singer, usually credited simply as 'Topol'. He was best known for his many appearances as Tevye in the musical Fiddler on the Roof.\",\n    ],\n];",
      "section_id": "create-the-data"
    },
    {
      "id": "add-the-data-to-a-vector-set-ex0",
      "language": "php",
      "code": "$r->del('famousPeople');\n\nforeach ($peopleData as $name => $details) {\n    $embedding = $extractor($details['description'], normalize: true, pooling: 'mean');\n\n    $r->vadd('famousPeople', $embedding[0], $name);\n    $r->vsetattr('famousPeople', $name, [\n        'born' => $details['born'],\n        'died' => $details['died'],\n    ]);\n}",
      "section_id": "add-the-data-to-a-vector-set"
    },
    {
      "id": "query-the-vector-set-ex0",
      "language": "php",
      "code": "$actorsEmbedding = $extractor('actors', normalize: true, pooling: 'mean');\n$actorsResults = $r->vsim('famousPeople', $actorsEmbedding[0]);\n\necho \"'actors': \" . json_encode($actorsResults), PHP_EOL;\n// >>> 'actors': [\"Masako Natsume\",\"Chaim Topol\",\"Linus Pauling\",\"Marie Fredriksson\",\"Maryam Mirzakhani\",\"Freddie Mercury\",\"Marie Curie\",\"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\",\"Freddie Mercury\",\n\"Marie Curie\",\"Paul Erdos\"]",
      "section_id": "query-the-vector-set"
    },
    {
      "id": "query-the-vector-set-ex2",
      "language": "php",
      "code": "$twoActorsResults = $r->vsim('famousPeople', $actorsEmbedding[0], false, false, 2);\n\necho \"'actors (2)': \" . json_encode($twoActorsResults), PHP_EOL;\n// >>> 'actors (2)': [\"Masako Natsume\",\"Chaim Topol\"]",
      "section_id": "query-the-vector-set"
    },
    {
      "id": "query-the-vector-set-ex3",
      "language": "php",
      "code": "$entertainerEmbedding = $extractor('entertainer', normalize: true, pooling: 'mean');\n$entertainerResults = $r->vsim('famousPeople', $entertainerEmbedding[0]);\n\necho \"'entertainer': \" . json_encode($entertainerResults), PHP_EOL;\n// >>> 'entertainer': [\"Chaim Topol\",\"Freddie Mercury\",\"Linus Pauling\",\"Marie Fredriksson\",\"Masako Natsume\",\"Paul Erdos\",\"Maryam Mirzakhani\",\"Marie Curie\"]",
      "section_id": "query-the-vector-set"
    },
    {
      "id": "query-the-vector-set-ex4",
      "language": "text",
      "code": "'science': [\"Linus Pauling\",\"Marie Curie\",\"Maryam Mirzakhani\",\n\"Paul Erdos\",\"Marie Fredriksson\",\"Masako Natsume\",\n\"Freddie Mercury\",\"Chaim Topol\"]",
      "section_id": "query-the-vector-set"
    },
    {
      "id": "query-the-vector-set-ex5",
      "language": "php",
      "code": "$science2000Results = $r->vsim('famousPeople', $scienceEmbedding[0], false, false, null, null, null, '.died < 2000');\n\necho \"'science2000': \" . json_encode($science2000Results), PHP_EOL;\n// >>> 'science2000': [\"Linus Pauling\",\"Marie Curie\",\"Paul Erdos\",\"Masako Natsume\",\"Freddie Mercury\"]",
      "section_id": "query-the-vector-set"
    }
  ]
}
