flexsearch icon indicating copy to clipboard operation
flexsearch copied to clipboard

How to use encode with Worker

Open vincesp opened this issue 1 year ago • 0 comments

const indexWorker = new Worker({
  tokenize: 'full',
  worker: '/flexsearch/worker/worker.js',
  encode: (str) => str.toLowerCase().split(/[\p{Z}\p{S}\p{P}\p{C}]+/u),
})

I am using flexsearch in the browser. With above configuration, I get an error:

TypeError: this.encode is not a function at Index.add

encode is converted to a string to post it to the worker. But in the worker, it is not converted back properly.

https://github.com/nextapps-de/flexsearch/blob/master/src/worker/index.js#L25 https://github.com/nextapps-de/flexsearch/blob/master/src/worker/handler.js#L22

This code also would not work:

const indexWorker = new Worker({
  tokenize: 'full',
  worker: '/flexsearch/worker/worker.js',
  encode(str) {
    return str.toLowerCase().split(/[\p{Z}\p{S}\p{P}\p{C}]+/u)
  },
})

To make the code work, it has to be changed to:

const indexWorker = new Worker({
  tokenize: 'full',
  worker: '/flexsearch/worker/worker.js',
  encode: function (str) {
    return str.toLowerCase().split(/[\p{Z}\p{S}\p{P}\p{C}]+/u)
  },
})

Proposal: make the code in worker/handler.js more robust so that it can handle any kind of function passed into encode.

vincesp avatar Feb 28 '23 13:02 vincesp