Using Atlas Search with Java not working

Hi, I’d like to use Atlas Search in Java.

I created a working query in Compass, and exported its Java code.

AggregateIterable<Document> result = collection.aggregate(Arrays
                                eq("query", line),
                                eq("path", "title"),
                                eq("fuzzy", eq("maxEdits", 1L)),
                                eq("score", eq("boost", eq("value", 2L))))),
                                eq("query", line),
                                eq("path", "ausschreibungsText"),
                                eq("fuzzy", eq("maxEdits", 1L)),
                                eq("score", eq("boost", eq("value", 1L))))),
                                eq("query", line),
                                eq("path", "artikelnummerHersteller"),
                                eq("score", eq("boost", eq("value", 4L))))))))),
                    include("title", "ausschreibungsText", "artikelnummerHersteller", "netPrice", "brutoPrice", "basismengenEinheit"),
                    computed("score", eq("$meta", "searchScore"))))));, false)
        .peek(a ->"result: {}", a))

Exception I am getting is:

com.mongodb.MongoCommandException: Command failed with error 8 (UnknownError): ‘Remote error from mongot :: caused by :: “path” is required (from “compound.should[0].text”)’ on server The full response is {“operationTime”: {“$timestamp”: {“t”: 1613647388, “i”: 2}}, “ok”: 0.0, “errmsg”: “Remote error from mongot :: caused by :: "path" is required (from "compound.should[0].text")”, “code”: 8, “codeName”: “UnknownError”, “$clusterTime”: {“clusterTime”: {“$timestamp”: {“t”: 1613647388, “i”: 2}}, “signature”: {“hash”: {“$binary”: {“base64”: “XcTWq9PN6XA+VigM2gDaifLd+9g=”, “subType”: “00”}}, “keyId”: 6928397309239623682}}}
at com.mongodb.internal.connection.ProtocolHelper.getCommandFailureException( ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse( ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive( ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.connection.UsageTrackingInternalConnection.sendAndReceive( ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.connection.DefaultConnectionPool$PooledConnection.sendAndReceive( ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.connection.CommandProtocolImpl.execute( ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.connection.DefaultServer$DefaultServerProtocolExecutor.execute( ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.connection.DefaultServerConnection.executeProtocol( ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.connection.DefaultServerConnection.command( ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.connection.DefaultServerConnection.command( ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.operation.CommandOperationHelper.executeCommand( ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.operation.CommandOperationHelper.executeCommand( ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.operation.CommandOperationHelper.executeCommandWithConnection( ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.operation.CommandOperationHelper$ ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.operation.OperationHelper.withReadConnectionSource( ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.operation.CommandOperationHelper.executeCommand( ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.operation.AggregateOperationImpl.execute( ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.operation.AggregateOperation.execute( ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.operation.AggregateOperation.execute( ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute( ~[mongodb-driver-sync-4.0.5.jar:na]
at com.mongodb.client.internal.MongoIterableImpl.execute( ~[mongodb-driver-sync-4.0.5.jar:na]
at com.mongodb.client.internal.MongoIterableImpl.iterator( ~[mongodb-driver-sync-4.0.5.jar:na]
at com.mongodb.client.internal.MongoIterableImpl.iterator( ~[mongodb-driver-sync-4.0.5.jar:na]
at java.base/java.lang.Iterable.spliterator( ~[na:na]

So message is “path” is required (from “compound.should[0].text”)’ but I am providing this path.
What am I doing wrong here?

Marco, I believe this issue has to do with improperly formatted BSON. I am looking into your precise issue. In the meantime, you can follow this related JIRA.

1 Like

Thanks Mark, please let me know, since I am blocked by this.

Hello @Marco_Dell_Anna, meanwhile if you want to, you can try this approach to execute the aggregation query in Java:

Thanks @Prasad_Saya, using native query is bypassing the problem.
Now I got a List; since I am using Spring Data MongoDB, do you know what is the recommended approach to transform a Document into a POJO?

Hello @Marco_Dell_Anna, Spring Data MongoDB has its own APIs to work with aggregation queries. You can use MongoOperations (its implementation MongoTemplate class) or MongoRepository to build your aggregation query. For example, using MongoTemplate#Aggregate method (See MongoTemplate APIdocs) you can return the output of your aggregation as a POJO types instead of the Document class.

Also, see this post solves an aggregation using Spring Data MongoDB’'s MongoRepository API: Compass pipeline export to Java not producing same results

Thanks @Prasad_Saya, I am trying to create the query in the Spring Data MongoDB way (as in java - How to do this aggregation in spring data mongo db? - Stack Overflow), but I still need to use the “fuzzy” and “boost” functionalities provided by Atlas Search. Is it possible to do it?
If not I will need to use native query and I will need another way to create the POJOs, right?

I don’t see the API for the $search pipeline stage in the MongoTemplate#Aggregate methods. I don’t know if Spring Data supports Atlas Search (did not find anything online, yet).

I think, yes. You need some routine to transform the Document class to a POJO ( a method to extract fields from the Document class and map them to a POJO class).

Hi Marco,

thanks for bringing that issue up! It was fixed in the new Compass 1.26.0 Beta that we published. It will generate the Java Code for aggregation pipelines without the fluent API for now. We are working on providing the fluent API for aggregation pipelines with all the different stages as soon as possible.

You should also be able to generate a working Java aggregation pipeline in previous versions of Compass by disabling the “Use Builders” checkbox in the “Export Pipeline to Language” dialog.

Let me know if that helps and sorry for the inconvenience!

1 Like

Spring Data does not yet support $search pipeline natively, but we could see support for it soon.


This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.