如何使用Java Driver在MongoDB中执行全文搜索命令?

问题描述:

Mongo和Java大师。我们的团队决定使用最近在MongoDB中引入的全文搜索API。但是,我们发现使用Java MongoDB驱动程序执行命令有些困难。

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);
}

这是我打印时的结果

 System.out.println(commandResult) 

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


摘自Google群组中的帖子( https://groups.google.com/forum/?fromgroups#!topic/mongodb-user/7jWUbunUcFQ ):

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.