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
.asList(eq("$search",
eq("compound",
eq("should",
Arrays.asList(
eq("text",
and(
eq("query", line),
eq("path", "title"),
eq("fuzzy", eq("maxEdits", 1L)),
eq("score", eq("boost", eq("value", 2L))))),
eq("text",
and(
eq("query", line),
eq("path", "ausschreibungsText"),
eq("fuzzy", eq("maxEdits", 1L)),
eq("score", eq("boost", eq("value", 1L))))),
eq("text",
and(
eq("query", line),
eq("path", "artikelnummerHersteller"),
eq("score", eq("boost", eq("value", 4L))))))))),
limit(5),
project(
fields(
excludeId(),
include("title", "ausschreibungsText", "artikelnummerHersteller", "netPrice", "brutoPrice", "basismengenEinheit"),
computed("score", eq("$meta", "searchScore"))))));
StreamSupport.stream(result.spliterator(), false)
.peek(a -> LOG.info("result: {}", a))
.collect(Collectors.toList());
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 cluster0-shard-00-01.43pre.mongodb.net:27017. 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(ProtocolHelper.java:175) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:302) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:258) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.connection.UsageTrackingInternalConnection.sendAndReceive(UsageTrackingInternalConnection.java:99) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.connection.DefaultConnectionPool$PooledConnection.sendAndReceive(DefaultConnectionPool.java:500) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.connection.CommandProtocolImpl.execute(CommandProtocolImpl.java:71) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:224) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:202) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.connection.DefaultServerConnection.command(DefaultServerConnection.java:118) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.connection.DefaultServerConnection.command(DefaultServerConnection.java:110) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.operation.CommandOperationHelper.executeCommand(CommandOperationHelper.java:343) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.operation.CommandOperationHelper.executeCommand(CommandOperationHelper.java:334) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.operation.CommandOperationHelper.executeCommandWithConnection(CommandOperationHelper.java:220) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.operation.CommandOperationHelper$5.call(CommandOperationHelper.java:206) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.operation.OperationHelper.withReadConnectionSource(OperationHelper.java:462) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.operation.CommandOperationHelper.executeCommand(CommandOperationHelper.java:203) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.operation.AggregateOperationImpl.execute(AggregateOperationImpl.java:189) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.operation.AggregateOperation.execute(AggregateOperation.java:296) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.operation.AggregateOperation.execute(AggregateOperation.java:41) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:190) ~[mongodb-driver-sync-4.0.5.jar:na]
at com.mongodb.client.internal.MongoIterableImpl.execute(MongoIterableImpl.java:135) ~[mongodb-driver-sync-4.0.5.jar:na]
at com.mongodb.client.internal.MongoIterableImpl.iterator(MongoIterableImpl.java:92) ~[mongodb-driver-sync-4.0.5.jar:na]
at com.mongodb.client.internal.MongoIterableImpl.iterator(MongoIterableImpl.java:39) ~[mongodb-driver-sync-4.0.5.jar:na]
at java.base/java.lang.Iterable.spliterator(Iterable.java:101) ~[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?