为什么我使用 API 脚本将数据导入 Algolia 搜索超时
Why is my data import to Algolia search using the API script timing out
我正在尝试使用 Algoliasearch 为我的 iOS 移动应用程序实施单一索引搜索。我的应用程序大约有 110 个用户。但是,当我将他们的数据上传到 Algolia 搜索的索引时,该功能在上传所有用户之前超时。相反,它会在 http 浏览器中抛出错误消息并在 Firestore 控制台中声明超时。
Firestore 控制台:
sendCollectionToAlgolia
Function execution took 60044 ms, finished with status: 'timeout'
我使用本教程创建了函数:
https://medium.com/@soares.rfarias/how-to-set-up-firestore-and-algolia-319fcf2c0d37
虽然我 运行 遇到了一些麻烦,但如果您的应用程序使用 swiftUI iOS 平台并使用 Typescript 实现云功能,我强烈推荐该教程。
这是我的函数:
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import algoliasearch from 'algoliasearch';
admin.initializeApp();
const db = admin.firestore();
const algoliaClient = algoliasearch(functions.config().algolia.appid, functions.config().algolia.apikey)
const collectionIndexName = functions.config().projectId === 'PROJECT-XXXX' ? 'prod_SEARCH' : 'dev_SEARCH';
const collectionIndex = algoliaClient.initIndex(collectionIndexName);
//rename to uploadUsersToAlgolia
export const sendCollectionToAlgolia = functions.https.onRequest(async (req, res) => {
const algoliaRecords: any[] = [];
const querySnapshot = await db.collection('users').get();
querySnapshot.docs.forEach(doc => {
const document = doc.data();
const record = {
objectID: doc.id,
fullname: document.fullname,
bio: document.bio,
username: document.username,
uid: document.uid,
profileImageURL: document.profileImageURL,
backgroundImageURL: document.backgroundImageURL,
fcmToken: document.fcmToken,
accountCreated: document.accountCreated,
inspirationCount: document.inspriationCount,
BucketListCount: document.BucketListCount,
CompletedBucketListCount: document.CompletedBucketListCount,
FriendsCount: document.FriendsCount
};
algoliaRecords.push(record);
});
// After all records are created, we save them to
collectionIndex.saveObjects(algoliaRecords, (_error: any, content: any) => {
res.status(200).send("users collection was indexed to Algolia successfully.");
});
});
如果您只想更改默认的 1 分钟超时,您可以在 configure the function 时执行此操作。
functions.runWith({timeoutSeconds: X}).https.onRequest(async (req, res)
如果您的函数最终没有发送响应,增加超时将无济于事,因此您还应该添加一些 logging/debugging 以确定对 res.send()
的最终调用是否实际上是发生。如果函数永远不会发送响应,那么无论发生什么情况都肯定会超时。
我正在尝试使用 Algoliasearch 为我的 iOS 移动应用程序实施单一索引搜索。我的应用程序大约有 110 个用户。但是,当我将他们的数据上传到 Algolia 搜索的索引时,该功能在上传所有用户之前超时。相反,它会在 http 浏览器中抛出错误消息并在 Firestore 控制台中声明超时。
Firestore 控制台:
sendCollectionToAlgolia
Function execution took 60044 ms, finished with status: 'timeout'
我使用本教程创建了函数: https://medium.com/@soares.rfarias/how-to-set-up-firestore-and-algolia-319fcf2c0d37
虽然我 运行 遇到了一些麻烦,但如果您的应用程序使用 swiftUI iOS 平台并使用 Typescript 实现云功能,我强烈推荐该教程。
这是我的函数:
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import algoliasearch from 'algoliasearch';
admin.initializeApp();
const db = admin.firestore();
const algoliaClient = algoliasearch(functions.config().algolia.appid, functions.config().algolia.apikey)
const collectionIndexName = functions.config().projectId === 'PROJECT-XXXX' ? 'prod_SEARCH' : 'dev_SEARCH';
const collectionIndex = algoliaClient.initIndex(collectionIndexName);
//rename to uploadUsersToAlgolia
export const sendCollectionToAlgolia = functions.https.onRequest(async (req, res) => {
const algoliaRecords: any[] = [];
const querySnapshot = await db.collection('users').get();
querySnapshot.docs.forEach(doc => {
const document = doc.data();
const record = {
objectID: doc.id,
fullname: document.fullname,
bio: document.bio,
username: document.username,
uid: document.uid,
profileImageURL: document.profileImageURL,
backgroundImageURL: document.backgroundImageURL,
fcmToken: document.fcmToken,
accountCreated: document.accountCreated,
inspirationCount: document.inspriationCount,
BucketListCount: document.BucketListCount,
CompletedBucketListCount: document.CompletedBucketListCount,
FriendsCount: document.FriendsCount
};
algoliaRecords.push(record);
});
// After all records are created, we save them to
collectionIndex.saveObjects(algoliaRecords, (_error: any, content: any) => {
res.status(200).send("users collection was indexed to Algolia successfully.");
});
});
如果您只想更改默认的 1 分钟超时,您可以在 configure the function 时执行此操作。
functions.runWith({timeoutSeconds: X}).https.onRequest(async (req, res)
如果您的函数最终没有发送响应,增加超时将无济于事,因此您还应该添加一些 logging/debugging 以确定对 res.send()
的最终调用是否实际上是发生。如果函数永远不会发送响应,那么无论发生什么情况都肯定会超时。