nextapps-de / flexsearch

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

Unexpected exception when attempting to call Index.search method #40

Closed georgyfarniev closed 5 years ago

georgyfarniev commented 5 years ago

I tried to use code example from unit test, but got the following error:

Code to reproduce:

const FlexSearch = require('flexsearch')

// tslint:disable

;(async () => {
  const index = new FlexSearch({
    async: true,
    doc: {
      id: 'id',
      field: [ 'data:name' ]
    }
  })

  const data = [{
    id: 2,
    data: {
      title: 'Title 3',
      body: 'Body 3'
    }
  }, {
    id: 1,
    data: {
      title: 'Title 2',
      body: 'Body 2'
    }
  }, {
    id: 0,
    data: {
      title: 'Title 1',
      body: 'Body 1'
    }
  }]

  await index.add(data)

  console.log(index.search)

  const result = await index.search({
    field: 'data:body',
    query: 'body'
  })

  console.dir(result)
})()

Output:

[Function]
(node:10016) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'search' of undefined
    at h.search (C:\Users\User\Documents\Projects\test\node_modules\flexsearch\dist\flexsearch.node.js:24:281)
    at C:\Users\User\Documents\Projects\test\index.js:38:30
    at process._tickCallback (internal/process/next_tick.js:43:7)
    at Function.Module.runMain (internal/modules/cjs/loader.js:778:11)
    at startup (internal/bootstrap/node.js:300:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:826:3)
(node:10016) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:10016) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Environment: Node Node version: v11.2.0 Flexsearch version: "^0.5.2"

ts-thomas commented 5 years ago

Hello, thanks for the report. Your document descriptor did not define the index on the field data:body, that will solve it:

const index = new FlexSearch({
    async: true,
    doc: {
      id: 'id',
      field: [ 'data:body' ] // <---
    }
  })
georgyfarniev commented 5 years ago

@ts-thomas my apologies, I should pay enough attention to configuration. I think it could be nice to throw more meaningful error in that case. What do you think?

ts-thomas commented 5 years ago

That's true, I added this improvement to a milestone: https://github.com/nextapps-de/flexsearch/milestone/8