How to ensure written doc is included in next query?

query

#1

I have a DAO (Data Access Object) class that handles reads and writes to my data. Everything is in Java and I am using v.2.6.1 of the Java SDK - and Community server 5.1.1

I have run into this scenario:

I read a list of documents and in the interface the user creates a new document. So I save the document and then the code reads all the relevant docs again to include the new one… However, this often doesn’t include the new document - I guess the reason is related to index updates.

The code that saves the document ends:

	doc.content.put("field", "some data...");
	bucket.upsert(doc);
	bucket.invalidateQueryCache(); // Force re-read... This seems not to work??

And then I re-read the entire list of relevant documents for that scenario:

	String q = "SELECT * FROM data WHERE type=$1 AND (userkey='" + user +"')");
	N1qlQueryResult result = bucket.query(N1qlQuery.parameterized(q, JsonArray.from(formName)));
	return result;

If I re-load the same page then the extra document is normally there…

How can I make sure that the newly created document in these scenarios is also returned to the complete list for the user to work with? It is fine if I can control when and not just always force the re-cache or whatever controls it :slight_smile:


#2

Checkout Scan Consistency https://developer.couchbase.com/documentation/server/current/indexes/performance-consistency.html


#3

Bingo!

By modifying my code to this:

	String q = "SELECT * FROM data WHERE type=$1 AND (userkey='" + user +"')");
	N1qlParams parms = N1qlParams.build().consistency(ScanConsistency.STATEMENT_PLUS);
	N1qlQuery query = N1qlQuery.parameterized(q, JsonArray.from(formName), parms);
	N1qlQueryResult result = bucket.query(query);

then I get the newly saved document back with the total list!

… so now I just need to implement a little logic to only use the consistency flag when I know it is needed :wink: