2013-04-08 4 views
6

Guru Mongo e Giava. Il nostro team ha deciso di utilizzare l'API di ricerca full text, introdotta di recente in MongoDB. Tuttavia, abbiamo riscontrato alcune difficoltà nell'esecuzione del comando utilizzando il driver Java MongoDB.Come eseguire il comando di ricerca full text in MongoDB con Java Driver?

Qui è il mio codice che sto usando:

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

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

     CommandResult commandResult = db.command(searchCommand); 
} 

Questo è ciò che ottengo quando stampo

System.out.println(commandResult) 

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

risposta

11

Tratto da un post sul gruppo Google (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); 

Mostra esattamente come formattare il comando.

+0

Grazie, penso che questo sia il comando :) – Adelin

+0

come posso aggiungere il parametro di query in db.command? – rajeshpanwar