nextapps-de / flexsearch

Next-Generation full text search library for Browser and Node.js
Apache License 2.0
12.53k stars 491 forks source link

Results Not Sorted by Relevance for Simple Queries Using match Preset #448

Open ChandraNakka opened 3 months ago

ChandraNakka commented 3 months ago

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

  1. Initialize a FlexSearch index with the match preset and forward tokenization.
  2. Add a series of strings to the index, each progressively shorter and varying by the inclusion of additional words.
  3. 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.