MongoDB Java API: Full-Text Search

I have a problem using Java API for MongoDB. I created a query using Robomongo:

db.collection.find( {$text : {$search : "\"expression\" keyword"}}, {score : {$meta : "textScore"}} ).sort({score : {$meta : "textScore"}}) 

Now I would like to create the same query using the Java API:

 DBObject searchCommand = new BasicDBObject( "$text", new BasicDBObject("$search", "\"expression\" keyword") ).append( "score", new BasicDBObject("'$meta'", "textScore") ); DBObject sorting = new BasicDBObject( "score", new BasicDBObject("'$meta'", "textScore") ); DBCursor result = collection.find(searchCommand).sort(sorting); 

The problem is that this code does not work. Request:

 DBObject searchCommand = new BasicDBObject( "$text", new BasicDBObject("$search", "\"expression\" keyword") ); 

works great. After adding the second part, all results become invisible. What else is this line:

 DBCursor result = collection.find(searchCommand).sort(sorting); 

throws a MongoException (BadValue bad sort specification). When I delete the call to the sort () method, there is no Exception, but still I have no results (if I add a "grade").

I found a solution to this problem, but using Spring. I would not want to use any other libraries. Also, I am new to MongoDB. Thanks for your help and time, greetings.


UPDATE The problem is solved. Adding a β€œscore” to a searchCommand query that passed as the first find () parameter is incorrect. The "score" must be passed in a separate DBObject as the second parameter to the find () method as follows:

 DBObject search = new BasicDBObject( "$text", new BasicDBObject("$search", "\"expression\" keyword") ); DBObject project = new BasicDBObject( "score", new BasicDBObject("$meta", "textScore") ); DBObject sorting = new BasicDBObject( "score", new BasicDBObject("$meta", "textScore") ); DBCursor result = collection.find(search, project).sort(sorting); 
+5
source share
1 answer

You are really close with what you have tried.

Please note that in,

 db.collection.find( {$text : {$search : "\"expression\" keyword"}}, {score : {$meta : "textScore"}} ).sort({score : {$meta : "textScore"}}) 
  • {$text : {$search : "\"expression\" keyword"}} is part of the query .

  • {score : {$meta : "textScore"}} is part of the projection .

In what you tried to implement using the Java driver,

 DBObject searchCommand = new BasicDBObject( "$text", new BasicDBObject("$search", "\"expression\" keyword") ).append( "score", new BasicDBObject("'$meta'", "textScore") ); 

finish creation

 {$text:{$search:"\"expression\" keyword"},"score":{"meta":"textscore"}} 

which is not the equivalent of your own request. Even the supposed projection statement was part of the query itself.

Note that this ends with a search for a field named score , since now it has become part of query and not projection .

You can easily modify your DBObject instances to make it part of the projection parameter, and it will work:

 DBObject findCommand = new BasicDBObject( "$text", new BasicDBObject("$search", "keyword") ); DBObject projectCommand = new BasicDBObject( "score", new BasicDBObject("$meta", "textScore")); DBObject sortCommand = new BasicDBObject( "score", new BasicDBObject("$meta", "textScore") ); DBCursor result = collection.find( findCommand ,projectCommand) .sort(sortCommand ); 
+7
source

Source: https://habr.com/ru/post/1237996/


All Articles