From 592e051485e2c3b80a7a059fa57e2e60799a5b22 Mon Sep 17 00:00:00 2001 From: Naz Date: Wed, 6 Jul 2022 10:56:16 +0200 Subject: [PATCH] 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 --- ghost/sodo-search/src/search-index.js | 25 ++++++++++++++++++++++ ghost/sodo-search/src/search-index.test.js | 23 ++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/ghost/sodo-search/src/search-index.js b/ghost/sodo-search/src/search-index.js index 303e920bcd..f046054c7f 100644 --- a/ghost/sodo-search/src/search-index.js +++ b/ghost/sodo-search/src/search-index.js @@ -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]; }) }; } diff --git a/ghost/sodo-search/src/search-index.test.js b/ghost/sodo-search/src/search-index.test.js index 8a110e671e..ee4513739d 100644 --- a/ghost/sodo-search/src/search-index.test.js +++ b/ghost/sodo-search/src/search-index.test.js @@ -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); }); });