1
0
mirror of https://github.com/bpatrik/pigallery2.git synced 2025-01-10 04:07:35 +02:00

Making autocompete finetuning #660

This commit is contained in:
Patrik J. Braun 2023-06-24 01:00:15 +02:00
parent b744c72c31
commit 574de71eb7
3 changed files with 53 additions and 36 deletions

View File

@ -85,7 +85,7 @@ export class SearchManager {
.where('photo.metadata.keywords LIKE :text COLLATE ' + SQL_COLLATE, {
text: '%' + text + '%',
})
.limit(Config.Search.AutoComplete.ItemsPerCategory.keyword*2)
.limit(Config.Search.AutoComplete.ItemsPerCategory.keyword)
.getRawMany()
)
.map(
@ -120,7 +120,7 @@ export class SearchManager {
text: '%' + text + '%',
})
.limit(
Config.Search.AutoComplete.ItemsPerCategory.person*2
Config.Search.AutoComplete.ItemsPerCategory.person
)
.orderBy('person.count', 'DESC')
.getRawMany()
@ -161,7 +161,7 @@ export class SearchManager {
.groupBy(
'photo.metadata.positionData.country, photo.metadata.positionData.state, photo.metadata.positionData.city'
)
.limit(Config.Search.AutoComplete.ItemsPerCategory.position*2)
.limit(Config.Search.AutoComplete.ItemsPerCategory.position )
.getRawMany()
)
.filter((pm): boolean => !!pm)
@ -199,7 +199,7 @@ export class SearchManager {
text: '%' + text + '%',
})
.limit(
Config.Search.AutoComplete.ItemsPerCategory.file_name*2
Config.Search.AutoComplete.ItemsPerCategory.fileName
)
.getRawMany()
).map((r) => r.name),
@ -223,7 +223,7 @@ export class SearchManager {
{text: '%' + text + '%'}
)
.limit(
Config.Search.AutoComplete.ItemsPerCategory.caption*2
Config.Search.AutoComplete.ItemsPerCategory.caption
)
.getRawMany()
).map((r) => r.caption),
@ -246,7 +246,7 @@ export class SearchManager {
text: '%' + text + '%',
})
.limit(
Config.Search.AutoComplete.ItemsPerCategory.directory*2
Config.Search.AutoComplete.ItemsPerCategory.directory
)
.getRawMany()
).map((r) => r.name),
@ -255,22 +255,23 @@ export class SearchManager {
);
}
let result: AutoCompleteItem[];
const result: AutoCompleteItem[] = [];
// if not enough items are available, load more from one category
if (
[].concat(...partialResult).length <
Config.Search.AutoComplete.ItemsPerCategory.maxItems
) {
result = [].concat(...partialResult);
} else {
result = [].concat(
...partialResult.map((l) =>
l.slice(0, (Config.Search.AutoComplete.ItemsPerCategory as any)[SearchQueryTypes[l[0].type]])
)
);
while (result.length < Config.Search.AutoComplete.ItemsPerCategory.maxItems) {
let adding = false;
for (let i = 0; i < partialResult.length; ++i) {
if (partialResult[i].length <= 0) {
continue;
}
result.push(partialResult[i].pop());
adding = true;
}
if (!adding) {
break;
}
}
return SearchManager.autoCompleteItemsUnique(result);
}
@ -720,7 +721,7 @@ export class SearchManager {
const relationBottom = tq.negate ? '<=' : '>';
switch (tq.frequency) {
case DatePatternFrequency.every_year:
if(tq.daysLength >= 365){
if (tq.daysLength >= 365) {
return q;
}
q.where(
@ -729,7 +730,7 @@ export class SearchManager {
textParam);
break;
case DatePatternFrequency.every_month:
if(tq.daysLength >=31){
if (tq.daysLength >= 31) {
return q;
}
q.where(
@ -738,7 +739,7 @@ export class SearchManager {
textParam);
break;
case DatePatternFrequency.every_week:
if(tq.daysLength >= 7){
if (tq.daysLength >= 7) {
return q;
}
q.where(

View File

@ -80,7 +80,7 @@ export class AutoCompleteItemsPerCategoryConfig {
},
description: $localize`Maximum number autocomplete items shown per photo category.`
})
file_name: number = 2;
fileName: number = 2;
@ConfigProperty({
type: 'unsignedInt',

View File

@ -244,27 +244,43 @@ export class AutoCompleteService {
items: RenderableAutoCompleteItem[]
): RenderableAutoCompleteItem[] {
const textLC = text.toLowerCase();
// Source: https://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex
const isStartRgx = new RegExp('(\\s|^)' + textLC.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'i');
return items.sort((a, b) => {
const aLC = a.text.toLowerCase();
const bLC = b.text.toLowerCase();
// prioritize persons higher
if (a.type !== b.type) {
if (a.type === SearchQueryTypes.person) {
return -1;
} else if (b.type === SearchQueryTypes.person) {
return 1;
}
}
if (
(aLC.startsWith(textLC) && bLC.startsWith(textLC)) ||
(!aLC.startsWith(textLC) && !bLC.startsWith(textLC))
) {
const basicCompare = () => {
// prioritize persons higher
if (a.type !== b.type) {
if (a.type === SearchQueryTypes.person) {
return -1;
} else if (b.type === SearchQueryTypes.person) {
return 1;
}
}
return aLC.localeCompare(bLC);
};
// both starts with the searched string
if (aLC.startsWith(textLC) && bLC.startsWith(textLC)) {
return basicCompare();
// none starts with the searched string
} else if (!aLC.startsWith(textLC) && !bLC.startsWith(textLC)) {
if ((isStartRgx.test(aLC) && isStartRgx.test(bLC)) ||
(!isStartRgx.test(aLC) && !isStartRgx.test(bLC))) {
return basicCompare();
} else if (isStartRgx.test(aLC)) {
return -1;
}
return 1;
// one of them starts with the searched string
} else if (aLC.startsWith(textLC)) {
return -1;
}
return 1;
return basicCompare();
});
}