What is the correct way of handling client in Elasticsearch?

is creating and closing client in each functions the correct way ? Let's take we have 3 functions createIndex(), createMapping(), importDataToES().

           initClientObject()
                     createIndex()
           client.close()
           //method 2
           initClientObject()
                     createMapping()
           client.close()
           //method3
           initClientObject()
                     importDataToES()
           client.close()

No. Create one single client instance for your VM. It's threadsafe.

Will there be a performance issue, if we keep the connection open for long time? Because in this case can create connection(i.e client) when we start the application and can destroy it when we shutdown the application.

People are doing that for years now with the Java client.

Yes, create a client when the application starts and close it when the application stops.

Thanks for the reply.. Let us check