【发布时间】:2020-08-26 19:36:25
【问题描述】:
我正在尝试使用 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 分钟超时时间?
-
也许,我不确定是不是我编写的函数效率太低,无法在超时发生之前上传所有数据。有没有办法配置超时来延长定时器,或者我应该分组上传用户数据? @DougStevenson
标签: javascript firebase google-cloud-firestore google-cloud-functions algolia