AI

Create an Index in Pincoce Using Node.js

Pinecone DB is an advanced vector database that leverages the cutting-edge techniques including approximate nearest neighbor search, indexing structures, compression algorithms, and distributed computing to enable the efficient storage, retrieval, and similarity search of high-dimensional vectors.

What Is an Index in Pinecone?

In Pinecone, an index is a critical component of the functionality of the database. It provides a highly optimized data structure for organizing and optimizing the storage and querying of a high-dimensional vector data.

In this tutorial, we will learn how to use the Node.js Pinecone client to connect to a Pinecone client and create a new index.

Requirements:

To follow along with this post, ensure that you have the following:

  1. Installed Node.js 17 and above
  2. 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:

$ npm install @pinecone-database/pinecone

Once installed, we can proceed and learn how to configure Pinecone with Node.js.

Initializing a 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:

import { PineconeClient } from "@pinecone-database/pinecone";

 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:

await pinecone.createIndex({

 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.

List the Indexes

Once we create an index, we can use the listIndexes() method to list all the available indexes in that server as follows:

const indexesList = await pinecone.listIndexes();

console.log(indexesList)

This should get a list of all available indexes from the server. An example output is as follows:

[ 'sample-index' ]

Pinecone Index Information

You can also use the “DescribeIndex” method to log all the information about the created index as demonstrated in the following:

const indexDescription = await pinecone.describeIndex({

  indexName: "sample-index",

});

console.log(indexDescription)

An example output is as follows:

{
  database: {
    name: 'sample-index',
    dimensions: undefined,
    indexType: undefined,
    metric: 'cosine',
    pods: 1,
    replicas: 1,
    shards: 1,
    podType: undefined,
    indexConfig: undefined,
    metadataConfig: undefined
  },
  status: { ready: true, state: 'Ready' }
}

Delete an Index in Pinecone

Once you are done with the index, you can remove the index using the “DeleteIndex” method. An example is as follows:

await pinecone.deleteIndex({

indexName: "sample-index",

});

This should remove the specified index from the server.

Conclusion

You learned how to work with the Pinecone client for Node.js to connect to the Pinecone server, create a new Pinecone index, list the available indexes, gather an index information, and remove the index from the server.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list