azure-sdk-for-python icon indicating copy to clipboard operation
azure-sdk-for-python copied to clipboard

Need some wait when use SearchClient for actual count

Open nobuko-yamada opened this issue 1 year ago • 2 comments

Is your feature request related to a problem? Please describe. When we delete or upload the documents, the actual count is some delay. So we need to wait like this

time.sleep(2)

Image

## import 

import time
import asyncio
from azure.core.credentials import AzureKeyCredential
from azure.search.documents import SearchClient

search_endpoint: str = "https://<search>.search.windows.net"
search_api_key: str = "<apikey>"
index_name: str = "<indexname>"

from azure.core.credentials import AzureKeyCredential

# Create a client
credential = AzureKeyCredential(search_api_key)
client = SearchClient(endpoint=search_endpoint,
                      index_name=index_name,
                      credential=credential)

count = 10000 # count 10000 

async def delete():
    for i in range(0, count):
        result = client.delete_documents(documents=[{"id": str(i)}])
        ## print("Delete new document succeeded: {}".format(result[0].succeeded))
        client.get_document_count()
        ## print("Current Document count: {}".format(client.get_document_count()))


async def main():
    await delete()
    print("*** Deleted")

if __name__ == "__main__":
    asyncio.run(main())

elapsed = 0
while True:
    remaining = client.get_document_count()
    print("Current document count: {}".format(remaining))

    if remaining == 0: 
        break
    time.sleep(2)
    elapsed += 2
    if elapsed >= 600:
        raise TimeoutError(f"abort")

print("*** completed")

Describe the solution you'd like It wanted to be explained in documents at least.

Describe alternatives you've considered Implement as above.

Additional context N/A

nobuko-yamada avatar Apr 22 '25 08:04 nobuko-yamada

@nobuko-yamada thanks for your feedback, can you share which version of azure-search-documents you are seeing this with?

kristapratico avatar Apr 22 '25 16:04 kristapratico

@kristapratico thank you very much for the checking.

The latest one. https://learn.microsoft.com/en-us/python/api/azure-search-documents/azure.search.documents.searchclient?view=azure-python#azure-search-documents-searchclient-delete-documents https://learn.microsoft.com/en-us/python/api/azure-search-documents/azure.search.documents.searchclient?view=azure-python#azure-search-documents-searchclient-get-document-count

nobuko-yamada avatar Apr 22 '25 21:04 nobuko-yamada