0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-11 02:12:21 -05:00

Added post properties to search results

refs https://github.com/TryGhost/Team/issues/1665

- The npm package for elasticlunr does not contain the latest changes (no way to include document properties in search results). This hack adds document properties manually.
- Long term we should think about either helping the maintainer with releases of the library or weight out alternatives like fuse.js (https://fusejs.io)
This commit is contained in:
Naz 2022-07-05 16:38:29 +02:00
parent 72aa270282
commit b77fee0735
2 changed files with 17 additions and 12 deletions

View file

@ -14,7 +14,11 @@ export default class SearchIndex {
#updateIndex(data) {
data.posts.forEach((post) => {
this.index.addDoc(post);
this.index.addDoc({
id: post.id,
title: post.title,
excerpt: post.excerpt
});
});
this.storage.setItem('ease_search_index', JSON.stringify(this.index));
@ -32,18 +36,15 @@ export default class SearchIndex {
this.storage.removeItem('ease_index');
this.storage.removeItem('ease_last');
if (
!indexDump
) {
if (!indexDump) {
return fetch(url)
.then(response => response.json())
.then((data) => {
if (data.posts.length > 0) {
this.index = elasticlunr(function () {
this.addField('title');
this.addField('plaintext');
this.setRef('id');
});
this.index = elasticlunr();
this.index.addField('title');
this.index.addField('excerpt');
this.index.setRef('id');
this.#updateIndex(data);
}
@ -63,6 +64,9 @@ export default class SearchIndex {
}
search(value) {
return this.index.search(value, {expand: true});
const searchResults = this.index.search(value, {expand: true});
return searchResults.map((doc) => {
return this.index.documentStore.docs[doc.ref];
});
}
}

View file

@ -32,8 +32,8 @@ describe('search index', function () {
.reply(200, {
posts: [{
id: 'sounique',
title: 'Amazing Barcelona Life',
plaintext: 'We are sitting by the pool and smashing out search features'
title: 'Awesome Barcelona Life',
excerpt: 'We are sitting by the pool and smashing out search features'
}]
});
@ -41,6 +41,7 @@ describe('search index', function () {
let searchResults = searchIndex.search('Barcelona');
expect(searchResults.length).toEqual(1);
expect(searchResults[0].title).toEqual('Awesome Barcelona Life');
searchResults = searchIndex.search('Nothing like this');
expect(searchResults.length).toEqual(0);