1

Hi my Mongo db fields as follows:

        "_id" : ObjectId("51d582be7a8d51bd29ca78a3"),
        "Filename" : "sample.pdfe",
        "Title" : null,
        "Author" : null,
        "ContentType" : "application/x-msdownload; format=pe32",
        "url" : "Z:sample.pdf",
        "Keywords" : ["php","java",".net","python"]

below is my code to do full text search on indexed fields

m = new Mongo("10.0.0.26", 27017);
DB db = m.getDB("soft") ;
DBCollection col = db.getCollection("metadatanew") ;
String collection = col.toString();
DBObject searchCmd = new BasicDBObject();
searchCmd.put("text", collection); 
searchCmd.put("search", name); 

CommandResult commandResult = db.command(searchCmd);

BasicDBList results = (BasicDBList)commandResult.get("results");

for (Iterator <Object> it = results.iterator();it.hasNext();)
{
    BasicDBObject result  = (BasicDBObject) it.next();
    BasicDBObject dbo = (BasicDBObject) result.get("obj");
    System.out.println(dbo.getString("Filename"));
}

I have used below command to create Text index on two fields:

db.metadatanew.ensureIndex({'Filename':"text"}, {'Keywords':"text"})

I have created text index on filename and keywords but i can able to do full text search on filename only, is there any wrong in my code, please help me

2 Answers 2

4

The second parameter on ensureIndex function is options. You should create an unique index on both fields:

db.metadatanew.ensureIndex( { "Keywords" : "text", "Filename" : "text"} )

See more:

http://docs.mongodb.org/manual/tutorial/create-text-index-on-multiple-fields/

Sign up to request clarification or add additional context in comments.

Comments

2

Another way to create an unique index on both fields :

BasicDBObject basicDBObject = new BasicDBObject();
basicDBObject.append("Keywords", "text");
basicDBObject.append("Filename", "text");
dbCollection.createIndex(basicDBObject);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.