2023-07-27 09:09:01 +02:00
|
|
|
|
import Flexsearch from 'flexsearch';
|
2022-07-06 17:09:01 +02:00
|
|
|
|
import GhostContentAPI from '@tryghost/content-api';
|
2022-07-05 10:09:10 +02:00
|
|
|
|
|
2022-07-05 11:22:36 +02:00
|
|
|
|
export default class SearchIndex {
|
2024-10-01 14:04:54 -04:00
|
|
|
|
constructor({adminUrl, apiKey, dir}) {
|
2022-07-06 17:09:01 +02:00
|
|
|
|
this.api = new GhostContentAPI({
|
2022-07-07 11:09:53 +02:00
|
|
|
|
url: adminUrl,
|
2022-07-06 17:09:01 +02:00
|
|
|
|
key: apiKey,
|
|
|
|
|
version: 'v5.0'
|
|
|
|
|
});
|
2024-10-01 14:04:54 -04:00
|
|
|
|
const rtl = (dir === 'rtl');
|
|
|
|
|
const tokenize = (dir === 'rtl') ? 'reverse' : 'forward';
|
2023-07-27 09:09:01 +02:00
|
|
|
|
this.postsIndex = new Flexsearch.Document({
|
2024-10-01 14:04:54 -04:00
|
|
|
|
tokenize: tokenize,
|
|
|
|
|
rtl: rtl,
|
2022-07-07 12:52:01 +02:00
|
|
|
|
document: {
|
|
|
|
|
id: 'id',
|
|
|
|
|
index: ['title', 'excerpt'],
|
|
|
|
|
store: true
|
2024-10-02 02:01:54 +08:00
|
|
|
|
},
|
|
|
|
|
...this.#getEncodeOptions()
|
2022-07-07 12:52:01 +02:00
|
|
|
|
});
|
2023-07-27 09:09:01 +02:00
|
|
|
|
this.authorsIndex = new Flexsearch.Document({
|
2024-10-01 14:04:54 -04:00
|
|
|
|
tokenize: tokenize,
|
|
|
|
|
rtl: rtl,
|
2022-07-07 12:52:01 +02:00
|
|
|
|
document: {
|
|
|
|
|
id: 'id',
|
|
|
|
|
index: ['name'],
|
|
|
|
|
store: true
|
2024-10-02 02:01:54 +08:00
|
|
|
|
},
|
|
|
|
|
...this.#getEncodeOptions()
|
2022-07-07 12:52:01 +02:00
|
|
|
|
});
|
2023-07-27 09:09:01 +02:00
|
|
|
|
this.tagsIndex = new Flexsearch.Document({
|
2024-10-01 14:04:54 -04:00
|
|
|
|
tokenize: tokenize,
|
|
|
|
|
rtl: rtl,
|
2022-07-07 12:52:01 +02:00
|
|
|
|
document: {
|
|
|
|
|
id: 'id',
|
|
|
|
|
index: ['name'],
|
|
|
|
|
store: true
|
2024-10-02 02:01:54 +08:00
|
|
|
|
},
|
|
|
|
|
...this.#getEncodeOptions()
|
2022-07-07 12:52:01 +02:00
|
|
|
|
});
|
2022-07-05 10:09:10 +02:00
|
|
|
|
|
2022-07-05 11:22:36 +02:00
|
|
|
|
this.init = this.init.bind(this);
|
|
|
|
|
this.search = this.search.bind(this);
|
|
|
|
|
}
|
2022-07-05 10:09:10 +02:00
|
|
|
|
|
2022-07-06 17:09:01 +02:00
|
|
|
|
#updatePostIndex(posts) {
|
|
|
|
|
posts.forEach((post) => {
|
2022-07-07 12:52:01 +02:00
|
|
|
|
this.postsIndex.add(post);
|
2022-07-05 10:09:10 +02:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-06 17:09:01 +02:00
|
|
|
|
#updateAuthorsIndex(authors) {
|
|
|
|
|
authors.forEach((author) => {
|
2022-07-07 12:52:01 +02:00
|
|
|
|
this.authorsIndex.add(author);
|
2022-07-06 10:37:49 +02:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-06 17:09:01 +02:00
|
|
|
|
#updateTagsIndex(tags) {
|
|
|
|
|
tags.forEach((tag) => {
|
2022-07-07 12:52:01 +02:00
|
|
|
|
this.tagsIndex.add(tag);
|
2022-07-06 10:56:16 +02:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-05 11:22:36 +02:00
|
|
|
|
async init() {
|
2022-07-06 17:09:01 +02:00
|
|
|
|
let posts = await this.api.posts.browse({
|
2022-07-07 12:58:41 +02:00
|
|
|
|
limit: '10000',
|
2022-07-06 17:09:01 +02:00
|
|
|
|
fields: 'id,slug,title,excerpt,url,updated_at,visibility',
|
2022-07-07 12:55:08 +02:00
|
|
|
|
order: 'updated_at DESC'
|
2022-07-06 17:09:01 +02:00
|
|
|
|
});
|
2022-07-06 11:41:58 +02:00
|
|
|
|
|
2022-07-06 17:09:01 +02:00
|
|
|
|
if (posts || posts.length > 0) {
|
|
|
|
|
if (!posts.length) {
|
|
|
|
|
posts = [posts];
|
|
|
|
|
}
|
2022-07-06 11:41:58 +02:00
|
|
|
|
this.#updatePostIndex(posts);
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-06 17:09:01 +02:00
|
|
|
|
let authors = await this.api.authors.browse({
|
2022-07-07 12:58:41 +02:00
|
|
|
|
limit: '10000',
|
|
|
|
|
fields: 'id,slug,name,url,profile_image',
|
|
|
|
|
order: 'updated_at DESC'
|
2022-07-06 17:09:01 +02:00
|
|
|
|
});
|
2022-07-06 11:41:58 +02:00
|
|
|
|
|
2022-07-06 17:09:01 +02:00
|
|
|
|
if (authors || authors.length > 0) {
|
|
|
|
|
if (!authors.length) {
|
|
|
|
|
authors = [authors];
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-06 11:41:58 +02:00
|
|
|
|
this.#updateAuthorsIndex(authors);
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-06 17:09:01 +02:00
|
|
|
|
let tags = await this.api.tags.browse({
|
2022-07-07 12:58:41 +02:00
|
|
|
|
limit: '10000',
|
|
|
|
|
fields: 'id,slug,name,url',
|
2022-07-08 09:34:04 +02:00
|
|
|
|
order: 'updated_at DESC',
|
|
|
|
|
filter: 'visibility:public'
|
2022-07-06 17:09:01 +02:00
|
|
|
|
});
|
2022-07-06 11:41:58 +02:00
|
|
|
|
|
2022-07-06 17:09:01 +02:00
|
|
|
|
if (tags || tags.length > 0) {
|
|
|
|
|
if (!tags.length) {
|
|
|
|
|
tags = [tags];
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-06 11:41:58 +02:00
|
|
|
|
this.#updateTagsIndex(tags);
|
2022-07-05 11:22:36 +02:00
|
|
|
|
}
|
2022-07-05 10:09:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-07 12:52:01 +02:00
|
|
|
|
#normalizeSearchResult(result) {
|
|
|
|
|
const normalized = [];
|
|
|
|
|
const usedIds = {};
|
|
|
|
|
|
|
|
|
|
result.forEach((resultItem) => {
|
|
|
|
|
resultItem.result.forEach((doc) => {
|
|
|
|
|
if (!usedIds[doc.id]) {
|
|
|
|
|
normalized.push(doc.doc);
|
|
|
|
|
usedIds[doc.id] = true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return normalized;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-05 11:22:36 +02:00
|
|
|
|
search(value) {
|
2022-07-06 11:26:05 +02:00
|
|
|
|
const posts = this.postsIndex.search(value, {
|
2022-07-07 12:52:01 +02:00
|
|
|
|
enrich: true
|
2022-07-06 11:26:05 +02:00
|
|
|
|
});
|
|
|
|
|
const authors = this.authorsIndex.search(value, {
|
2022-07-07 12:52:01 +02:00
|
|
|
|
enrich: true
|
2022-07-06 11:26:05 +02:00
|
|
|
|
});
|
|
|
|
|
const tags = this.tagsIndex.search(value, {
|
2022-07-07 12:52:01 +02:00
|
|
|
|
enrich: true
|
2022-07-06 11:26:05 +02:00
|
|
|
|
});
|
2022-07-06 10:37:49 +02:00
|
|
|
|
|
2022-07-05 17:35:07 +02:00
|
|
|
|
return {
|
2022-07-07 12:52:01 +02:00
|
|
|
|
posts: this.#normalizeSearchResult(posts),
|
|
|
|
|
authors: this.#normalizeSearchResult(authors),
|
|
|
|
|
tags: this.#normalizeSearchResult(tags)
|
2022-07-05 17:35:07 +02:00
|
|
|
|
};
|
2022-07-05 11:22:36 +02:00
|
|
|
|
}
|
2024-10-02 02:01:54 +08:00
|
|
|
|
|
|
|
|
|
#getEncodeOptions() {
|
|
|
|
|
const regex = new RegExp(
|
|
|
|
|
`[\u{4E00}-\u{9FFF}\u{3040}-\u{309F}\u{30A0}-\u{30FF}\u{AC00}-\u{D7A3}\u{3400}-\u{4DBF}\u{20000}-\u{2A6DF}\u{2A700}-\u{2B73F}\u{2B740}-\u{2B81F}\u{2B820}-\u{2CEAF}\u{2CEB0}-\u{2EBEF}\u{30000}-\u{3134F}\u{31350}-\u{323AF}\u{2EBF0}-\u{2EE5F}\u{F900}-\u{FAFF}\u{2F800}-\u{2FA1F}]|[0-9A-Za-zа-я\u00C0-\u017F\u0400-\u04FF\u0600-\u06FF\u0980-\u09FF\u1E00-\u1EFF]+`,
|
|
|
|
|
'mug'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
encode: (str) => {
|
|
|
|
|
return ('' + str).toLowerCase().match(regex) ?? [];
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
2022-07-05 11:22:36 +02:00
|
|
|
|
}
|