Skip to content

Instantly share code, notes, and snippets.

@olivernn olivernn/icon_search.js
Last active Nov 8, 2015

Embed
What would you like to do?
// Assuming a 'document' strucutre like below to represent a single icon
{
"id": "some-unique-id",
"tags": ["tag1", "tag2", "tag3"],
"title": "My Awesome Icon"
}
// You can then set up an index with the following, I put a boost on the tags, but it depends on your data
var idx = lunr(function () {
this.ref('id')
this.field('tags', { "boost": 10 })
this.field('title')
})
idx.search("tag1")

When you define fields, they are usually strings, but it is also possible to use an Array (or anything else that can be meaningfully turned into a string).

So you could have a field called tags that is just an array, this will then be indexed correctly and searchable.

I don't know what metadata you have, but you could have multiple fields like this, perhaps a field for colour or type, you can then boost them in a way that makes sense for your data.

You may want to customise the processing pipeline, again it depends on your data, but stop word filtering or stemming might not be required if your data is not full text, you would have to experiment.

@markvital

This comment has been minimized.

Copy link

commented Nov 8, 2015

Hey,
Thank a lot for the manual. I've created quick prototype: http://fnf.vc/iconSearch
I have 2 questions:

  1. Do you know how to integrate autocomplete with Lunr? Something like Twitter typeahead?
  2. Can I prioritize some specific documents over others, similar to 'boost' that prioritizes a specific field. For ex. I want baby icon to always be first, no matter what you search for ex. "kid", "baby", "child" .

screen shot 2015-11-07 at 10 14 45 pm

var icons = [{
    id: 1,
    title: “baby",
    tags: “baby, kid, child,”,
    boost: 10
},{
    id: 2,
    title: "man shopping with child",
    tags: "shopping, grocery, shopping with kid, shopping with child, shopping with baby”
}

Also can you prioritize a separate tag somehow?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.