I have a large text with 10 "hell" words in the document, and I want flexSearch to retrieve all the matches in the full text for me. I currently use the index. search method, but it can only retrieve the first hell, not include the rest. I don't know what's going on.
my core code:
import Flexsearch from "flexsearch";
//英文搜索
const searchENOptions = {
encode: "icase",
tokenize: "reverse",
resolution: 9,
doc: {
id: "key",
field: ["content"],
},
};
this.indexEN = new Flexsearch(searchENOptions);
const { pages } = this.$site;
this.indexEN.add(pages);
const query = this.query.trim().toLowerCase(); // query keywords
let result = null;
if (!query) {
return;
}
const regex = /[\x00-\x7F]/g;
// currently, the result only has one item.
result = this.indexEN.search(query).map((page) => {
return {
...page,
text: this.getSuggestionText(page),
};
});
}
function getSuggestionText(page) {
const content = page.content; // fullText
const queryIndex = content
.toLowerCase()
.indexOf(this.query.toLowerCase());
const queryFirstWord = this.query.split(" ")[0];
let startIndex =
queryIndex === -1
? content.toLowerCase().indexOf(queryFirstWord.toLowerCase())
: queryIndex;
let prefix = "";
if (startIndex > 15) {
startIndex -= 15;
prefix = "... ";
}
const text = page.content.substr(startIndex, SEARCH_RESULT_LENGTH);
return highlightText(text, this.query) + prefix;
}