如何使用Java Driver在MongoDB中执行全文搜索命令? [英] How to execute full text search command in MongoDB with Java Driver ?

查看:611
本文介绍了如何使用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.

这篇关于如何使用Java Driver在MongoDB中执行全文搜索命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