Assigning weights to the values externally in MongoDB using Java
mongodb
Solution
Assigning weights to the indexed fields from `Java`:
BasicDBObject index = new BasicDBObject();
index.put("content", "text");
index.put("keywords", "text");
index.put("about", "text");
BasicDBObject weights = new BasicDBObject("content", 10).append("keyword", 5);
BasicDBObject options = new BasicDBObject("weights", weights).append("name", "TextIndex");
collection.createIndex(index,options);
Problem
This is how I could create index with different weights in Mongo Shell. ``` db.blog.ensureIndex( { content: "text", keywords: "text", about: "text" }, { weights: { content: 10, keywords: 5, }, name: "TextIndex" } ) ``` This is how I created Index for the fields using Java. ``` BasicDBObject index = new BasicDBObject(); index.put("content", "text"); index.put("keywords", "text"); index.put("about", "text"); collection.ensureIndex( index, "TextIndex"); ``` How do I assign weights using the `Java` driver?