Skip to content

Commit fb15f1e

Browse files
committed
feat(python): more guides
1 parent fa3acd1 commit fb15f1e

19 files changed

+131
-32
lines changed

templates/python/guides/search/deleteMultipleIndices.mustache

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{{> snippets/import}}
22

33

4-
if __name__ == '__main__':
4+
if __name__ == "__main__":
55
try:
66
# You need an API key with `deleteIndex`
77
_{{> snippets/init}}
@@ -15,13 +15,13 @@ if __name__ == '__main__':
1515

1616
# Delete primary indices first
1717
if primary_indices:
18-
requests = [{'action': 'delete', 'indexName': index.name} for index in primary_indices]
18+
requests = [{"action": "delete", "indexName": index.name} for index in primary_indices]
1919
{{#dynamicSnippet}}deleteMultipleIndicesPrimary{{/dynamicSnippet}}
2020
print("Deleted primary indices.")
2121

2222
# Now, delete replica indices
2323
if replica_indices:
24-
requests = [{'action': 'delete', 'indexName': index.name} for index in replica_indices]
24+
requests = [{"action": "delete", "indexName": index.name} for index in replica_indices]
2525
{{#dynamicSnippet}}deleteMultipleIndicesReplica{{/dynamicSnippet}}
2626
print("Deleted replica indices.\n")
2727

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from algoliasearch.search.models.anchoring import Anchoring
2+
from algoliasearch.search.models.condition import Condition
3+
from algoliasearch.search.models.consequence import Consequence
4+
from algoliasearch.search.models.rule import Rule
5+
6+
7+
if __name__ == "__main__":
8+
condition = Condition(
9+
anchoring=Anchoring.IS,
10+
pattern="{facet:brand}",
11+
)
12+
13+
consequence = Consequence(
14+
filter_promotes=True,
15+
)
16+
17+
rule = Rule(
18+
enabled=True,
19+
object_id="rule_with_filterPromotes",
20+
conditions=[condition],
21+
consequence=consequence,
22+
)

templates/python/guides/search/saveObjectsChunks.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import json
33
{{> snippets/import}}
44

55

6-
if __name__ == '__main__':
6+
if __name__ == "__main__":
77
try:
88
_{{> snippets/init}}
99

templates/python/guides/search/saveObjectsMCM.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
def _get_all_app_id_configurations():
44
return [] # A list of your MCM AppID/ApiKey pairs
55

6-
if __name__ == '__main__':
6+
if __name__ == "__main__":
77
playlists = [] # Your records
88

99
# Fetch from your own data storage and with your own code

templates/python/guides/search/saveObjectsModified.mustache

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import json
33
{{> snippets/import}}
44

55

6-
if __name__ == '__main__':
6+
if __name__ == "__main__":
77
try:
88
_{{> snippets/init}}
99

@@ -13,14 +13,14 @@ if __name__ == '__main__':
1313
records = []
1414

1515
for product in products:
16-
reference = product['product_reference']
16+
reference = product["product_reference"]
1717

1818
suffixes = []
1919
while len(reference) > 1:
2020
reference = reference[1:]
2121
suffixes.append(reference)
2222

23-
product['product_reference_suffixes'] = suffixes
23+
product["product_reference_suffixes"] = suffixes
2424
records.append(product)
2525

2626
{{#dynamicSnippet}}saveObjectsRecords{{/dynamicSnippet}}

templates/python/guides/search/saveObjectsMovies.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import requests
22
{{> snippets/import}}
33

44

5-
if __name__ == '__main__':
5+
if __name__ == "__main__":
66
# Fetch sample dataset
77
url = "https://dashboard.algolia.com/api/1/sample_datasets?type=movie"
88
movies = requests.get(url).json()

templates/python/guides/search/saveObjectsPublicUser.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{{> snippets/import}}
22

33

4-
if __name__ == '__main__':
4+
if __name__ == "__main__":
55
playlists = [] # Your records
66

77
try:

templates/python/guides/search/savePopularRecords.mustache

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from algoliasearch.search.models.browse_response import BrowseResponse
33

44

5-
if __name__ == '__main__':
5+
if __name__ == "__main__":
66
try:
77
_{{> snippets/init}}
88

@@ -11,9 +11,9 @@ if __name__ == '__main__':
1111
for hit in resp.hits:
1212
hit_dict = hit.to_dict()
1313
records.append({
14-
'twitterHandle': hit_dict['twitterHandle'],
15-
'nbFollowers': hit_dict['nbFollowers'],
16-
'isPopular': hit_dict['nbFollowers'] > 1000000,
14+
"twitterHandle": hit_dict["twitterHandle"],
15+
"nbFollowers": hit_dict["nbFollowers"],
16+
"isPopular": hit_dict["nbFollowers"] > 1000000,
1717
})
1818

1919
_client.browse_objects(
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{{> snippets/import}}
2+
3+
4+
if __name__ == "__main__":
5+
_{{> snippets/init}}
6+
7+
query = "query"
8+
9+
# 1. Change the sort dynamically based on the UI events
10+
sort_by_price = False
11+
12+
# 2. Get the index name based on sortByPrice
13+
index_name = "products_price_desc" if sort_by_price else "products"
14+
15+
# 3. Search on dynamic index name (primary or replica)
16+
{{#dynamicSnippet}}searchWithIndexNameVar{{/dynamicSnippet}}

templates/python/guides/search/searchRecentlyPublishedBooks.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import time
33
{{> snippets/import}}
44

55

6-
if __name__ == '__main__':
6+
if __name__ == "__main__":
77
try:
88
_{{> snippets/init}}
99

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{{> snippets/import}}
2+
from algoliasearch.search.models.search_params import SearchParams
3+
4+
5+
if __name__ == "__main__":
6+
_{{> snippets/init}}
7+
8+
"""
9+
'94.228.178.246' should be replaced with your user's IP address.
10+
Depending on your stack there are multiple ways to get this information.
11+
"""
12+
ip = "94.228.178.246"
13+
query = "query"
14+
15+
search_params = SearchParams(
16+
query=query,
17+
analytics=True,
18+
)
19+
20+
{{#dynamicSnippet}}searchWithSearchParamsAndForwardedHeader{{/dynamicSnippet}}
21+

templates/python/guides/search/searchWithGAToken.mustache

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@
33

44
def _get_google_analytics_user_id_from_browser_cookie(cookie_name: str) -> str:
55
# Implement your logic here
6-
return ''
6+
return ""
77

8-
if __name__ == '__main__':
8+
if __name__ == "__main__":
99
try:
1010
_{{> snippets/init}}
1111

12-
user_token = _get_google_analytics_user_id_from_browser_cookie('_ga')
12+
user_token = _get_google_analytics_user_id_from_browser_cookie("_ga")
1313
search_params = {
14-
'query': '<YOUR_SEARCH_QUERY>',
15-
'userToken': user_token,
14+
"query": "<YOUR_SEARCH_QUERY>",
15+
"userToken": user_token,
1616
}
1717

1818
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}}
1919

2020
logged_in_user = None
2121

22-
search_params['user_token'] = logged_in_user or user_token
22+
search_params["user_token"] = logged_in_user or user_token
2323

2424
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}}
2525
except Exception as e:
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{{> snippets/import}}
2+
from algoliasearch.search.models.search_params import SearchParams
3+
4+
5+
if __name__ == "__main__":
6+
_{{> snippets/init}}
7+
8+
query = "the query"
9+
optional_words = ["the", "query"]
10+
search_params = SearchParams(
11+
query=query,
12+
optional_words=optional_words,
13+
)
14+
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}}

templates/python/guides/search/searchWithOptionalFilters.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def _reduce_labels_to_filters(_labels):
66
# Implement your logic here
77
return []
88

9-
if __name__ == '__main__':
9+
if __name__ == "__main__":
1010
labels = [] # A list of labels
1111

1212
try:

templates/python/guides/search/searchWithRuleContextBuyer.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ from algoliasearch.search.models.search_params import SearchParams
44

55
def _get_buyer_account_id() -> str:
66
# Implement your logic here
7-
return ''
7+
return ""
88

9-
if __name__ == '__main__':
9+
if __name__ == "__main__":
1010

1111
try:
1212
_{{> snippets/init}}

templates/python/guides/search/searchWithRuleContexts.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ from algoliasearch.search.models.search_params import SearchParams
44

55
def _get_platform_tag() -> str:
66
# Implement your logic here
7-
return ''
7+
return ""
88

9-
if __name__ == '__main__':
9+
if __name__ == "__main__":
1010
try:
1111
_{{> snippets/init}}
1212

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{{> snippets/import}}
22

33

4-
if __name__ == '__main__':
4+
if __name__ == "__main__":
55
playlists = [] # Your records
66

77
try:
88
_{{> snippets/init}}
99

1010
for playlist in playlists:
11-
playlist_user_id = playlist['userID']
11+
playlist_user_id = playlist["userID"]
1212
{{#dynamicSnippet}}saveObjectsPlaylistsWithRequestOptions{{/dynamicSnippet}}
1313
except Exception as e:
1414
print(f"Error: {e}")

templates/python/guides/search/setSettingsThenSaveObjects.mustache

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ def _get_indexing_api_key_for(_user):
1010
# Implement your own logic here
1111
return ""
1212

13-
if __name__ == '__main__':
13+
if __name__ == "__main__":
1414
playlists = [] # Your records
1515

1616
try:
1717
for playlist in playlists:
18-
app_id = _get_app_id_for(playlist['user'])
19-
api_key = _get_indexing_api_key_for(playlist['user'])
18+
app_id = _get_app_id_for(playlist["user"])
19+
api_key = _get_indexing_api_key_for(playlist["user"])
2020

2121
_client = SearchClientSync(app_id, api_key)
2222
settings = IndexSettings(
23-
attributes_for_faceting=['filterOnly(user)']
23+
attributes_for_faceting=["filterOnly(user)"]
2424
)
2525
{{#dynamicSnippet}}setSettings{{/dynamicSnippet}}
2626

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{{> snippets/import}}
2+
from algoliasearch.search.models.consequence import Consequence
3+
from algoliasearch.search.models.rule import Rule
4+
from algoliasearch.search.models.time_range import TimeRange
5+
6+
if __name__ == "__main__":
7+
_{{> snippets/init}}
8+
9+
object_id = "a-rule-id"
10+
11+
rule = Rule(
12+
object_id=object_id,
13+
consequence=Consequence(
14+
# Set relevant consequence
15+
),
16+
)
17+
18+
# Set validity (optional)
19+
rule.validity = [
20+
TimeRange(
21+
var_from=1688774400,
22+
until=1738972800,
23+
),
24+
]
25+
26+
{{#dynamicSnippet}}saveRule{{/dynamicSnippet}}

0 commit comments

Comments
 (0)