metalsmith-lunr
metalsmith-lunr copied to clipboard
Uncaught TypeError: Cannot read property 'tf' of undefined
I'm not sure if this is a bug for you, or for Lunr, but seeing as it has to do with the index, and this is what I'm using to build the index, I thought I would start here.
So I'm getting the error Uncaught TypeError: Cannot read property 'tf' of undefined
. It seems to have to do with parsing vector similarities. The code seems to be looking for tf
on a document in the tokenStore.
https://cl.ly/1B3f1m0B391b
My lunr configs look like:
metalsmithLunr({
ref: 'id',
indexPath: 'api/search-index.json',
fields: {
headline: 5,
subheadline: 3,
category: 5,
tags: 3,
contents: 1,
},
preprocess: function(content) {
const tr = (str) => {
const map = {"а":"a","б":"b",/* truncated for issue */"І":"I","і":"i"};
let new_str = "", char, substitute, n = str.length;
for(let i = 0; i < n; i++) {
char = str[i];
substitute = map[char];
new_str += substitute ? substitute : char;
}
return new_str;
};
return tr(
content.replace(/<[^>]+>/g, ' ') // Strip HTML
) // Transliterate foreign characters
.replace(/[^\w]/g, ' ') // Strip Punctuation
;
},
}),
And the client side code looks like:
let ip = new Promise((resolve, reject) => {
$.getJSON('/api/search-index.json', data => {
console.log('SEARCH DATA', data);
resolve(lunr.Index.load(data));
});
});
and then later:
if (keywords) {
SearchService.results = SearchService.index.search(keywords);
}
I can provide the index data or the serialized index object if you would like.
Do give you more information, I seem to only get the error when I have a field in the fields
object that is optional. In the example above, I think the subheadline
is present, but always has an empty value.