flexsearch
flexsearch copied to clipboard
Results Not Sorted by Relevance for Simple Queries Using match Preset
Description
When performing a search for the phrase 'cats cute' using the match
preset and forward
tokenization, the results are returned in the order they were added, without sorting by closest match to the search phrase.
Steps to Reproduce
- Initialize a FlexSearch index with the
match
preset andforward
tokenization. - Add a series of strings to the index, each progressively shorter and varying by the inclusion of additional words.
- Perform a search for the phrase 'cats cute'.
Expected Behavior The search results should be sorted by relevance, with the string closest to 'cats cute' appearing first, followed by others in decreasing order of match closeness.
Actual Behavior The search results are returned in the order of their insertion into the index, without any apparent sorting based on the query relevance.
Code to Reproduce
import FlexSearch from 'flexsearch';
const data = [
'cats abcd efgh ijkl mnop qrst uvwx cute',
'cats abcd efgh ijkl mnop qrst cute',
'cats abcd efgh ijkl mnop cute',
'cats abcd efgh ijkl cute',
'cats abcd efgh cute',
'cats abcd cute',
'cats cute',
];
const index = new FlexSearch.Index({preset: 'match', tokenize: "forward"});
data.forEach((item, id) => {
index.add(id, item);
});
index.search('cats cute').forEach(i => {
console.log(data[i]);
});
Output
cats abcd efgh ijkl mnop qrst uvwx cute
cats abcd efgh ijkl mnop qrst cute
cats abcd efgh ijkl mnop cute
cats abcd efgh ijkl cute
cats abcd efgh cute
cats abcd cute
cats cute
Additional Information This behavior may lead to inefficiencies and unexpected results in applications relying on relevance-based sorting for search functionality.