# Search for Vector Store documents by semantic similarity

{% openapi src="<https://2952947711-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FyNqpTh7Mh66N0RnO0k24%2Fuploads%2Fgit-blob-77b6137eeb641262ec8e531c78123c02b825b865%2Frememberizer_openapi.yml?alt=media&token=cbad765b-1613-4222-b591-9ae17a3b7cfa>" path="/vector-stores/{vector-store-id}/documents/search" method="get" %}
[rememberizer\_openapi.yml](https://2952947711-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FyNqpTh7Mh66N0RnO0k24%2Fuploads%2Fgit-blob-77b6137eeb641262ec8e531c78123c02b825b865%2Frememberizer_openapi.yml?alt=media\&token=cbad765b-1613-4222-b591-9ae17a3b7cfa)
{% endopenapi %}

## Example Requests

{% tabs %}
{% tab title="cURL" %}

```bash
curl -X GET \
  "https://api.rememberizer.ai/api/v1/vector-stores/vs_abc123/documents/search?q=How%20to%20integrate%20our%20product%20with%20third-party%20systems&n=5&prev_chunks=1&next_chunks=1" \
  -H "x-api-key: YOUR_API_KEY"
```

{% hint style="info" %}
Replace `YOUR_API_KEY` with your actual Vector Store API key and `vs_abc123` with your Vector Store ID.
{% endhint %}
{% endtab %}

{% tab title="JavaScript" %}

```javascript
const searchVectorStore = async (vectorStoreId, query, numResults = 5, prevChunks = 1, nextChunks = 1) => {
  const url = new URL(`https://api.rememberizer.ai/api/v1/vector-stores/${vectorStoreId}/documents/search`);
  url.searchParams.append('q', query);
  url.searchParams.append('n', numResults);
  url.searchParams.append('prev_chunks', prevChunks);
  url.searchParams.append('next_chunks', nextChunks);
  
  const response = await fetch(url.toString(), {
    method: 'GET',
    headers: {
      'x-api-key': 'YOUR_API_KEY'
    }
  });
  
  const data = await response.json();
  console.log(data);
};

searchVectorStore(
  'vs_abc123',
  'How to integrate our product with third-party systems',
  5,
  1,
  1
);
```

{% hint style="info" %}
Replace `YOUR_API_KEY` with your actual Vector Store API key and `vs_abc123` with your Vector Store ID.
{% endhint %}
{% endtab %}

{% tab title="Python" %}

```python
import requests

def search_vector_store(vector_store_id, query, num_results=5, prev_chunks=1, next_chunks=1):
    headers = {
        "x-api-key": "YOUR_API_KEY"
    }
    
    params = {
        "q": query,
        "n": num_results,
        "prev_chunks": prev_chunks,
        "next_chunks": next_chunks
    }
    
    response = requests.get(
        f"https://api.rememberizer.ai/api/v1/vector-stores/{vector_store_id}/documents/search",
        headers=headers,
        params=params
    )
    
    data = response.json()
    print(data)

search_vector_store(
    'vs_abc123',
    'How to integrate our product with third-party systems',
    5,
    1,
    1
)
```

{% hint style="info" %}
Replace `YOUR_API_KEY` with your actual Vector Store API key and `vs_abc123` with your Vector Store ID.
{% endhint %}
{% endtab %}

{% tab title="Ruby" %}

```ruby
require 'net/http'
require 'uri'
require 'json'

def search_vector_store(vector_store_id, query, num_results=5, prev_chunks=1, next_chunks=1)
  uri = URI("https://api.rememberizer.ai/api/v1/vector-stores/#{vector_store_id}/documents/search")
  params = {
    q: query,
    n: num_results,
    prev_chunks: prev_chunks,
    next_chunks: next_chunks
  }
  
  uri.query = URI.encode_www_form(params)
  
  request = Net::HTTP::Get.new(uri)
  request['x-api-key'] = 'YOUR_API_KEY'
  
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  
  response = http.request(request)
  data = JSON.parse(response.body)
  puts data
end

search_vector_store(
  'vs_abc123',
  'How to integrate our product with third-party systems',
  5,
  1,
  1
)
```

{% hint style="info" %}
Replace `YOUR_API_KEY` with your actual Vector Store API key and `vs_abc123` with your Vector Store ID.
{% endhint %}
{% endtab %}
{% endtabs %}

## Path Parameters

| Parameter       | Type   | Description                                            |
| --------------- | ------ | ------------------------------------------------------ |
| vector-store-id | string | **Required.** The ID of the vector store to search in. |

## Query Parameters

| Parameter    | Type    | Description                                                       |
| ------------ | ------- | ----------------------------------------------------------------- |
| q            | string  | **Required.** The search query text.                              |
| n            | integer | Number of results to return. Default: 10.                         |
| t            | number  | Matching threshold. Default: 0.7.                                 |
| prev\_chunks | integer | Number of chunks before the matched chunk to include. Default: 0. |
| next\_chunks | integer | Number of chunks after the matched chunk to include. Default: 0.  |

## Response Format

```json
{
  "vector_store": {
    "id": "vs_abc123",
    "name": "Product Documentation"
  },
  "matched_chunks": [
    {
      "document": {
        "id": 1234,
        "name": "Integration Guide.pdf",
        "type": "application/pdf",
        "size": 250000,
        "indexed_on": "2023-06-15T10:30:00Z",
        "vector_store": "vs_abc123",
        "created": "2023-06-15T10:15:00Z",
        "modified": "2023-06-15T10:30:00Z"
      },
      "matched_content": "Our product offers several integration options for third-party systems. The primary method is through our RESTful API, which supports OAuth2 authentication. Additionally, you can use our SDK available in Python, JavaScript, and Java.",
      "distance": 0.123
    },
    // ... more matched chunks
  ]
}
```

## Authentication

This endpoint requires authentication using an API key in the `x-api-key` header.

## Error Responses

| Status Code | Description                                                 |
| ----------- | ----------------------------------------------------------- |
| 400         | Bad Request - Missing required parameters or invalid format |
| 401         | Unauthorized - Invalid or missing API key                   |
| 404         | Not Found - Vector Store not found                          |
| 500         | Internal Server Error                                       |

## Search Optimization Tips

### Context Windows

Use the `prev_chunks` and `next_chunks` parameters to control how much context is included with each match:

* Set both to 0 for precise matches without context
* Set both to 1-2 for matches with minimal context
* Set both to 3-5 for matches with substantial context

### Matching Threshold

The `t` parameter controls how strictly matches are filtered:

* Higher values (e.g., 0.9) return only very close matches
* Lower values (e.g., 0.5) return more matches with greater variety
* The default (0.7) provides a balanced approach

## Batch Operations

For high-throughput applications, Rememberizer supports efficient batch operations on vector stores. These methods optimize performance when processing multiple search queries.

### Batch Search Implementation

{% tabs %}
{% tab title="Python" %}

```python
import requests
import time
import concurrent.futures

def batch_search_vector_store(vector_store_id, queries, num_results=5, batch_size=10):
    """
    Perform batch searches against a vector store
    
    Args:
        vector_store_id: ID of the vector store to search
        queries: List of search query strings
        num_results: Number of results per query
        batch_size: Number of parallel requests
        
    Returns:
        List of search results
    """
    headers = {
        "x-api-key": "YOUR_API_KEY"
    }
    
    results = []
    
    # Process in batches to avoid overwhelming the API
    for i in range(0, len(queries), batch_size):
        batch_queries = queries[i:i+batch_size]
        
        with concurrent.futures.ThreadPoolExecutor(max_workers=batch_size) as executor:
            futures = []
            
            for query in batch_queries:
                params = {
                    "q": query,
                    "n": num_results,
                    "prev_chunks": 1,
                    "next_chunks": 1
                }
                
                # Submit the request to the thread pool
                future = executor.submit(
                    requests.get,
                    f"https://api.rememberizer.ai/api/v1/vector-stores/{vector_store_id}/documents/search",
                    headers=headers,
                    params=params
                )
                futures.append(future)
            
            # Collect results from all futures
            for future in futures:
                response = future.result()
                if response.status_code == 200:
                    results.append(response.json())
                else:
                    results.append({"error": f"Failed with status code: {response.status_code}"})
        
        # Add a delay between batches to avoid rate limiting
        if i + batch_size < len(queries):
            time.sleep(1)
    
    return results

# Example usage
queries = [
    "Integration with REST APIs",
    "Authentication protocols",
    "How to deploy to production",
    "Performance optimization techniques",
    "Error handling best practices"
]

search_results = batch_search_vector_store("vs_abc123", queries, num_results=3, batch_size=5)
```

{% endtab %}

{% tab title="JavaScript" %}

```javascript
/**
 * Perform batch searches against a vector store
 * 
 * @param {string} vectorStoreId - ID of the vector store
 * @param {string[]} queries - List of search queries
 * @param {Object} options - Configuration options
 * @returns {Promise<Array>} - List of search results
 */
