NodeJS initIndex

Hello, I have an doubt about what’s the best way to use initIndex in NodeJS server.

const index = algoliaSearch.initIndex(indexName);

The options are create a new instance of index on every request or create global index object and reuse this.

After analize the code the only diference I found is that create a new index for every request creates an index object with a clean cache, but this can be reached in the global object setting the _useCache option to false

So, what’s the right way to use initIndex? and why?

(Personally I think the right way is use a global object with _useCache setted to false)

Hi @ivanalemunioz,

Welcome to the Algolia community!

I understand you’d like to learn more about using the initIndex method with NodeJs.

The initIndex method chooses which Algolia index to use. If the index does not exist it will create a new one. There is no need to create a new one each time.

The current documentation for the initIndex function can be found here.

Can you send me a link to the documentation where you found this information about --useCache?

I look forward to your reply!

Thank you for answer!

I not found documentation about _useCache, I found this paramether looking into the internal code.

So you say there is no difference between use a global index object or a new one for each request?

Hi @ivanalemunioz,

If you are using our API, I don’t think you’ll have that option.

Are you using our JS API? If so, then you should just use the index name where you want your objects stored. Unless you have a specific use case that would require it, you shouldn’t need to make a new one each time. And you shouldn’t need any parameters other than those mentioned in our docs for the initIndex found here.

I hope that helps!