config-hash.asciidoc 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. [discrete]
  2. [[config-hash]]
  3. === Building the client from a configuration hash
  4. To help ease automated building of the client, all configurations can be
  5. provided in a setting hash instead of calling the individual methods directly.
  6. This functionality is exposed through the `ClientBuilder::fromConfig()` static
  7. method, which accepts an array of configurations and returns a fully built
  8. client.
  9. Array keys correspond to the method name, for example `retries` key corresponds
  10. to `setRetries()` method.
  11. [source,php]
  12. ----
  13. $params = [
  14. 'hosts' => [
  15. 'localhost:9200'
  16. ],
  17. 'retries' => 2
  18. ];
  19. $client = ClientBuilder::fromConfig($params);
  20. ----
  21. Unknown parameters throw an exception, to help the user find potential problems.
  22. If this behavior is not desired (for example, you are using the hash for other
  23. purposes), you can set `$quiet = true` in fromConfig() to silence the exceptions.
  24. [source,php]
  25. ----
  26. $params = [
  27. 'hosts' => [
  28. 'localhost:9200'
  29. ],
  30. 'retries' => 2,
  31. 'imNotReal' => 5
  32. ];
  33. // Set $quiet to true to ignore the unknown `imNotReal` key
  34. $client = ClientBuilder::fromConfig($params, true);
  35. ----