-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
267 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}") |
29 changes: 29 additions & 0 deletions
29
templates/python/guides/search/saveObjectsModified.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
templates/python/guides/search/saveObjectsPublicUser.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}") |
26 changes: 26 additions & 0 deletions
26
templates/python/guides/search/savePopularRecords.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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="<YOUR_INDEX_NAME>", | ||
aggregator=_aggregator, | ||
) | ||
|
||
{{#dynamicSnippet}}saveObjectsRecords{{/dynamicSnippet}} | ||
except Exception as e: | ||
print(f"Error: {e}") |
18 changes: 18 additions & 0 deletions
18
templates/python/guides/search/searchRecentlyPublishedBooks.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import time | ||
|
||
{{> snippets/import}} | ||
|
||
|
||
if __name__ == '__main__': | ||
try: | ||
_{{> snippets/init}} | ||
|
||
date_timestamp = time.time() # Get current timestamp | ||
search_params = { | ||
"query": "<YOUR_SEARCH_QUERY>", | ||
"filters": f"date_timestamp > {date_timestamp}" | ||
} | ||
|
||
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}} | ||
except Exception as e: | ||
print(f"Error: {e}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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': '<YOUR_SEARCH_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}") |
23 changes: 23 additions & 0 deletions
23
templates/python/guides/search/searchWithOptionalFilters.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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="<YOUR_SEARCH_QUERY>", | ||
optional_filters=optional_filters, | ||
) | ||
|
||
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}} | ||
except Exception as e: | ||
print(f"Error: {e}") |
23 changes: 23 additions & 0 deletions
23
templates/python/guides/search/searchWithRuleContextBuyer.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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="<YOUR_SEARCH_QUERY>", | ||
rule_contexts=[buyer], | ||
) | ||
|
||
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}} | ||
except Exception as e: | ||
print(f"Error: {e}") |
22 changes: 22 additions & 0 deletions
22
templates/python/guides/search/searchWithRuleContexts.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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="<YOUR_SEARCH_QUERY>", | ||
rule_contexts=[platform_tag], | ||
) | ||
|
||
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}} | ||
except Exception as e: | ||
print(f"Error: {e}") |
14 changes: 14 additions & 0 deletions
14
templates/python/guides/search/setHeaderUserIDThenSaveObjects.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}") |
31 changes: 31 additions & 0 deletions
31
templates/python/guides/search/setSettingsThenSaveObjects.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}") |