olivernn / lunr.js

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

Can refs have spaces #374

Closed rlingineni closed 5 years ago

rlingineni commented 5 years ago

I am building an autocomplete index for my search data, and I want the titles back, not necessarily the id.

A sample document:

{           
    "id":"789102",
    "title": "Get Out",
    "year": 2017,
    "director": "Jordan Peele",
    "genre": "Suspense",
    "tldr": "Suspense movie that keeps you on the edge"
}

My index has the ref as title.

Here is the query function I am using for autocomplete:

let results = index.query(function() {

    //i tried escaping spaces to no avail
    //query = query.replace(" ","\\ ");

    // exact matches should have the highest boost
    this.term(query, { boost: 100 });

    // prefix matches should be boosted slightly
    this.term(query, { boost: 10, usePipeline: false, wildcard: lunr.Query.wildcard.TRAILING });

    // finally, try a fuzzy search with character 2, without any boost
    this.term(query, { boost: 5, usePipeline: false, editDistance: 3 });
});

Performing this.query('get out') yields no results, however, this.query('get') yields the proper result set. Not sure if this is because of case sensitivity, or something I am missing for the exact match case.

However, if I use the search function instead of query, it does give me an exact match.

Is it because when indexing, the ref is split up and spaces are ignored?

rlingineni commented 5 years ago

using the tokenizer for the query seems to have fixed it. Hopefully, it doesn't have too much of a performance impact.

// exact matches should have the highest boost
this.term(lunr.tokenizer(query), { boost: 100 });