diff --git a/templates/python/guides/ingestion/pushSetup.mustache b/templates/python/guides/ingestion/pushSetup.mustache index a647314722..2379c62a0a 100644 --- a/templates/python/guides/ingestion/pushSetup.mustache +++ b/templates/python/guides/ingestion/pushSetup.mustache @@ -1,9 +1,8 @@ -import asyncio import json {{> snippets/import}} -async def main(): +if __name__ == '__main__': # use the region matching your applicationID _{{> snippets/init}} @@ -14,5 +13,3 @@ async def main(): resp = {{#dynamicSnippet}}pushSetup{{/dynamicSnippet}} print(resp) - -asyncio.run(main()) \ No newline at end of file diff --git a/templates/python/guides/search/deleteMultipleIndices.mustache b/templates/python/guides/search/deleteMultipleIndices.mustache index ce404624b6..157b73752b 100644 --- a/templates/python/guides/search/deleteMultipleIndices.mustache +++ b/templates/python/guides/search/deleteMultipleIndices.mustache @@ -1,8 +1,7 @@ -import asyncio {{> snippets/import}} -async def main(): +if __name__ == '__main__': try: # You need an API key with `deleteIndex` _{{> snippets/init}} @@ -29,4 +28,3 @@ async def main(): except Exception as e: print(f"Error: {e}") -asyncio.run(main()) diff --git a/templates/python/guides/search/saveObjectsChunks.mustache b/templates/python/guides/search/saveObjectsChunks.mustache new file mode 100644 index 0000000000..0fbba687a8 --- /dev/null +++ b/templates/python/guides/search/saveObjectsChunks.mustache @@ -0,0 +1,21 @@ +import json + +{{> snippets/import}} + + +if __name__ == '__main__': + try: + _{{> snippets/init}} + + with open("records.json", "r", encoding="utf-8") as f: + records = json.load(f) + + chunk_size = 10000 + + for begin_index in range(0, len(records), chunk_size): + chunk = records[begin_index:begin_index + chunk_size] + {{#dynamicSnippet}}saveObjectsChunks{{/dynamicSnippet}} + + except Exception as e: + print(f"Error: {e}") + diff --git a/templates/python/guides/search/saveObjectsMCM.mustache b/templates/python/guides/search/saveObjectsMCM.mustache new file mode 100644 index 0000000000..7771b40a0c --- /dev/null +++ b/templates/python/guides/search/saveObjectsMCM.mustache @@ -0,0 +1,19 @@ +{{> snippets/import}} + +def _get_all_app_id_configurations(): + return [] # A list of your MCM AppID/ApiKey pairs + +if __name__ == '__main__': + playlists = [] # Your records + + # Fetch from your own data storage and with your own code + # the list of application IDs and API keys to target each cluster + configurations = _get_all_app_id_configurations() + + # Send the records to each cluster + for appID, apiKey in configurations: + try: + _client = SearchClientSync(appID, apiKey) + {{#dynamicSnippet}}saveObjectsPlaylists{{/dynamicSnippet}} + except Exception as e: + print(f"Error: {e}") diff --git a/templates/python/guides/search/saveObjectsModified.mustache b/templates/python/guides/search/saveObjectsModified.mustache new file mode 100644 index 0000000000..dc5248be81 --- /dev/null +++ b/templates/python/guides/search/saveObjectsModified.mustache @@ -0,0 +1,29 @@ +import json + +{{> snippets/import}} + + +if __name__ == '__main__': + try: + _{{> snippets/init}} + + with open("products.json", "r", encoding="utf-8") as f: + products = json.load(f) + + records = [] + + for product in products: + reference = product['product_reference'] + + suffixes = [] + while len(reference) > 1: + reference = reference[1:] + suffixes.append(reference) + + product['product_reference_suffixes'] = suffixes + records.append(product) + + {{#dynamicSnippet}}saveObjectsRecords{{/dynamicSnippet}} + except Exception as e: + print(f"Error: {e}") + diff --git a/templates/python/guides/search/saveObjectsMovies.mustache b/templates/python/guides/search/saveObjectsMovies.mustache index 0b1dd1e84f..d135f76f66 100644 --- a/templates/python/guides/search/saveObjectsMovies.mustache +++ b/templates/python/guides/search/saveObjectsMovies.mustache @@ -1,9 +1,8 @@ -import asyncio import requests {{> snippets/import}} -async def main(): +if __name__ == '__main__': # Fetch sample dataset url = "https://dashboard.algolia.com/api/1/sample_datasets?type=movie" movies = requests.get(url).json() @@ -13,5 +12,3 @@ async def main(): # Save records in Algolia index {{#dynamicSnippet}}saveObjectsMovies{{/dynamicSnippet}} - -asyncio.run(main()) \ No newline at end of file diff --git a/templates/python/guides/search/saveObjectsPublicUser.mustache b/templates/python/guides/search/saveObjectsPublicUser.mustache new file mode 100644 index 0000000000..fb1a621e95 --- /dev/null +++ b/templates/python/guides/search/saveObjectsPublicUser.mustache @@ -0,0 +1,12 @@ +{{> snippets/import}} + + +if __name__ == '__main__': + playlists = [] # Your records + + try: + _{{> snippets/init}} + + {{#dynamicSnippet}}saveObjectsPlaylistsWithUserIDPublic{{/dynamicSnippet}} + except Exception as e: + print(f"Error: {e}") diff --git a/templates/python/guides/search/savePopularRecords.mustache b/templates/python/guides/search/savePopularRecords.mustache new file mode 100644 index 0000000000..ed7f24cbc4 --- /dev/null +++ b/templates/python/guides/search/savePopularRecords.mustache @@ -0,0 +1,26 @@ +{{> snippets/import}} +from algoliasearch.search.models.browse_response import BrowseResponse + + +if __name__ == '__main__': + try: + _{{> snippets/init}} + + records = [] + def _aggregator(resp: BrowseResponse): + for hit in resp.hits: + hit_dict = hit.to_dict() + records.append({ + 'twitterHandle': hit_dict['twitterHandle'], + 'nbFollowers': hit_dict['nbFollowers'], + 'isPopular': hit_dict['nbFollowers'] > 1000000, + }) + + _client.browse_objects( + index_name="", + aggregator=_aggregator, + ) + + {{#dynamicSnippet}}saveObjectsRecords{{/dynamicSnippet}} + except Exception as e: + print(f"Error: {e}") diff --git a/templates/python/guides/search/searchRecentlyPublishedBooks.mustache b/templates/python/guides/search/searchRecentlyPublishedBooks.mustache new file mode 100644 index 0000000000..f31798730d --- /dev/null +++ b/templates/python/guides/search/searchRecentlyPublishedBooks.mustache @@ -0,0 +1,18 @@ +import time + +{{> snippets/import}} + + +if __name__ == '__main__': + try: + _{{> snippets/init}} + + date_timestamp = time.time() # Get current timestamp + search_params = { + "query": "", + "filters": f"date_timestamp > {date_timestamp}" + } + + {{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}} + except Exception as e: + print(f"Error: {e}") diff --git a/templates/python/guides/search/searchWithGAToken.mustache b/templates/python/guides/search/searchWithGAToken.mustache new file mode 100644 index 0000000000..03d221c3d9 --- /dev/null +++ b/templates/python/guides/search/searchWithGAToken.mustache @@ -0,0 +1,26 @@ +{{> snippets/import}} + + +def _get_google_analytics_user_id_from_browser_cookie(cookie_name: str) -> str: + # Implement your logic here + return '' + +if __name__ == '__main__': + try: + _{{> snippets/init}} + + user_token = _get_google_analytics_user_id_from_browser_cookie('_ga') + search_params = { + 'query': '', + 'userToken': user_token, + } + + {{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}} + + logged_in_user = None + + search_params['user_token'] = logged_in_user or user_token + + {{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}} + except Exception as e: + print(f"Error: {e}") diff --git a/templates/python/guides/search/searchWithOptionalFilters.mustache b/templates/python/guides/search/searchWithOptionalFilters.mustache new file mode 100644 index 0000000000..addd20ad02 --- /dev/null +++ b/templates/python/guides/search/searchWithOptionalFilters.mustache @@ -0,0 +1,23 @@ +{{> snippets/import}} +from algoliasearch.search.models.search_params import SearchParams + + +def _reduce_labels_to_filters(_labels): + # Implement your logic here + return [] + +if __name__ == '__main__': + labels = [] # A list of labels + + try: + _{{> snippets/init}} + + optional_filters = _reduce_labels_to_filters(labels) + search_params = SearchParams( + query="", + optional_filters=optional_filters, + ) + + {{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}} + except Exception as e: + print(f"Error: {e}") diff --git a/templates/python/guides/search/searchWithRuleContextBuyer.mustache b/templates/python/guides/search/searchWithRuleContextBuyer.mustache new file mode 100644 index 0000000000..182d0104eb --- /dev/null +++ b/templates/python/guides/search/searchWithRuleContextBuyer.mustache @@ -0,0 +1,23 @@ +{{> snippets/import}} +from algoliasearch.search.models.search_params import SearchParams + + +def _get_buyer_account_id() -> str: + # Implement your logic here + return '' + +if __name__ == '__main__': + + try: + _{{> snippets/init}} + + # get the buyer account information + buyer = _get_buyer_account_id() + search_params = SearchParams( + query="", + rule_contexts=[buyer], + ) + + {{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}} + except Exception as e: + print(f"Error: {e}") diff --git a/templates/python/guides/search/searchWithRuleContexts.mustache b/templates/python/guides/search/searchWithRuleContexts.mustache new file mode 100644 index 0000000000..68b43c4202 --- /dev/null +++ b/templates/python/guides/search/searchWithRuleContexts.mustache @@ -0,0 +1,22 @@ +{{> snippets/import}} +from algoliasearch.search.models.search_params import SearchParams + + +def _get_platform_tag() -> str: + # Implement your logic here + return '' + +if __name__ == '__main__': + try: + _{{> snippets/init}} + + # get the buyer account information + platform_tag = _get_platform_tag() + search_params = SearchParams( + query="", + rule_contexts=[platform_tag], + ) + + {{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}} + except Exception as e: + print(f"Error: {e}") diff --git a/templates/python/guides/search/setHeaderUserIDThenSaveObjects.mustache b/templates/python/guides/search/setHeaderUserIDThenSaveObjects.mustache new file mode 100644 index 0000000000..29fdf0169b --- /dev/null +++ b/templates/python/guides/search/setHeaderUserIDThenSaveObjects.mustache @@ -0,0 +1,14 @@ +{{> snippets/import}} + + +if __name__ == '__main__': + playlists = [] # Your records + + try: + _{{> snippets/init}} + + for playlist in playlists: + playlist_user_id = playlist['userID'] + {{#dynamicSnippet}}saveObjectsPlaylistsWithRequestOptions{{/dynamicSnippet}} + except Exception as e: + print(f"Error: {e}") diff --git a/templates/python/guides/search/setSettingsThenSaveObjects.mustache b/templates/python/guides/search/setSettingsThenSaveObjects.mustache new file mode 100644 index 0000000000..20f9d665ee --- /dev/null +++ b/templates/python/guides/search/setSettingsThenSaveObjects.mustache @@ -0,0 +1,31 @@ +{{> snippets/import}} +from algoliasearch.search.models.index_settings import IndexSettings + + +def _get_app_id_for(_user): + # Implement your own logic here + return "" + +def _get_indexing_api_key_for(_user): + # Implement your own logic here + return "" + +if __name__ == '__main__': + playlists = [] # Your records + + try: + _{{> snippets/init}} + + for playlist in playlists: + app_id = _get_app_id_for(playlist['user']) + api_key = _get_indexing_api_key_for(playlist['user']) + + _client = SearchClientSync(app_id, api_key) + settings = IndexSettings( + attributes_for_faceting=['filterOnly(user)'] + ) + {{#dynamicSnippet}}setSettings{{/dynamicSnippet}} + + {{#dynamicSnippet}}saveObjectsPlaylists{{/dynamicSnippet}} + except Exception as e: + print(f"Error: {e}")