Trouble converting collections query to Java ParameterizedN1qlQuery

I’m having trouble converting this N1QL query to be run in the Java SDK. Can someone help point out what I’m doing wrong?

This query works great in the Query Workbench Developer. I get back 1 result:

SELECT * FROM ledo b WHERE type="questionnaire/path"
AND META(b).id LIKE "questionnaire/path/2016/%"
AND ANY group1 WITHIN questionGroups SATISFIES (group1.id = “VehExp.6so”) END

When I run this from the Java SDK, I get results with the following code:

Statement stmt = select("*")
.from(i(storage.getBucketName())).as(“b”)
.where(x(“META(b).id”).like(x("$keyPrefix"))
.and(i(“type”).eq(x("$type")))
.and(anyIn(“questionGroups”, x(“group1”)).satisfies(x(“group1.id”))));
JsonObject params = JsonObject.create()
.put(“keyPrefix”, “questionnaire/path/” + taxYear + “/%”)
.put(“type”, QuestionnairePath.DOCUMENT_TYPE)
.put(“id”, id);
ParameterizedN1qlQuery query = N1qlQuery.parameterized(stmt, params, N1qlParams.build().consistency(
ScanConsistency.REQUEST_PLUS));

But when I try to add the equals statement, I don’t get any results:

.and(anyIn(“group1”, x(“questionGroups”)).satisfies(i(“group1.id”).eq(x("$id")))));

Can someone spot my bug in translating the query to code?

Hi @hoppe,

You can use toString on Statement/ParameterizedQuery and check if your query is constructed as expected. One thing I could spot is the last part of the where clause

and(anyWithin(“group1”, x(“questionGroups”)).satisfies(x(“group1.id”).eq(x("$id")))))

this matches more with the intended query

Note: ParameterizedQuery toString is only available on recent versions 2.3.6 and greater

2 Likes