gpt4 book ai didi

node.js - azure 的阿尔戈利亚

转载 作者:太空宇宙 更新时间:2023-11-03 22:50:42 25 4
gpt4 key购买 nike

我正在为我的 Android 应用程序使用 firebase,并使用所有博客建议的 Algolia 执行全文搜索。我已经成功开发了该脚本并且其功能正常。现在我想托管脚本以 24* 运行 7. 由于我有一个 azure 帐户,我如何上传脚本?我尝试将以下内容作为函数、网络应用程序上传,但没有成功。

PS:- 我尝试过 Heroku 但不满意。

脚本。

var http = require('http');
var port = process.env.port || 1337;
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello World\n');
}).listen(port);
var dotenv = require('dotenv');
var firebaseAdmin = require("firebase-admin");
var algoliasearch = require('algoliasearch');
var algoliasearchHelper = require('algoliasearch-helper');


// load values from the .env file in this directory into process.env
dotenv.load();

// configure firebase
var serviceAccount = require("./serviceAccountKey.json");
firebaseAdmin.initializeApp({
credential: firebaseAdmin.credential.cert(serviceAccount),
databaseURL: process.env.FIREBASE_DATABASE_URL
});
var database = firebaseAdmin.database();

// configure algolia
var algolia = algoliasearch(process.env.ALGOLIA_APP_ID, process.env.ALGOLIA_API_KEY);
var index = algolia.initIndex('books');

var contactsRef = database.ref("/BookFair");

contactsRef.on('child_added', addOrUpdateIndexRecord);
contactsRef.on('child_changed', addOrUpdateIndexRecord);
contactsRef.on('child_removed', deleteIndexRecord);

function addOrUpdateIndexRecord(dataSnapshot) {
// Get Firebase object
var firebaseObject = dataSnapshot.val();
// Specify Algolia's objectID using the Firebase object key
firebaseObject.objectID = dataSnapshot.key;
// Add or update object
index.saveObject(firebaseObject, function(err, content) {
if (err) {
throw err;
}
console.log('Firebase object indexed in Algolia', firebaseObject.objectID);
});
}

function deleteIndexRecord(dataSnapshot) {
// Get Algolia's objectID from the Firebase object key
var objectID = dataSnapshot.key;
// Remove the object from Algolia
index.deleteObject(objectID, function(err, content) {
if (err) {
throw err;
}
console.log('Firebase object deleted from Algolia', objectID);
});
}

var queries = database.ref("/queries");

queries.on('child_added', addOrUpdateIndexRecordN);

function addOrUpdateIndexRecordN(dataSnapshot) {
// Get Firebase object
var firebaseObject = dataSnapshot.val();
// Specify Algolia's objectID using the Firebase object key
firebaseObject.objectID = dataSnapshot.key;
// Add or update object

var collegeName = "";
var query_ID_LOLWA= "";
var year="";
var query = "";
var counter = 0;

for(var i in firebaseObject){
var c = firebaseObject.charAt(i);
if(c=='/'){
counter = counter + 1;
continue;
}
else{

if(counter==2)
collegeName = collegeName + c;
else if(counter == 3)
year = year+c;
else if(counter == 1)
query_ID_LOLWA = query_ID_LOLWA + c;
else
query = query +c;

}

}
console.log(collegeName);
console.log(year);
console.log(query_ID_LOLWA);
console.log(query);

const query_final = query_ID_LOLWA;

var helper = algoliasearchHelper(algoliasearch("****", "****"), 'books', {
facets: ['collegeName', 'year','priority']});


helper.on('result', function(data,query_ID_LOLWA){


data.getFacetValues('priority',{sortBy: ['count:desc']});

console.log(data.hits);

var path_query = "/queries_answers/"+query_final;

path_query = path_query.toString();
console.log(path_query);
if(data.hits.length==0){
console.log("No results");
database.ref(path_query).push(-1);
}

else if(data.hits.length>1){

var ID = 1;
var counter = -1;
var length = data.hits.length-1;
for(var h in data.hits){
counter = counter + 1;
if( (counter%5 == 0) && (counter != 0)){
ID = ID + 1;
}
database.ref(path_query+"/"+ID).push(data.hits[h].uuid);

}
database.ref(path_query+"/totalResults").push(data.hits.length);
}
else{
database.ref(path_query+"/totalResults").push(data.hits.length);
for(var h in data.hits)
database.ref(path_query+"/1").push(data.hits[h].uuid);
}

});

helper.addFacetRefinement('collegeName', collegeName);
helper.addFacetRefinement('year',year);

helper.setQuery(query);



helper.search();



/*index.search(firebaseObject, function(err, content) {
if (err) {
console.error(err);
return;
}
console.log(content.hits);

for (var h in content.hits) {

console.log('Hit(' + content.hits[h].objectID + '): ' + content.hits[h].uuid);
}

database.ref("/query_result").push(content.hits);
});*/




}

最佳答案

没有比更多的详细信息,但没有成功,我们可以给您的唯一建议是按照常规步骤部署基于时间的 Azure 函数。

最简单的方法是使用 Azure Portal :

  • Login to your Microsoft Azure account
  • Create a Function App to host your function
  • Add a Timer-triggered Function
  • Select the TimerTrigger-Javascript template to get started

此时,您将拥有一个每分钟运行一次的函数。您可以检查日志以确认其正常工作。您现在想要配置其频率:

  • Update the function's Timer Schedule (in the Integrate tab) to set how frequently the function should run
  • Finally, replace the template's code with your own.

您可以找到detailed tutorial here 并解释如何实现每个步骤。

关于node.js - azure 的阿尔戈利亚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46003231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com