mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-11-28 14:03:48 +03:00
54812dc67a
ref https://linear.app/tryghost/issue/MOM-117 ref https://linear.app/tryghost/issue/MOM-70 - moved current search into new `search-provider` service and updated `search` service to use the provider service internally - added `search-provider-beta` service - uses `flexsearch` as the underlying index for each document so we have better indexing and matching compared to the naive exact-match search we had previously - adds `excerpt` matching for posts and pages - keeps results output the same as the original search provider - added `internalLinkingSearchImprovements` labs flag so we can test this internally before reaching our internal linking beta testers - updated `search` service to switch between providers based on labs flag
61 lines
1.5 KiB
JavaScript
61 lines
1.5 KiB
JavaScript
import Service from '@ember/service';
|
|
import {action} from '@ember/object';
|
|
import {isBlank} from '@ember/utils';
|
|
import {inject as service} from '@ember/service';
|
|
import {task, timeout} from 'ember-concurrency';
|
|
|
|
export default class SearchService extends Service {
|
|
@service ajax;
|
|
@service feature;
|
|
@service notifications;
|
|
@service searchProvider;
|
|
@service searchProviderBeta;
|
|
@service store;
|
|
|
|
isContentStale = true;
|
|
|
|
get provider() {
|
|
return this.feature.internalLinkingSearchImprovements
|
|
? this.searchProviderBeta
|
|
: this.searchProvider;
|
|
}
|
|
|
|
@action
|
|
expireContent() {
|
|
this.isContentStale = true;
|
|
}
|
|
|
|
@task({restartable: true})
|
|
*searchTask(term) {
|
|
if (isBlank(term)) {
|
|
return [];
|
|
}
|
|
|
|
// start loading immediately in the background
|
|
this.refreshContentTask.perform();
|
|
|
|
// debounce searches to 200ms to avoid thrashing CPU
|
|
yield timeout(200);
|
|
|
|
// wait for any on-going refresh to finish
|
|
if (this.refreshContentTask.isRunning) {
|
|
yield this.refreshContentTask.lastRunning;
|
|
}
|
|
|
|
return yield this.provider.searchTask.perform(term);
|
|
}
|
|
|
|
@task({drop: true})
|
|
*refreshContentTask({forceRefresh = false} = {}) {
|
|
if (!forceRefresh && !this.isContentStale) {
|
|
return true;
|
|
}
|
|
|
|
this.isContentStale = true;
|
|
|
|
yield this.provider.refreshContentTask.perform();
|
|
|
|
this.isContentStale = false;
|
|
}
|
|
}
|