1
0
mirror of https://github.com/nextapps-de/flexsearch.git synced 2025-09-25 04:51:29 +02:00

fix document result highlighting when using "pluck"

This commit is contained in:
Thomas Wilkerling
2025-04-14 10:07:11 +02:00
parent d40308d57a
commit c37605cf7d
22 changed files with 2918 additions and 2809 deletions

View File

@@ -408,7 +408,7 @@ Document.prototype.search = function (query, limit, options, _promises) {
}
if (pluck) {
return resolve ? /** @type {SearchResults|EnrichedSearchResults} */res : new Resolver( /** @type {IntermediateSearchResults} */res);
return resolve ? highlight ? highlight_fields( /** @type {string} */query, res, this.index, pluck, highlight) : /** @type {SearchResults|EnrichedSearchResults} */res : new Resolver( /** @type {IntermediateSearchResults} */res);
}
result[i] = {
@@ -423,23 +423,22 @@ Document.prototype.search = function (query, limit, options, _promises) {
for (let j = 0; j < promises.length; j++) {
result[j].result = promises[j];
}
return merge ? merge_fields(result) : highlight ? highlight_fields(result, /** @type {string} */query, self.index, self.field, self.tree, highlight) : /** @type {DocumentSearchResults} */result;
return merge ? merge_fields(result) : highlight ? highlight_fields( /** @type {string} */query, result, self.index, pluck, highlight) : /** @type {DocumentSearchResults} */result;
});
}
return merge ? merge_fields(result) : highlight ? highlight_fields(result, /** @type {string} */query, this.index, this.field, this.tree, highlight) : /** @type {DocumentSearchResults} */result;
return merge ? merge_fields(result) : highlight ? highlight_fields( /** @type {string} */query, result, this.index, pluck, highlight) : /** @type {DocumentSearchResults} */result;
};
/**
* @param {EnrichedDocumentSearchResults} result
* @param {string} query
* @param {EnrichedDocumentSearchResults|EnrichedSearchResults} result
* @param {Map<string, Index>} index
* @param {string} field
* @param {Array<string>} tree
* @param {string} pluck
* @param {string} template
* @return {EnrichedDocumentSearchResults}
* @return {EnrichedDocumentSearchResults|EnrichedSearchResults}
*/
function highlight_fields(result, query, index, field, tree, template) {
function highlight_fields(query, result, index, pluck, template) {
// The biggest issue is dealing with custom encoders, for this reason
// a regular expression can't apply
@@ -454,18 +453,26 @@ function highlight_fields(result, query, index, field, tree, template) {
// for every field
for (let i = 0, res_field, enc, idx, path; i < result.length; i++) {
res_field = result[i].field;
// skip when not a field entry (e.g. tags)
if (!res_field) continue;
for (let i = 0, enc, idx, path; i < result.length; i++) {
/** @type {EnrichedSearchResults} */
let res = result[i].result;
idx = index.get(res_field);
let res;
if (pluck) {
res = result;
path = pluck;
} else {
const tmp = result[i];
path = tmp.field;
if (!path) continue;
res = tmp.result;
}
// skip when not a field entry (e.g. tags)
idx = index.get(path);
enc = idx.encoder;
tokenize = idx.tokenize;
path = tree[field.indexOf(res_field)];
// re-encode query when encoder has changed
if (enc !== encoder) {
@@ -528,6 +535,10 @@ function highlight_fields(result, query, index, field, tree, template) {
res[j].highlight = str;
}
if (pluck) {
break;
}
}
return result;