1

I am creating a view in code using the Couchbase Java API 2.1.2 like this:

DefaultView view = DefaultView.create(viewName, jsMapFunctionAsString);
List<View> views = new ArrayList<>();
views.add(view);
DesignDocument doc = DesignDocument.create(name, views);
bucket.bucketManager().insertDesignDocument(doc);

Calling ViewResult result = bucket.query(ViewQuery.from(name, viewName)) directly after inserting the document, viewResult.success() always returns true, but both iterators rows() and iterator return 0 rows (there are definitely results present. When I execute the view in the web interface, it returns correct values).

The workaround I found after several hours is to call query twice with enough waiting time in between like

ViewResult result = bucket.query(ViewQuery.from(name, viewName));
Thread.sleep(10000);
result = bucket.query(ViewQuery.from(name, viewName));

The second call will then return the correct result.

It seems as if Couchbase has to build the index for the query first but returns directly, even before the index has been built.
Waiting 10 seconds is of course not optimal, maybe creating the index will take more time in the future.

So my question is, how can I make sure, that I only wait until the index has been built?
Is this a bug in the API?

Sebastian
  • 5,177
  • 4
  • 30
  • 47
  • @Bohemian With version 3 they introduced an SQL-like query language called N1QL. Haven't tried that yet. We are very happy with Couchbase, but we don't need too many queries, either. – Sebastian Apr 25 '15 at 11:10
  • 1
    @Sebastian There is a [developer preview](http://docs.couchbase.com/4.0/admin/whats-new.html#topic_hdg_nkd_54) of Couchbase Server 4 which has support for N1QL, Global secondary indexes and multi-dimensional spatial indexes which you might want to play with. – Paddy Apr 25 '15 at 12:23

1 Answers1

4

You can use the stale method and set it to false in the ViewQuery, this will force to wait for the indexation to finish before returning results.

Simon Baslé
  • 27,105
  • 5
  • 69
  • 70