async function batchSearchVectorStore(vectorStoreId, queries, options = {}) {
  const {
    numResults = 5,
    batchSize = 10,
    delayBetweenBatches = 1000,
    prevChunks = 1,
    nextChunks = 1,
    distanceThreshold = 0.7
  } = options;
  
  const results = [];
  const apiKey = 'YOUR_API_KEY';
  
  // Process in batches to manage API load
  for (let i = 0; i < queries.length; i += batchSize) {
    const batchQueries = queries.slice(i, i + batchSize);
    
    // Create promise array for parallel requests
    const batchPromises = batchQueries.map(query => {
      const url = new URL(`https://api.rememberizer.ai/api/v1/vector-stores/${vectorStoreId}/documents/search`);
      url.searchParams.append('q', query);
      url.searchParams.append('n', numResults);
      url.searchParams.append('prev_chunks', prevChunks);
      url.searchParams.append('next_chunks', nextChunks);
      url.searchParams.append('t', distanceThreshold);
      
      return fetch(url.toString(), {
        method: 'GET',
        headers: {
          'x-api-key': apiKey
        }
      })
      .then(response => {
        if (response.ok) {
          return response.json();
        } else {
          return { error: `Failed with status: ${response.status}` };
        }
      })
      .catch(error => {
        return { error: error.message };
      });
    });
    
    // Wait for all requests in batch to complete
    const batchResults = await Promise.all(batchPromises);
    results.push(...batchResults);
    
    // Add delay between batches to avoid rate limiting
    if (i + batchSize < queries.length) {
      await new Promise(resolve => setTimeout(resolve, delayBetweenBatches));
    }
  }
  
  return results;
}

// Example usage
const queries = [
  "Integration with REST APIs",
  "Authentication protocols",
  "How to deploy to production",
  "Performance optimization techniques",
  "Error handling best practices"
];

const options = {
  numResults: 3,
  batchSize: 5,
  delayBetweenBatches: 1000,
  prevChunks: 1,
  nextChunks: 1
};

batchSearchVectorStore("vs_abc123", queries, options)
  .then(results => console.log(results))
  .catch(error => console.error("Batch search failed:", error));
```

{% endtab %}

{% tab title="Ruby" %}

```ruby
require 'net/http'
require 'uri'
require 'json'
require 'concurrent'

# Perform batch searches against a vector store
#
# @param vector_store_id [String] ID of the vector store
# @param queries [Array<String>] List of search queries
# @param num_results [Integer] Number of results per query
# @param batch_size [Integer] Number of parallel requests
# @param delay_between_batches [Float] Seconds to wait between batches
# @return [Array] Search results for each query
def batch_search_vector_store(vector_store_id, queries, num_results: 5, batch_size: 10, delay_between_batches: 1.0)
  results = []
  api_key = 'YOUR_API_KEY'
  
  # Process in batches
  queries.each_slice(batch_size).with_index do |batch_queries, batch_index|
    # Create a thread pool for concurrent execution
    pool = Concurrent::FixedThreadPool.new(batch_size)
    futures = []
    
    batch_queries.each do |query|
      # Submit each request to thread pool
      futures << Concurrent::Future.execute(executor: pool) do
        uri = URI("https://api.rememberizer.ai/api/v1/vector-stores/#{vector_store_id}/documents/search")
        params = {
          q: query,
          n: num_results,
          prev_chunks: 1,
          next_chunks: 1
        }
        
        uri.query = URI.encode_www_form(params)
        
        request = Net::HTTP::Get.new(uri)
        request['x-api-key'] = api_key
        
        http = Net::HTTP.new(uri.host, uri.port)
        http.use_ssl = true
        
        begin
          response = http.request(request)
          
          if response.code.to_i == 200
            JSON.parse(response.body)
          else
            { "error" => "Failed with status code: #{response.code}" }
          end
        rescue => e
          { "error" => e.message }
        end
      end
    end
    
    # Collect results from all futures
    batch_results = futures.map(&:value)
    results.concat(batch_results)
    
    # Add delay between batches
    if batch_index < (queries.length / batch_size.to_f).ceil - 1
      sleep(delay_between_batches)
    end
  end
  
  pool.shutdown
  results
end

# Example usage
queries = [
  "Integration with REST APIs",
  "Authentication protocols",
  "How to deploy to production", 
  "Performance optimization techniques",
  "Error handling best practices"
]

results = batch_search_vector_store(
  "vs_abc123", 
  queries, 
  num_results: 3, 
  batch_size: 5
)

puts results
```

{% endtab %}
{% endtabs %}

### Performance Optimization for Batch Operations

When implementing batch operations for vector store searches, consider these best practices:

1. **Optimal Batch Sizing**: For most applications, processing 5-10 queries in parallel provides a good balance between throughput and resource usage.
2. **Rate Limiting Awareness**: Include delay mechanisms between batches (typically 1-2 seconds) to avoid hitting API rate limits.
3. **Error Handling**: Implement robust error handling for individual queries that may fail within a batch.
4. **Connection Management**: For high-volume applications, implement connection pooling to reduce overhead.
5. **Timeout Configuration**: Set appropriate timeouts for each request to prevent long-running queries from blocking the entire batch.
6. **Result Processing**: Consider processing results asynchronously as they become available rather than waiting for all results.
7. **Monitoring**: Track performance metrics like average response time and success rates to identify optimization opportunities.

For production applications with very high query volumes, consider implementing a queue system with worker processes to manage large batches efficiently.

This endpoint allows you to search your vector store using semantic similarity. It returns documents that are conceptually related to your query, even if they don't contain the exact keywords. This makes it particularly powerful for natural language queries and question answering.
