The create document, as you said, execute the call to create the view but does not block, or check that the view is created, also once the view is created, you have to let the system do the indexing of the document.
First of all, this API has not been build to create "view on the fly" to mimic adhoc queries (because of the behavior describe above) but mainly to be able to deploy views automatically when you deploy an application or new version of the application.
That said it is possible to work around this with simple code:
- when you create the view, the "createDesignDoc()" method, you can check just after if the view is created in your code and "loop/wait" until it is created. (until the "Failed to access the view" exception is not thrown anymore - check that you do not end with an infinite loop in case of real exception)
- once the view is created, the server needs to index "all" existing documents this takes some time depending of the size of your dataset. This is why your view does not return anything first. So when you query you index just be sure you do a stale=false like that the index will be updated before returning the result to your application. Note that with stale, you application will have to way.
Hope that helps!