Hi @taimoorqureshi80,
While it’s not possible to query multiple namespaces at the same time, you could implement a function that performs a query against a parameterized namespace, and then call this function concurrently using async syntax in your desired programming language.
For example, if you’re working in JavaScript, you could imagine having a function that queries one namespace, and then wrapping multiple invocations of that function in a Promise.all:
// This is sample code you may need to modify or adopt to your application
async function queryNamespace(namespace, query) {
// Assuming you have already initialized the Pinecone client
const index = pinecone.Index("your-index-name");
const queryRequest = {
vector: query.vector,
topK: query.topK,
includeValues: query.includeValues,
includeMetadata: query.includeMetadata,
namespace: namespace
};
return await index.query(queryRequest);
}
async function queryMultipleNamespaces(namespaces, query) {
const queryPromises = namespaces.map(namespace => queryNamespace(namespace, query));
return await Promise.all(queryPromises);
}
// Usage
const namespaces = ["namespace1", "namespace2", "namespace3"];
const query = {
vector: [0.1, 0.2, 0.3],
topK: 10,
includeValues: true,
includeMetadata: true
};
queryMultipleNamespaces(namespaces, query)
.then(results => {
results.forEach((result, index) => {
console.log(`Results from namespace ${namespaces[index]}:`, result);
});
})
.catch(error => console.error("Error querying namespaces:", error));
Or if you wanted to achieve something similar in Python:
# This is sample code you may need to modify or adopt to your application
import asyncio
from pinecone import Pinecone
async def query_namespace(pc, index_name, namespace, query):
index = pc.Index(index_name)
return index.query(
vector=query['vector'],
top_k=query['top_k'],
include_values=query['include_values'],
include_metadata=query['include_metadata'],
namespace=namespace
)
async def query_multiple_namespaces(pc, index_name, namespaces, query):
tasks = [query_namespace(pc, index_name, namespace, query) for namespace in namespaces]
return await asyncio.gather(*tasks)
# Usage
async def main():
pc = Pinecone(api_key="your-api-key")
index_name = "your-index-name"
namespaces = ["namespace1", "namespace2", "namespace3"]
query = {
'vector': [0.1, 0.2, 0.3],
'top_k': 10,
'include_values': True,
'include_metadata': True
}
results = await query_multiple_namespaces(pc, index_name, namespaces, query)
for namespace, result in zip(namespaces, results):
print(f"Results from namespace {namespace}:", result)
if __name__ == "__main__":
asyncio.run(main())
I hope this helps!
Best,
Zack