mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-02 07:43:11 +03:00
Connected tags to the search index
refs https://github.com/TryGhost/Team/issues/1665 - Tags should be searchable. This change hooks up the Tags Content API with the search index
This commit is contained in:
parent
dfc5b1c33d
commit
592e051485
@ -36,12 +36,22 @@ export default class SearchIndex {
|
||||
});
|
||||
}
|
||||
|
||||
#updateTagsIndex(data) {
|
||||
data.tags.forEach((tag) => {
|
||||
this.tagsIndex.addDoc({
|
||||
id: tag.id,
|
||||
name: tag.name
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async init() {
|
||||
// remove default stop words to search of *any* word
|
||||
elasticlunr.clearStopWords();
|
||||
|
||||
const postsAPIUrl = `${this.apiUrl}/posts/?key=${this.apiKey}&limit=all&fields=id,slug,title,excerpt,url,updated_at,visibility&order=updated_at%20desc&formats=plaintext`;
|
||||
const authorsAPIUrl = `${this.apiUrl}/authors/?key=${this.apiKey}&limit=all&fields=id,slug,name,profile_image`;
|
||||
const tagsAPIUrl = `${this.apiUrl}/tags/?key=${this.apiKey}&limit=all&fields=id,slug,name,url`;
|
||||
|
||||
const indexDump = JSON.parse(this.storage.getItem('ease_search_index'));
|
||||
|
||||
@ -71,6 +81,17 @@ export default class SearchIndex {
|
||||
|
||||
this.#updateAuthorsIndex(authors);
|
||||
}
|
||||
|
||||
const tagsResponse = await fetch(tagsAPIUrl);
|
||||
const tags = await tagsResponse.json();
|
||||
|
||||
if (tags.tags.length > 0) {
|
||||
this.tagsIndex = elasticlunr();
|
||||
this.tagsIndex.addField('name');
|
||||
this.tagsIndex.setRef('id');
|
||||
|
||||
this.#updateTagsIndex(tags);
|
||||
}
|
||||
} else {
|
||||
this.postsIndex = elasticlunr.Index.load(indexDump);
|
||||
|
||||
@ -88,6 +109,7 @@ export default class SearchIndex {
|
||||
search(value) {
|
||||
const posts = this.postsIndex.search(value, {expand: true});
|
||||
const authors = this.authorsIndex.search(value, {expand: true});
|
||||
const tags = this.tagsIndex.search(value, {expand: true});
|
||||
|
||||
return {
|
||||
posts: posts.map((doc) => {
|
||||
@ -95,6 +117,9 @@ export default class SearchIndex {
|
||||
}),
|
||||
authors: authors.map((doc) => {
|
||||
return this.authorsIndex.documentStore.docs[doc.ref];
|
||||
}),
|
||||
tags: tags.map((doc) => {
|
||||
return this.tagsIndex.documentStore.docs[doc.ref];
|
||||
})
|
||||
};
|
||||
}
|
||||
|
@ -22,6 +22,13 @@ describe('search index', function () {
|
||||
id: 'different_uniq',
|
||||
name: 'Barcelona Author'
|
||||
}]
|
||||
})
|
||||
.get('/tags/?key=secret_key&&limit=all&fields=id,slug,name,url')
|
||||
.reply(200, {
|
||||
tags: [{
|
||||
id: 'uniq_tag',
|
||||
name: 'Barcelona Tag'
|
||||
}]
|
||||
});
|
||||
|
||||
await searchIndex.init({apiUrl, apiKey});
|
||||
@ -51,6 +58,15 @@ describe('search index', function () {
|
||||
name: 'Barcelona Author',
|
||||
profile_image: 'https://url_to_avatar/barcelona.png'
|
||||
}]
|
||||
})
|
||||
.get('/tags/?key=secret_key&&limit=all&fields=id,slug,name,url')
|
||||
.reply(200, {
|
||||
tags: [{
|
||||
id: 'uniq_tag',
|
||||
slug: 'barcelona-tag',
|
||||
name: 'Barcelona Tag',
|
||||
url: 'http://localhost/ghost/tags/barcelona-tag'
|
||||
}]
|
||||
});
|
||||
|
||||
await searchIndex.init({apiUrl, apiKey});
|
||||
@ -58,9 +74,16 @@ describe('search index', function () {
|
||||
let searchResults = searchIndex.search('Barcelona');
|
||||
expect(searchResults.posts.length).toEqual(1);
|
||||
expect(searchResults.posts[0].title).toEqual('Awesome Barcelona Life');
|
||||
|
||||
expect(searchResults.authors.length).toEqual(1);
|
||||
expect(searchResults.authors[0].name).toEqual('Barcelona Author');
|
||||
|
||||
expect(searchResults.tags.length).toEqual(1);
|
||||
expect(searchResults.tags[0].name).toEqual('Barcelona Tag');
|
||||
|
||||
searchResults = searchIndex.search('Nothing like this');
|
||||
expect(searchResults.posts.length).toEqual(0);
|
||||
expect(searchResults.authors.length).toEqual(0);
|
||||
expect(searchResults.tags.length).toEqual(0);
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user