Special N1QL and Iterator handling

I found an N1QL query that allows me to get all documents starting with an special id.

SELECT * FROM `feeder` WHERE META(`feeder`).id LIKE `GO_SF_USER::%`

Using this as text is easy but I tried to build it with Java.

Statement statement = select("*").from(i(feederBucket.name())).where(meta(i(feederBucket
			.name())).add("id").like(x("GO_SF_USER::%"))).limit(1000);

.add(“id”) is wrong I know. I haven’t found the correct method.

@Override
public Iterable<T> findAll() {
	Statement statement = select("*").from(i(feederBucket.name())).where("META(" + feederBucket
			.name() + ").id like 'GO_SF_USER::%' LIMIT 1000");
	log.debug("Query: {}", statement);
	N1qlQuery query = N1qlQuery.simple(statement);
	final N1qlQueryResult result = feederBucket.query(query);
	Iterable<T> it = new Iterable<T>() {
		private Iterator<N1qlQueryRow> rows = result.iterator();

		@Override
		public Iterator<T> iterator() {
			return new Iterator<T>() {

				@Override
				public boolean hasNext() {
					return rows.hasNext();
				}

				@Override
				public T next() {
					N1qlQueryRow row = rows.next();
					log.debug("row: {}", row);
					return null;
				}
			};
		}
	};
	return it;
}

This is an excerpt from my log.
As you can see it takes 25 seconds to get the first result out of 110.000 documents. I know, one can limit this with offset and limit for pagination, but what about processes that should run over all documents. Is the Iterable not the right approach?

22:19:54.945 DEBUG [main] d.p.s.couchbase.AbstractRepository - Query: SELECT * FROM `feeder` WHERE META(feeder).id like 'GO_SF_USER::%'
22:20:19.503 DEBUG [main] d.p.s.couchbase.AbstractRepository - row: {"feeder":{"feeder_state":0,"_type":"genericobject","FIRSTNAME":"NO_MANAGER","CUSTOM_MANAGER":"TECH_GLOBAL|TECH_GLOBALHR","EMAIL":"no_manager@localhost.com","type":"09d192b7-b09f-48d1-8fd7-df726912850c","EmployeeType":"Internal","DISPLAY":"bullet_red","AcceptTransfer":false,"Scope":"Both","LASTNAME":"NO_MANAGER","DEFAULT_LOCALE":"en_GB","_created":"2017-01-18T20:28:02.861+01:00","HR_feedback_field":"No HR Rep lookup possible for unit: null and flag: F","USERID":"NO_MANAGER","USERNAME":"no_manager@localhost.com","_id":"583c5771aad2d4361ed04c49","_modified":"2017-01-18T20:28:02.861+01:00"}}

My last question is because of the row. The result is wrapped in an object with property feeder. Is it possible to get rid of this wrapping or even better, if it is wrapped somehow, then why not in the JsonDocument with id and content and all the other information?

Thank you for your help…

Hi @mburchard, for selecting meta().id using dsl, path(meta(i(“feeder”)),“id”) should work.
N1qlQueryResult is already an iterable, you don’t need to wrap it into another one.

It’ll be useful to log N1qlMetrics result.info to check the query execution times. If you’ve have a large result set and want to process results as they arrive, using async operations would be the right approach.