Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug]: Cannot wait for objects to finish indexing after save_objects() #574

Closed
lappemic opened this issue Oct 25, 2024 · 2 comments · Fixed by algolia/api-clients-automation#4030
Assignees

Comments

@lappemic
Copy link

Description

When using save_objects() to add items to an index, I can't find a way to wait for the indexing to complete. I tried:

  • Using .wait()
  • Using wait_for_task()

But the neither of them worked.

Example code

from algoliasearch.search.client import SearchClientSync

algolia_client = SearchClientSync(ALGOLIA_APP_ID, ALGOLIA_ADMIN_KEY)

# Upload items to Algolia index
save_resp = algolia_client.save_objects(ALGOLIA_INDEX_NAME, new_items) # also tried .wait() here

# Tried this approach but getting error that wait_for_task() does not exist
algolia_client.wait_for_task(
    index_name=ALGOLIA_INDEX_NAME,
    task_id=save_resp.task_id
)```

## Expected behavior
- After calling `wait()` or `wait_for_task()`, the program should await until the indexing is finished
- There should be a clear way to wait for indexing to complete

## Questions
1. What is the correct way to wait for objects to be fully indexed?
2. Is there any documentation about this? The ones i found were not helpful but rather a bit confusing.

### Client

Crawler

### Version

4.6.5

### Relevant log output

_No response_
@shortcuts
Copy link
Member

shortcuts commented Oct 25, 2024

Hey there, we have actually an option internally to wait in those helpers, it will be exposed after this PR algolia/api-clients-automation#4030

In the meantime, the below snippet to wait after a save_objects call should work with the latest version

from os import environ

from algoliasearch.search.client import SearchClientSync
from algoliasearch.search import __version__


def main():
    print("SearchClient version", __version__)

    client = SearchClientSync(environ.get("ALGOLIA_APPLICATION_ID"), environ.get("ALGOLIA_API_KEY"))
    print("client initialized", client)

    try:
        resp = client.save_objects("foo", [{"foo": "bar"}])
        print(resp)

        for r in resp:
            client.wait_for_task(index_name="foo", task_id=r.task_id)
    finally:
        client.close()

        print("client closed")


main()

Notice that save_objects returns a list of responses, since we chunk the given objects to make sure it fits in the request made to the API

@lappemic
Copy link
Author

Well, solutions not always need to be hard. 😄 Perfect, thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants