0

I was checking a part of my application in which I connect to elasticsearch host server and then I realized for every time the front-end sends an report request to my back-end I'm creating an instance of elasticsearch client class using the following code :

$elasticClient = ClientBuilder::create()->setHosts($this->setHostsParams())->build();

Since our application sends about 20 requests to the back-end by loading the first page, I was considering if PHP's elasticsearch library might be capable of optimizing the initiation phase, or if anyone has a better solution for this, or it might not be a big of a deal after all and it's not a real overhead!?

PS : I did some research with it and didn't find any resources covering this subject.

Armin Abbasi
  • 123
  • 1
  • 7
  • You could either use connection pool (take a look to swoole) or send everything in 1 request instead of 20. – Sanan Guliyev Sep 17 '20 at 20:34
  • on my side I always use it in a singleton to avoid recreating a separate connection each time I need the client. – Jaycreation Sep 18 '20 at 06:07
  • Keep it in mind that in PHP a singleton will be created with a new request and destructed as soon as the request is served, what I'm dealing with is different forms of elasticsearch aggregations in different API endpoints. – Armin Abbasi Sep 18 '20 at 09:24

1 Answers1

1

Sharing an object instance is already discussed here and elsewhere so I'm not going to go into that.

What I'd point out, though, is there there's an elasticsearch API called _msearch which enables you to send multiple search payloads at the same time and the system will respond after all the individual requests have resolved. Here's some sample PHP usage.

This might be useful if you need all your ~20 requests resolved at once -- though it may be useless if you defer some of those requests only after, say, a user scrolls down and what not.

Joe - GMapsBook.com
  • 15,787
  • 4
  • 23
  • 68