olivernn / lunr.js

A bit like Solr, but much smaller and not as bright
http://lunrjs.com
MIT License
8.87k stars 546 forks source link

How to match result tokens in matchData with original query tokens #519

Open tmisirpash opened 2 years ago

tmisirpash commented 2 years ago

Suppose that I want I do a search like the following:

const searchResults = index.search('finance *man')

With position token metadata and the default stemmer, the matchData might look like:

matchData:
    metadata:
        financ:
            field_1: {position: Array(2)}
            field_2: {position: Array(3)}
        spiderman:
            field_3: {position: Array(5)}

For each of these returned matches, I would like to store the original query term that was responsible for the match. For instance, *man was the query token that resulted in a match with spiderman, but from the matchData alone you cannot get this information. Does anybody have a solution for this that doesn't involve post-processing the matchData with (potentially expensive) substring algorithms or rewriting the lunr.Index.prototype.query function?