olivernn / lunr.js

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

Sub-fields support? #442

Closed sustained closed 4 years ago

sustained commented 4 years ago

Fuse.js allows us to do stuff like this:

{
  keys: [
    {
      name: 'title',
      weight: 1.0,
    },
    {
      name: 'body',
      weight: 0.75,
    },
    {
      name: 'user.login',
      weight: 0.3,
    },
    {
      name: 'labels.name',
      weight: 0.3,
    },
  ],
}

...which is really nice.

AFAICT from the docs/guide, here I would need to flatten the data, right?

But how would that work with e.g. my labels?

Thank you!

olivernn commented 4 years ago

A function can be used to 'extract' a field from a document, this is specified when configuring the index, e.g.


var fooFieldExtractor = function (doc) {
  return doc.foo.bar.baz
}

lunr(function () {
  this.field("foo", extractor: fooFieldExtractor)
})

You have access to the document in the extractor function so can do whatever you need to get the right value to index.

https://lunrjs.com/docs/lunr.Builder.html#field