How to create Vector search index using java

Hi,

I want to create vector search index using Java, springboot and mongoCK.
Can anyone guide me how can I do so,
I was able to create vector search index through below given mediums-

  • Atlas UI
  • Atlas Search API
  • Atlas CLI

Is it possible to create using mongoCK or mongo template.

I am currently using the below mentioned code -

Document vectorField = new Document(“type”, “vector”)
.append(“numDimensions”, 1536)
.append(“path”, “embeddings”)
.append(“similarity”,“euclidean”);

  	Document mapping = new Document("dynamic", true).append("fields", new Document("embeddings", vectorField));

  	IndexOptions indexOptions = new IndexOptions().name("embeddings_vector_index");

  	Document mapping = new Document("dynamic", true).append("fields", new Document("embeddings", vectorField));
  	IndexOptions indexOptions = new IndexOptions().name("embeddings_vector_index");
  	return collection.createIndex(mapping, indexOptions);

But getting an error.

Regards,
Deepak Dev

Hello @Deepak_Dev you cant currently configure a Vector Search index using the server commands, but in the next rapid release (and at that point also 6.0, 7.0, 7.2) you will be able to do that again using the createSearchIndex() command.

Since it sounds like you’re using Java you will probably need to invoke this using the runCommand option in the driver.

Hi @Benjamin_Flast, Any update that when we can expect the next release. Any update on this ?

1 Like

Sorry for the delay Deepak, not sure how I missed this.

But for posterity, createSearchIndexes can now be invoked on all clusters (Free Tier through Dedicated). You will need to invoke it using the runCommand option if using the Java Driver.

Documentation is here: https://www.mongodb.com/docs/manual/reference/command/createSearchIndexes/