nextapps-de / flexsearch

Next-Generation full text search library for Browser and Node.js
Apache License 2.0
12.54k stars 492 forks source link

How to use encode with Worker #376

Open vincesp opened 1 year ago

vincesp commented 1 year ago
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.