There us no quote at the end. guid is not missing is not required. guid IN … make the missing predicate implicit
curl -v http://localhost:8093/query/service -H “Content-Type: application/json” -d ‘{“statement”:“select * from table1 where doc_type="xyz" AND guid in $guid_list;”,“$guid_list”:[“123”,“234”]}’
This doesnt work. But if i try to pass the guid_list with single quote and hardcode the list in my query it works fine. Not sure why it doesnt work when i pass it as list. Is it because when i pass as list it goes in as double quotes [“a1b3594f-0b76-4c54-8206-db2c16286320”] instead of [‘a1b3594f-0b76-4c54-8206-db2c16286320’].
I saw same issue was reported by someone on different thread.
Does it work using the curl method posted by @vsr1 above. What response do you get when you query through the java sdk?
You can try printing the query generated N1qlQuery q = N1qlQuery.parameterized(statement, placeHolders); System.out.println(q.toString());
and compare if the results are similar with curl
@subhashni : Unfortunately i am not able to connect to remote server using curl. But below is my observation:
First thing,
String Query = “select * from table1 where guid is not missing and doc_type=‘xyz’ and (guid in $guid_list);”
N1qlQuery statement = N1qlQuery.parameterized(Query,placeholders) where my Query is like above - passed in as String.
I get below result
{“statement”:“select * from table1 where guid is not missing and doc_type=‘xyz’ and (guid in $guid_list)”,“$guid_list”:[“a1b3594f-0b76-4c54-8206-db2c16286320”]}
This doesnt return any result.
When i hard code the values in the query it self my String query looks like below and it return the expected result
String query = “select * from table1 where guid is not missing and doc_type=‘xyz’ and (guid in ["a1b3594f-0b76-4c54-8206-db2c16286320"])”;
N1qlQuery statement = N1qlQuery.simple(query);
now when i print - System.out.println(“Query Statement :”+statement.n1ql().toString());
Query Statement :{“statement”:“select * from table1 where guid is not missing and doc_type=‘xyz’ and (guid in ["a1b3594f-0b76-4c54-8206-db2c16286320"])”}
Also, i tried to concatenate string with single quotes using Joiner and that works too. For some reason only when i pass the list in my parameterized query - it doesnt work.
Hi @vsr1 i dont see any problems with that. The only problem i see is - when i pass the list in parameterized query - it goes as “$guid_list”:[“a1b3594f-0b76-4c54-8206-db2c16286320”] - which seems to be having some problem.
Do you have any example - how to pass list in N1QL parameterized query ?
As mentioned above my query looks like below
select * from table1 where guid is not missing and doc_type=‘xyz’ and (guid in $guid_list)
insert into default values("01",{"doc_type":"xyz","guid":"a1b3594f-0b76-4c54-8206-db2c16286320"});
CREATE PRIMARY INDEX ON default;
curl -v http://localhost:8093/query/service -H “Content-Type: application/json” -d ‘{“statement”:“select * from default where doc_type=“xyz” and (guid in $guid_list)”,"$guid_list":[“a1b3594f-0b76-4c54-8206-db2c16286320”],“creds”:[{“user”: “XXX”, “pass”:“XXX”}]}’
i’m facing the same problem and literally stuck. its would be a great help if acknowledge the issue to us.
FYI m using the same version of CB and java sdk
@subhashni : I tried using java sdk on the default bucket on my local - it works fine . I inserted the row and created index on default bucket and from my Java program i tried to run the query - it just worked fine.
But when i run the query against my actual bucket in my staging environment it just doesnt return any result .however as i said if i put the values hardcoded in my query it just works fine.
My understanding is, if it has to be any problem with index - even putting the hardcoded values it shouldn’t work , right ?
If its working putting the values directly then it should not have any index problem. Because the only difference between putting direct value vs. parameterized query is - we pass the list in parameter instead of values directly.
Hi @rkumar, I did not acknowledge an issue, instead I answered it works for me. Can you share the specific query you are trying to execute. Here is the code I had used for @parth.j.shah’s query
Hi @parth.j.shah,
Add EXPLAIN keyword before SELECT and post the output.
Also you can try this in cbq shell.
PREPARE p1 AS SELECT * FROM default WHERE doc_type=‘xyz’ AND guid IN $guid_list;
\set -$guid_list [“a1b3594f-0b76-4c54-8206-db2c16286320”] ;
execute p1;
If that doesn’t work post
EXPLAIN SELECT * FROM default WHERE doc_type=‘xyz’ AND guid IN $guid_list;
Hi,
Below is the query it make(no index except primary is created)
select * from default AS d
unnest d.avatar AS a
unnest d.source AS s
where d.time between $startDate and $endDate
and a.id IN $avatars
and s.id IN $sources
and d.client= xxx
ORDER BY d.name ASC
and limit 100 offset 10
and after printing N1qlQuery statement ,below is the result it shown:
ParameterizedN1qlQuery{statement=select * from default AS d unnest d.avatar AS a unnest d.source AS s where d.event between $startDate and $endDate and a.id IN $avatars and s.id IN $sources and d.client= xxx ORDER BY d.name ASC limit 100 offset 10, params={“sources”:[[“111111”]],“endDate”:" 2017-05-20T23:59:59.000Z",“startDate”:"2017-05-01T00:00:00.000Z ",“avatars”:[[“2222222”]]}}
and below is the java code to create the laceholder.
JsonObject placeHolders = JsonObject.create()
.put(“avatars”, JsonArray.from(Collections.singletonList(new ArrayList(avatarIds))))
.put(“sources”, JsonArray.from(Collections.singletonList(new ArrayList(sourceIds))))
.put(“startDate”, fromDate)
.put(“endDate”, toDate)
as avatarIds and sourceIds are set ,so converting from set to list inside.
you are supplying “avatars”:[[“2222222”]] is your document has “avatar”:{“id”:[“2222222”]} i.e id as array of string If not you need to supply right value
Also not sure what exactly you want to project. If predicate matches if you want whole document the following right query. It can be done without UNNEST. You can consider once actual problem is resolved.
SELECT d.* FROM default AS d
WHERE d.time BETWEEN $startDate AND $endDate AND d.client= xxx AND
ANY a IN d.avatar SATISFIES a.id IN $avatars END AND
ANY s IN d.source SATISFIES s.id IN $sources END AND
ORDER BY d.name ASC LIMIT 100 OFFSET 10
I had used Collections.singletonList for that query since there is one object. For your query it should be .put("avatars", JsonArray.from(Collections.singletonList("2222222")));