Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tuxido-feynman/2f34b16be87a7ed6a68d2e54ef9ab8f5 to your computer and use it in GitHub Desktop.
Save tuxido-feynman/2f34b16be87a7ed6a68d2e54ef9ab8f5 to your computer and use it in GitHub Desktop.
Transform Shopify string json fields for Algolia
import json
from algoliasearch import algoliasearch
APP_ID = 'MY_APP_ID'
API_ETL_KEY = 'API_KEY_WITH_BROWSE_AND_WRITE_ACCESS'
BROWSE_INDEX_NAME = 'MY_SHOPIFY_INDEX_NAME'
WRITE_INDEX_NAME = 'MY_NEW_SHOPIFY_INDEX_NAME'
BATCH_SIZE = 10
LOCALE = 'en_CA'
def extract(browse_index):
res = browse_index.browse_all()
for hit in res:
yield hit
def transform(hits):
for hit in hits:
specs = json.loads(hit['meta']['props']['alogolia'])
hit['specs'] = specs[LOCALE]
del(hit['meta'])
yield hit
def reload(write_index, hits):
batch = []
for hit in hits:
if len(batch) < BATCH_SIZE:
batch.append(hit)
else:
write_index.save_objects(batch)
batch = []
write_index.save_objects(batch)
if __name__ == '__main__':
browse_client = algoliasearch.Client(APP_ID, API_ETL_KEY)
browse_index = browse_client.init_index(BROWSE_INDEX_NAME)
write_client = algoliasearch.Client(APP_ID, API_ETL_KEY)
write_index = write_client.init_index(WRITE_INDEX_NAME)
reload(
write_index,
transform(
extract(browse_index)
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment