Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding per field tokenizer #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ lunr.Index.load = function (serialisedData) {
*/
lunr.Index.prototype.field = function (fieldName, opts) {
var opts = opts || {},
field = { name: fieldName, boost: opts.boost || 1 }
field = { name: fieldName, boost: opts.boost || 1, tokenizer: opts.tokenizer || null }

this._fields.push(field)
return this
Expand Down Expand Up @@ -145,7 +145,10 @@ lunr.Index.prototype.add = function (doc, emitEvent) {
emitEvent = emitEvent === undefined ? true : emitEvent

this._fields.forEach(function (field) {
var fieldTokens = this.pipeline.run(lunr.tokenizer(doc[field.name]))
var tokenizer = null;
if(!field.tokenizer) tokenizer = lunr.tokenizer
else tokenizer = field.tokenizer;
var fieldTokens = this.pipeline.run(tokenizer(doc[field.name]))

docTokens[field.name] = fieldTokens
lunr.SortedSet.prototype.add.apply(allDocumentTokens, fieldTokens)
Expand Down
2 changes: 1 addition & 1 deletion lib/lunr.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* @returns {lunr.Index}
*
*/
var lunr = function (config) {
window.lunr = function (config) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How will this affect using lunr as an npm module? Might need a check around whether window exists before assigning lunr to it.

var idx = new lunr.Index

idx.pipeline.add(lunr.stopWordFilter, lunr.stemmer)
Expand Down
9 changes: 6 additions & 3 deletions lunr.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* @returns {lunr.Index}
*
*/
var lunr = function (config) {
window.lunr = function (config) {
var idx = new lunr.Index

idx.pipeline.add(lunr.stopWordFilter, lunr.stemmer)
Expand Down Expand Up @@ -838,7 +838,7 @@ lunr.Index.load = function (serialisedData) {
*/
lunr.Index.prototype.field = function (fieldName, opts) {
var opts = opts || {},
field = { name: fieldName, boost: opts.boost || 1 }
field = { name: fieldName, boost: opts.boost || 1, tokenizer: opts.tokenizer || null }

this._fields.push(field)
return this
Expand Down Expand Up @@ -884,7 +884,10 @@ lunr.Index.prototype.add = function (doc, emitEvent) {
emitEvent = emitEvent === undefined ? true : emitEvent

this._fields.forEach(function (field) {
var fieldTokens = this.pipeline.run(lunr.tokenizer(doc[field.name]))
var tokenizer = null;
if(!field.tokenizer) tokenizer = lunr.tokenizer
else tokenizer = field.tokenizer;
var fieldTokens = this.pipeline.run(tokenizer(doc[field.name]))

docTokens[field.name] = fieldTokens
lunr.SortedSet.prototype.add.apply(allDocumentTokens, fieldTokens)
Expand Down
Loading