fergiemcdowall / search-index

A persistent, network resilient, full text search library for the browser and Node.js
MIT License
1.38k stars 149 forks source link

Not getting the asynchronus part working? #512

Closed eklem closed 3 years ago

eklem commented 3 years ago

I'm not so good at async stuff, so wondering if I could get a little help. First I was trying the quick start example, but didn't get that running. Then I tried examples/SEARCH.js

(async () => {

  const si = require('search-index') // search-index is a dependency in package.json
  const db = await si({ name: 'nodeQuickstart' })

  await db.PUT([
    {
      _id: 1,
      bandName: 'The Beatles',
      description: 'The nice boys of pop'
    }, {
      _id: 'two',
      bandName: 'The Rolling Stones',
      description: 'The bad boys of rock'
    }, {
      _id: 3,
      bandName: 'The Who',
      description: 'Nearly as good as Led Zeppelin'
    }
  ])

  console.log('\nSEARCH-ing ->')
  await db.read(
    {
      SEARCH: ['The']
    }
  ).then(console.log)

  console.log('\nSEARCH-ing ->')
  await db.read(
    {
      SEARCH: ['The', 'Beatles']
    }
  ).then(console.log)

  console.log('\nSEARCH-ing with negation ->')
  await db.read(
    {
      NOT: {
        include: {SEARCH: ['The']},
        exclude: {SEARCH: ['Beatles']}
      }
    }
  ).then(console.log)

})()

error message:

node index.js

SEARCH-ing ->
(node:73051) UnhandledPromiseRejectionWarning: TypeError: db.read is not a function
    at /Users/eklem/github_modules/idx-tests/siv3/index.js:47:12
(Use `node --trace-warnings ...` to show where the warning was created)
(node:73051) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:73051) [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.
fergiemcdowall commented 3 years ago

Hi Espen- thanks for pointing this out- the examples folder is out of date and should be removed (or updated). The read function has been removed, and normally you would use QUERY instead

A complete quick start would look something like this:

const si = require('search-index')

const run = async () => {

  // initialize an index
  const { PUT, QUERY } = await si()

  // add documents to the index
  await PUT([{
    _id: '1',
    text: 'the first document'
  }, {
    _id: '2',
    text: 'the second document'
  }])

  // read documents from the index, and send to console.log
  const results = await QUERY('document').then(console.log)

}

run()
eklem commented 3 years ago

Thanks, working! I think the examples are a fantastic resource, so I can see if I get them working, keeping the intention intact.

Also saw that include is now INCLUDE and exclude is now EXCLUDE in the last NOT-type query.