From 19eea428b8ea3fc26d1a8c1571dd5a3b0767d23e Mon Sep 17 00:00:00 2001 From: Etienne Lemay Date: Thu, 7 Jul 2016 16:29:04 -0400 Subject: [PATCH] Manually tokenize search terms and emoji indexes --- src/components/search.js | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/components/search.js b/src/components/search.js index c212f33..22d2cc2 100644 --- a/src/components/search.js +++ b/src/components/search.js @@ -11,27 +11,41 @@ export default class Search extends React.Component { buildIndex() { this.index = lunr(function() { + this.pipeline.reset() + this.field('short_name', { boost: 2 }) this.field('name') - this.ref('short_name') + this.ref('id') }) for (let emoji in data.emojis) { let emojiData = data.emojis[emoji], { short_name, name } = emojiData - this.index.add({ short_name, name }) + this.index.add({ + id: short_name, + short_name: this.tokenize(short_name), + name: this.tokenize(name), + }) } } + tokenize (string) { + if (['-', '-1', '+', '+1'].indexOf(string) == 0) { + return string.split('') + } + + return string.split(/[-|_|\s]+/) + } + handleChange() { var { input } = this.refs, value = input.value, results = null if (value.length) { - results = this.index.search(value).map((result) => + results = this.index.search(this.tokenize(value)).map((result) => result.ref ) }