Skip to main content
New projects should use the Documents API. To move an existing index, see Move to the Documents API.
This page shows you how to query a legacy vector index with the legacy Vectors API. For a document index, see Semantic search, Sparse-vector search, and Filter by metadata.

Query with dense vectors

In a legacy vector index, the query is either a dense vector or the ID of a record whose vector Pinecone uses as the query.

Query with a dense vector

To search with a dense vector representation of a query, use the Search with a vector operation with the following parameters:
  • The namespace to query. To use the default namespace, set the namespace to "__default__".
  • The vector parameter with the dense vector values representing your query.
  • The top_k parameter with the number of results to return.
  • Optionally, set include_values, include_metadata, or both to true to include the vector values, metadata, or both for the matching records in the response. For better performance, especially with higher top_k values, avoid including vector values unless you need them. See Decrease latency for more details.
For example, the following code uses a dense vector representation of the query “Disease prevention” to search for the 3 most semantically similar records in the example-namespace namespace:
In the response, each record has a similarity score that represents its distance to the query vector, calculated according to the index’s similarity metric:

Query with a record ID

When you search with a record ID, Pinecone uses the dense vector associated with the record as the query. Because search is approximate, the results aren’t guaranteed to include that record, even with a high top_k. To retrieve a specific record, fetch it by ID. To search with a record ID, use the Search with a vector operation with the following parameters:
  • The namespace to query. To use the default namespace, set the namespace to "__default__".
  • The id parameter with the unique record ID containing the vector to use as the query.
  • The top_k parameter with the number of results to return.
  • Optionally, set include_values, include_metadata, or both to true to include the vector values, metadata, or both for the matching records in the response. For better performance, especially with higher top_k values, avoid including vector values unless you need them. See Decrease latency for more details.
For example, the following code uses an ID to search for the 3 records in the example-namespace namespace that are most semantically similar to the dense vector in the record:

Query with sparse vectors

In a legacy vector index, the query is either a sparse vector or the ID of a record whose sparse vector Pinecone uses as the query.

Query with a sparse vector

To search an index of sparse vectors with a sparse vector representation of a query, use the Search with a vector operation with the following parameters:
  • namespace: The namespace to query. To use the default namespace, set to "__default__".
  • sparse_vector: The sparse vector values and indices.
  • top_k: The number of results to return.
  • include_values: Whether to include the vector values of the matching records in the response. Defaults to false.
  • include_metadata: Whether to include the metadata of the matching records in the response. Defaults to false.
When querying with top_k over 1000, for optimal performance set include_values=false and include_metadata=false to return only IDs and scores.Since vectors values are retrieved from object storage, include them in your query results only when you need them (especially with higher top_k values). For details, see Decrease latency.
For example, the following code uses a sparse vector representation of the query “What is AAPL’s outlook, considering both product launches and market conditions?” to search for the 3 most similar vectors in the example-namespace namespace:
The results look like this. The most similar records are scored highest.

Query with a record ID

When you search with a record ID, Pinecone uses the sparse vector associated with the record as the query. To search an index of sparse vectors with a record ID, use the Search with a vector operation with the following parameters:
  • namespace: The namespace to query. To use the default namespace, set to "__default__".
  • id: The unique record ID containing the sparse vector to use as the query.
  • top_k: The number of results to return.
  • include_values: Whether to include the vector values of the matching records in the response. Defaults to false.
  • include_metadata: Whether to include the metadata of the matching records in the response. Defaults to false.
When querying with top_k over 1000, for optimal performance set include_values=false and include_metadata=false to return only IDs and scores.Since vectors values are retrieved from object storage, include them in your query results only when you need them (especially with higher top_k values). For details, see Decrease latency.
For example, the following code uses an ID to search for the 3 records in the example-namespace namespace that best match the sparse vector in the record:

Query with a metadata filter

The following code queries for the 3 records that are most semantically similar to a query vector and that have a category metadata field with the value digestive system. For the supported operators, see Metadata filter expressions.