0
votes

I am trying to do Full Text Search in Mongo DB 3.2 Java Driver

Following is my Code :

  DBObject textSearchCommand = new BasicDBObject();
    textSearchCommand.put("text", collectionName);
    textSearchCommand.put("search", "MAURICE");
CommandResult commandResult = db.command(textSearchCommand);
     System.out.println("Command result is "+commandResult.toString());

Getting following error :

Command result is { "ok" : 0.0 , "errmsg" : "no such command: 'text', bad cmd: '{ text: \"citizen5\", search: \"MAURICE\" }'" , "code" : 59}

I have already created textIndex :

{ "v" : 1 , "key" : { "_fts" : "text" , "_ftsx" : 1} , "name" : "MyTextIndex" , "ns" : "matcher.citizen5" , "weights" : { "address" : 1 , "firstname" : 1 , "lastname" : 1 , "metaaddress" : 1 , "metafirstname" : 1 , "metalastname" : 1 , "mobile" : 1} , "default_language" : "english" , "language_override" : "language" , "textIndexVersion" : 3}

Can anybody point out where am I doing wrong?

1

1 Answers

0
votes

I got the solution. MongoDB 3.0 onwards text command is disabled.

Hence, you can use the normal find command to search using a query.

Here is my code :

 BasicDBObject query = new BasicDBObject();
  query.put("$text", new BasicDBObject("$search",value));
  DBCursor cursor = db.getCollection(dbName).find(query);
  while (cursor.hasNext()) {
      BasicDBObject obj = (BasicDBObject) cursor.next();
      System.out.println(obj.toString()); 

}