A query in Pinecone DB refers to the process of searching and retrieving the information from the Pinecone database using a specific query.
In this tutorial, we will learn how we can use the Node.js client for Pinecone DB to perform a query for the data that is stored in a given index.
Requirements:
To follow along with this post, ensure that you have the following:
- Installed Node.js 17 and above
- A configured Pinecone cluster
Installing the Pinecone Client
The first step is ensuring that the Pinecone client for Node.js is installed on the machine. We can do this by running the following command:
Once installed, we can proceed and learn how to configure Pinecone with Node.js.
Initializing the Client
Before interacting with the Pinecone database, we must create a client with the server configuration using the API key and the environment properties.
The following code shows how to use the “PineconeClient” and the init method:
const pinecone = new PineconeClient();
await pinecone.init({
environment: "us-west1-gcp-free",
apiKey: "0f57b6af-ea59-4fd3-a0ce-3c7f0c1d419f"
});
In this case, we initialize a new Pinecone client using the provided environment and API Key.
Create an Index in Pinecone Using Node.js
Once connected to the server, we can proceed and create an index to store the target data. The Node.js client provides us with the CreateIndex() method which enables us to quickly configure a new index as shown in the following example code:
createRequest: {
name: "sample-index",
dimension: 8,
metric: "cosine"
}
});
In the given example, we use the createIndex() method to create an index called “sample-index” with a dimension of 8 and a cosine distance metric.
Pinecone Node.js Upsert
In the Pinecone Node.js client, we have access to the index.upsert() method which allows us to write the vectors into a namespace. As mentioned, an upsert operation combines an insert and update operation in a single query. Hence, the operation overwrites the previous value if you upsert a new value for an existing vector ID.
The method accepts the following parameters:
- requestParameters – This defines an upsert operation wrapper.
- upsertRequest – This sets the actual upsert request.
The upsert request is composed of the following values:
- Vectors – This is an array that contains the vectors that you wish to insert.
- Id – It defines the unique ID of the vector that you wish to upsert.
- Values – It is the vector values.
- Metadata – This is an object that defines the metadata of the vector.
- Namespace – The namespace parameter defines the namespace on which you wish to insert the data. If the provided namespace does not exist, Pinecone creates one automatically.
The method returns an integer number which denores the number of records that are upserted in the index.
The following code demonstrates the upsert() method to add a data to a given Pinecone index:
const upsertResponse = await index.upsert({
upsertRequest: {
vectors: [
{
id: "vec1",
values: [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8],
metadata: {
active: true,
},
},
{
id: "vec2",
values: [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8],
metadata: {
active: false,
},
},
],
namespace: "linuxhint-namespace",
},
});
console.log(upsertResponse)
In the given example, we connect to the index where we wish to insert the data.
Next, we define the upsert request wrapper and pass the upsert request with the vector data that we wish to insert.
We also provide the target namespace that we wish to use. Since the namespace does not exist, Pinecone creates it automatically before storing the data.
Pinecone Node.js Query
The Index.query method allows us to search a namespace using a query vector. It then retrieves the IDs of the most similar items in the namespace along with their similarity scores.
The function syntax is as follows:
The method accepts the requestParameters which is a query operation request wrapper and the QueryOperationRequest as the parameters.
The parameters of the QueryOperationRequest are as follows:
- Namespace – It specifies the namespace that we wish to query.
- topK – The number of results to return from each query.
- Filter – It specifies the filter that you wish to apply to the vector metadata.
- includeValues – This is a Boolean value that determines whether the vector values are included in the result.
- includeMetadata – It specifies whether the method includes the vector metadata in the result.
- Vector – It specifies the query vector.
- Id – It specifies the unique ID of the vector that you wish to query.
Let us look at a basic example on how we can use this function. Consider the following example:
queryRequest: {
namespace: "sample-namespace",
topK: 10,
filter: {
active: { $eq: true},
},
includeValues: true,
includeMetadata: true,
vector: [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8],
},
});
console.log(queryResponse)
The previous code searches for the top 10 matching records where the active metadata field is equal to true. We also include the values and metadata from the function.
Conclusion
We learned how to use the Index.Query() method in the Pinecone Node.js client to gather the information about the matching vectors.