我正在尝试为我的iOS移动应用程序使用Algoliasearch实现单索引搜索。我的应用程序上大约有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分钟超时,您可以在配置函数时进行更改。
functions.runWith({timeoutSeconds: X}).https.onRequest(async (req, res)
如果函数最终没有发送响应,那么增加超时将没有帮助,因此您还应该添加一些日志记录/调试,以确定对res.send()
的最终调用是否真的发生了。如果函数从未发送响应,那么无论发生什么,它都肯定会超时。