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

Initialize lunr using a promise rather than a function? #497

Open
elega9t opened this issue Apr 13, 2021 · 2 comments
Open

Initialize lunr using a promise rather than a function? #497

elega9t opened this issue Apr 13, 2021 · 2 comments

Comments

@elega9t
Copy link

elega9t commented Apr 13, 2021

Hi,

Is there a way to pass in a promise to initialize lunr instead of a function?

I want to do a non-blocking stream read to load data for lunr to index, a promise would allow me to keep things async. Any suggestions?

Thanks

@solyarisoftware
Copy link

solyarisoftware commented Jul 5, 2021

Hi

Is there a way to pass in a promise to initialize lunr instead of a function?

BTW this is not an issue IMMO.

just wrap into a Promise the lunr() index creation:

function createLunrIndex(documents,fieldNames) {
    return new Promise( (resolve,reject) => {
       
      const idx = lunr(function() {
        //
        // load documents
        // set fields
        // ...
      })
       
       resolve(idx)
    });
}

const idx = await createLunrIndex(documents,fieldNames, ...) 

I want to do a non-blocking stream read to load data for lunr to index,

Do you want to add documents dynamically after the index creation?
This is maybe not possible, see: https://stackoverflow.com/a/44086343/1786393

So I fair you have to recreated the index when you have new documents to add.

Maybe that's a change request, not an issue.

@regnete
Copy link

regnete commented Sep 5, 2023

async function createIndex(): Promise<lunr.Index>{

    const builder = new lunr.Builder();
    builder.pipeline.add(
        lunr.trimmer,
        lunr.stopWordFilter,
        lunr.stemmer
    )
    builder.searchPipeline.add(
        lunr.stemmer
    )
    
    builder.ref('id');
    builder.field('keywords');

    await addObjectsToIndex();

    return builder.build();
}

async function addObjectsToIndex():Promise<void>{
   // do async stuff here
}

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

3 participants