Upgrade the Python API clients to v4
The latest major version of the algoliasearch
package is v4.
This page lists the breaking changes since the last release, v3.
Client imports
The imports for the API clients changed.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Search API
from algoliasearch.search.client import SearchClientSync
# Search API (async)
from algoliasearch.search.client import SearchClient
# Recommend API
from algoliasearch.recommend.client import RecommendClientSync
# Recommend API (async)
from algoliasearch.recommend.client import RecommendClient
# A/B testing
from algoliasearch.abtesting.client import AbtestingClientSync
# A/B testing (async)
from algoliasearch.abtesting.client import AbtestingClient
# Analytics API
from algoliasearch.analytics.client import AnalyticsClientSync
# Analytics API (async)
from algoliasearch.analytics.client import AnalyticsClient
# Ingestion API
from algoliasearch.ingestion.client import IngestionClientSync
# Ingestion API (async)
from algoliasearch.ingestion.client import IngestionClient
# Insights API
from algoliasearch.insights.client import InsightsClientSync
# Insights API (async)
from algoliasearch.insights.client import InsightsClient
# Monitoring API
from algoliaserarch.monitoring.client import MonitoringClientSync
# Monitoring API (async)
from algoliaserarch.monitoring.client import MonitoringClient
# Personalization API
from algoliasearch.personalization.client import PersonalizationClientSync
# Personalization API (async)
from algoliasearch.personalization.client import PersonalizationClient
# Query Suggestions API
from algoliasearch.query_suggestions.client import QuerySuggestionsClientSync
# Query Suggestions API (async)
from algoliasearch.query_suggestions.client import QuerySuggestionsClient
Client creation
To create a client, create an instance of the SearchClientSync
class.
In async environment, create an instance of the SearchClient
class instead.
The SearchClient.create()
method has been removed.
The async API clients support the
async with
syntax to automatically close open connections.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from algoliasearch.search.client import SearchClientSync
from algoliasearch.search.client import SearchClient # async client
from algoliasearch.search.config import SearchConfig
app_id = "ALGOLIA_APPLICATION_ID"
api_key = "ALGOLIA_API_KEY"
# With positional arguments
client = SearchClientSync(app_id, api_key)
# With keyword arguments
client = SearchClientSync(app_id=app_id, api_key=api_key)
# With a custom configuration
client = SearchClientSync(
app_id=app_id,
api_key=api_key,
config=SearchConfig()
)
# async
async with SearchClient(app_id=app_id, api_key=api_key) as client:
...
Removal of init_index
All methods are methods of a client
instance.
The init_index
method of the SearchClient
has been removed.
Instead, all methods require a index_name
parameter.
1
2
3
4
5
6
7
8
# v3
client = SearchClient.create("ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY")
index = client.init_index("ALGOLIA_INDEX_NAME")
index.search("QUERY")
# v4
client = SearchClientSync("ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY")
client.search_single_index("ALGOLIA_INDEX_NAME", {"query": "QUERY"})
Wait for tasks
The wait
method has been removed.
Instead, use one of the following helpers:
wait_for_task
to wait until indexing operations are donewait_for_app_task
to wait for application-level taskswait_for_api_key
to wait for API key operations
Copy or move indices, settings, synonyms, or rules
Use the operation_index
method,
which replaces the following methods:
copy_index
move_index
copy_rules
copy_synonyms
copy_settings