> ## Documentation Index
> Fetch the complete documentation index at: https://upstash.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Fetching Vectors

Sometimes, you’re not just searching for something—you know exactly which vector you want to retrieve.

In such cases, you can directly fetch specific vectors from your database.

## Fetch

Each record in Upstash Vector is assigned a unique ID, which you can use to retrieve a specific vector from your database.

Let's use the `fetch()` method to retrieve a vector from Upstash Vector.

<CodeGroup>
  ```php simple theme={"system"}
  use Upstash\Vector\Index;
  use Upstash\Vector\VectorFetch;

  $index = new Index(
    url: "<UPSTASH_VECTOR_REST_URL>",
    token: "<UPSTASH_VECTOR_REST_TOKEN>",
  );

  $results = $index->fetch(new VectorFetch(
    ids: ['1', '2'],
    includeMetadata: true, // (optional) if true the fetch results will contain metadata.
    includeVectors: true, // (optional) if true the fetch results will contain the indexed vectors.
    includeData: true, // (optional) if true the fetch results will contain the string data.
  ));
  ```

  ```php using namespaces theme={"system"}
  use Upstash\Vector\Index;
  use Upstash\Vector\VectorFetch;

  $index = new Index(
    url: "<UPSTASH_VECTOR_REST_URL>",
    token: "<UPSTASH_VECTOR_REST_TOKEN>",
  );

  $results = $index->namespace('my-namespace')->fetch(new VectorFetch(
    ids: ['1', '2'],
    includeMetadata: true, // (optional) if true the fetch results will contain metadata.
    includeVectors: true, // (optional) if true the fetch results will contain the indexed vectors.
    includeData: true, // (optional) if true the fetch results will contain the string data.
  ));
  ```
</CodeGroup>

The `fetch()` method returns a `Upstash\Vector\VectorFetchResult` object, which allows you to access the results of the query.

### Fetch by ID prefix

You can also use `fetch()` to get all vectors which their IDs match a defined prefix.

<CodeGroup>
  ```php simple theme={"system"}
  use Upstash\Vector\Index;
  use Upstash\Vector\VectorFetchByPrefix;

  $index = new Index(
    url: "<UPSTASH_VECTOR_REST_URL>",
    token: "<UPSTASH_VECTOR_REST_TOKEN>",
  );

  $results = $index->fetch(new VectorFetchByPrefix(
    prefix: 'users:',
    includeMetadata: true, // (optional) if true the fetch results will contain metadata.
    includeVectors: true, // (optional) if true the fetch results will contain the indexed vectors.
    includeData: true, // (optional) if true the fetch results will contain the string data.
  ));
  ```

  ```php using namespaces theme={"system"}
  use Upstash\Vector\Index;
  use Upstash\Vector\VectorFetchByPrefix;

  $index = new Index(
    url: "<UPSTASH_VECTOR_REST_URL>",
    token: "<UPSTASH_VECTOR_REST_TOKEN>",
  );

  $results = $index->namespace('my-namespace')->fetch(new VectorFetchByPrefix(
    prefix: 'users:',
    includeMetadata: true, // (optional) if true the fetch results will contain metadata.
    includeVectors: true, // (optional) if true the fetch results will contain the indexed vectors.
    includeData: true, // (optional) if true the fetch results will contain the string data.
  ));
  ```
</CodeGroup>
