How to execute full text search command in MongoDB with Java Driver ?

孤街醉人 提交于 2019-12-19 03:12:10

问题


Mongo and Java gurus. Our team decided to use full text search API, introduced recently in MongoDB. However, we found some difficulties executing the command using the Java MongoDB driver.

here is my code I am using :

public BasicDBObject find(String search) {
    BasicDBObject searchCommand = new BasicDBObject();

        searchCommand.put("text", new BasicDBObject().append("search", search));

        CommandResult commandResult = db.command(searchCommand);
}

This is what I get when I print

 System.out.println(commandResult) 

{ "serverUsed" : "/127.0.0.1:27017" , "errmsg" : "exception: wrong type for field (text) 3 != 2" , "code" : 13111 , "ok" : 0.0 }

回答1:


Taken from a post on the Google group ( https://groups.google.com/forum/?fromgroups#!topic/mongodb-user/7jWUbunUcFQ ):

    final DBObject textSearchCommand = new BasicDBObject();
    textSearchCommand.put("text", collectionName);
    textSearchCommand.put("search", textToSearchFor);
    final CommandResult commandResult = db.command(textSearchCommand);

Shows exactly how to format the command.



来源:https://stackoverflow.com/questions/15879109/how-to-execute-full-text-search-command-in-mongodb-with-java-driver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!