This commit is contained in:
adueck 2022-11-25 19:56:15 +05:00
parent ffca9b3dbc
commit 4ca43f27cd
2 changed files with 20 additions and 12 deletions

View File

@ -8,12 +8,12 @@ export const publishDictionary = functions.runWith({
timeoutSeconds: 60, timeoutSeconds: 60,
memory: "2GB" memory: "2GB"
}).https.onRequest( }).https.onRequest(
// lingdocsAuth( lingdocsAuth(
async (req, res: functions.Response<FT.PublishDictionaryResponse | FT.FunctionError>) => { async (req, res: functions.Response<FT.PublishDictionaryResponse | FT.FunctionError>) => {
// if (req.user.level !== "editor") { if (req.user.level !== "editor") {
// res.status(403).send({ ok: false, error: "403 forbidden" }); res.status(403).send({ ok: false, error: "403 forbidden" });
// return; return;
// } }
try { try {
const response = await publish(); const response = await publish();
res.send(response); res.send(response);
@ -22,7 +22,7 @@ export const publishDictionary = functions.runWith({
res.status(500).send({ ok: false, error: e.message }); res.status(500).send({ ok: false, error: e.message });
} }
} }
// ) )
); );
export const submissions = functions.runWith({ export const submissions = functions.runWith({

View File

@ -151,7 +151,10 @@ function englishLookup<S extends T.DictionaryEntry>({ searchString, page, tpFilt
page: number, page: number,
tpFilter?: (e: T.DictionaryEntry) => e is S, tpFilter?: (e: T.DictionaryEntry) => e is S,
}): S[] { }): S[] {
let resultsGiven: number[] = [];; function sortByR(a: T.DictionaryEntry, b: T.DictionaryEntry) {
return (b.r || 3) - (a.r || 3);
};
let resultsGiven: number[] = [];
// get exact results // get exact results
const exactQuery = { const exactQuery = {
e: { e: {
@ -164,6 +167,7 @@ function englishLookup<S extends T.DictionaryEntry>({ searchString, page, tpFilt
.limit(exactResultsLimit) .limit(exactResultsLimit)
.simplesort("i") .simplesort("i")
.data(); .data();
exactResults.sort(sortByR);
resultsGiven = exactResults.map((mpd: any) => mpd.$loki); resultsGiven = exactResults.map((mpd: any) => mpd.$loki);
// get results with full word match at beginning of string // get results with full word match at beginning of string
const startingQuery = { const startingQuery = {
@ -178,6 +182,7 @@ function englishLookup<S extends T.DictionaryEntry>({ searchString, page, tpFilt
.limit(startingResultsLimit) .limit(startingResultsLimit)
.simplesort("i") .simplesort("i")
.data(); .data();
startingResults.sort(sortByR);
resultsGiven = [...resultsGiven, ...startingResults.map((mpd: any) => mpd.$loki)]; resultsGiven = [...resultsGiven, ...startingResults.map((mpd: any) => mpd.$loki)];
// get results with full word match anywhere // get results with full word match anywhere
const fullWordQuery = { const fullWordQuery = {
@ -192,6 +197,7 @@ function englishLookup<S extends T.DictionaryEntry>({ searchString, page, tpFilt
.limit(fullWordResultsLimit) .limit(fullWordResultsLimit)
.simplesort("i") .simplesort("i")
.data(); .data();
fullWordResults.sort(sortByR);
resultsGiven = [...resultsGiven, ...fullWordResults.map((mpd: any) => mpd.$loki)] resultsGiven = [...resultsGiven, ...fullWordResults.map((mpd: any) => mpd.$loki)]
// get results with partial match anywhere // get results with partial match anywhere
const partialMatchQuery = { const partialMatchQuery = {
@ -202,11 +208,12 @@ function englishLookup<S extends T.DictionaryEntry>({ searchString, page, tpFilt
}; };
const partialMatchLimit = (pageSize * page) - resultsGiven.length; const partialMatchLimit = (pageSize * page) - resultsGiven.length;
const partialMatchResults = dictDb.collection.chain() const partialMatchResults = dictDb.collection.chain()
.where(tpFilter ? tpFilter : () => true) .where(tpFilter ? tpFilter : () => true)
.find(partialMatchQuery) .find(partialMatchQuery)
.limit(partialMatchLimit) .limit(partialMatchLimit)
.simplesort("i") .simplesort("i")
.data(); .data();
partialMatchResults.sort(sortByR);
const results = [ const results = [
...exactResults, ...exactResults,
...startingResults, ...startingResults,
@ -216,6 +223,7 @@ function englishLookup<S extends T.DictionaryEntry>({ searchString, page, tpFilt
if (tpFilter) { if (tpFilter) {
return results.filter(tpFilter); return results.filter(tpFilter);
} }
console.log({ results });
return results; return results;
} }