Writing unit/integration tests for your search code ? If this includes creating an index and adding some data for your tests, the wait_for_status flag might be useful. The API looks like this:
This is a blocking call and will wait till the timeout provided till the status changes. This will definitely be more helpful than sleeping for a while and will help in writing more stable tests.