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

[stockholmux/redredisearch] Parallel createSearch #55

Open
clasen opened this issue Jan 23, 2020 · 0 comments
Open

[stockholmux/redredisearch] Parallel createSearch #55

clasen opened this issue Jan 23, 2020 · 0 comments

Comments

@clasen
Copy link

clasen commented Jan 23, 2020

PROJECT: https://github.com/stockholmux/redredisearch

This code:

var strs = [];
strs.push('Tobi wants four dollars');
strs.push('Tobi only wants $4');
strs.push('Loki is really fat');
strs.push('Loki, Jane, and Tobi are ferrets');
strs.push('Manny is a cat');
strs.push('Luna is a cat');
strs.push('Mustachio is a cat');

const key = 'pets'

redsearch.createSearch(key, {}, (err, search) => {
    search.remove("hola")
})

redsearch.createSearch(key, {}, (err, search) => {
  if (err) console.log(err)
  strs.forEach(function (str, i) { search.index(str, "hola"); });
});

redsearch.createSearch(key, {}, (err, search) => {
    search
    .query('cat* mus*')
    .type('direct')
    .end(function (err, ids) {
      if (err) throw err;
      ids.forEach(function (id) {
        console.log('  - %s', id);
      });
    });
})

Drop this error when the index is not created (the fisrt time):

ReplyError: Index already exists. Drop it first!
    at parseError (/srv/test/node_modules/redis-parser/lib/parser.js:193:12)
    at parseType (/srv/test/node_modules/redis-parser/lib/parser.js:303:14) {
  command: 'FT.CREATE',
  args: [ 'pets', 'SCHEMA', 'payload', 'text' ]
}

I found some workaround grouping the callbacks:

...
const key = 'pets'

let cbks = []
function create(key, cbk) {
    cbks.push(cbk)
    if (cbks.length > 1) return;

    redsearch.createSearch(key, {}, (err, search) => {
        if (err) console.log(err)
        for (let cll of cbks) {
            cll(search)
        }
        cbks = []
    })
}

create(key, (search) => {
    search.remove("hola")
})

create(key, (search) => {
    strs.forEach(function (str, i) { search.index(str, "hola"); });
})

create(key, (search) => {
    search
        .query('cat* mus*')
        .type('direct')
        .end(function (err, ids) {
            if (err) throw err;
            ids.forEach(function (id) {
                console.log('  - %s', id);
            });
        });
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant