feat: algolia settings in .env

This commit is contained in:
Vyacheslav Matyukhin 2022-03-30 19:52:43 +03:00
parent 0c9e577319
commit 49d8140784
No known key found for this signature in database
GPG Key ID: 3D2A774C5489F96C
2 changed files with 18 additions and 11 deletions

View File

@ -4,13 +4,15 @@ import { databaseReadWithReadCredentials } from "../database/database-wrapper";
import { mergeEverythingInner } from "../flow/mergeEverything"; import { mergeEverythingInner } from "../flow/mergeEverything";
let cookie = process.env.ALGOLIA_MASTER_API_KEY; let cookie = process.env.ALGOLIA_MASTER_API_KEY;
const client = algoliasearch("96UD3NTQ7L", cookie); const algoliaAppId = process.env.NEXT_PUBLIC_ALGOLIA_APP_ID;
const client = algoliasearch(algoliaAppId, cookie);
console.log(`Initializing algolia index for ${algoliaAppId}`);
const index = client.initIndex("metaforecast"); const index = client.initIndex("metaforecast");
export async function rebuildAlgoliaDatabaseTheHardWay() { export async function rebuildAlgoliaDatabaseTheHardWay() {
console.log("Doing this the hard way"); console.log("Doing this the hard way");
let records = await mergeEverythingInner(); let records = await mergeEverythingInner();
records = records.map((record, index) => ({ records = records.map((record, index: number) => ({
...record, ...record,
has_numforecasts: record.numforecasts ? true : false, has_numforecasts: record.numforecasts ? true : false,
objectID: index, objectID: index,
@ -28,21 +30,23 @@ export async function rebuildAlgoliaDatabaseTheHardWay() {
} }
} }
let getoptionsstringforsearch = (record) => { let getoptionsstringforsearch = (record: any) => {
let result = ""; let result = "";
if (!!record.options && record.options.length > 0) { if (!!record.options && record.options.length > 0) {
result = record.options result = record.options
.map((option) => option.name || null) .map((option: any) => option.name || null)
.filter((x) => x != null) .filter((x: any) => x != null)
.join(", "); .join(", ");
} }
return result; return result;
}; };
export async function rebuildAlgoliaDatabaseTheEasyWay() { export async function rebuildAlgoliaDatabaseTheEasyWay() {
let records = await databaseReadWithReadCredentials({ group: "combined" }); let records: any[] = await databaseReadWithReadCredentials({
group: "combined",
});
records = records.map((record, index) => ({ records = records.map((record, index: number) => ({
...record, ...record,
has_numforecasts: record.numforecasts ? true : false, has_numforecasts: record.numforecasts ? true : false,
objectID: index, objectID: index,
@ -50,11 +54,11 @@ export async function rebuildAlgoliaDatabaseTheEasyWay() {
})); }));
// this is necessary to filter by missing attributes https://www.algolia.com/doc/guides/managing-results/refine-results/filtering/how-to/filter-by-null-or-missing-attributes/ // this is necessary to filter by missing attributes https://www.algolia.com/doc/guides/managing-results/refine-results/filtering/how-to/filter-by-null-or-missing-attributes/
console.log(index.appId, index.indexName);
if (index.exists()) { if (index.exists()) {
console.log("Index exists"); console.log("Index exists");
index await index.replaceAllObjects(records, { safe: true });
.replaceAllObjects(records, { safe: true })
.catch((error) => console.log(error));
console.log( console.log(
`Pushed ${records.length} records. Algolia will update asynchronously` `Pushed ${records.length} records. Algolia will update asynchronously`
); );

View File

@ -1,6 +1,9 @@
import algoliasearch from "algoliasearch"; import algoliasearch from "algoliasearch";
const client = algoliasearch("96UD3NTQ7L", "618dbd0092971388cfd43aac1ae5f1f5"); // Only search. const client = algoliasearch(
process.env.NEXT_PUBLIC_ALGOLIA_APP_ID,
process.env.NEXT_PUBLIC_ALGOLIA_SEARCH_KEY
);
const index = client.initIndex("metaforecast"); const index = client.initIndex("metaforecast");
let buildFilter = ({ let buildFilter = ({