# 配置

几乎每一个方面的client 都是可以配置的。大多数用户只需要配置一些参数来满足他们的需求,但如果需要可以完全取代大部分的内部配置．

## 内联主机配置

最常见的配置是告诉客户端他们的集群:有多少个节点,他们的地址和端口。如果没有指定主机,客户端将尝试连接到`localhost:9200`

这种行为可以通过setHosts方法在`ClientBuilder()`方法接受一个数组值,每个条目对应的集群中的一个节点。主机的格式各不相同,具体取决于您的需求(ip和主机名、端口、ssl等)

```php
$hosts = [
    '192.168.1.1:9200',         // IP + Port
    '192.168.1.2',              // Just IP
    'mydomain.server.com:9201', // Domain + Port
    'mydomain2.server.com',     // Just Domain
    'https://localhost',        // SSL to localhost
    'https://192.168.1.3:9200'  // SSL to IP + Port
];
$client = ClientBuilder::create()           // Instantiate a new ClientBuilder
                    ->setHosts($hosts)      // Set the hosts
                    ->build();              // Build the client object
```

注意,ClientBuilder对象允许链式方法要求简洁。也可以单独调用的方法:

```php
$hosts = [
    '192.168.1.1:9200',         // IP + Port
    '192.168.1.2',              // Just IP
    'mydomain.server.com:9201', // Domain + Port
    'mydomain2.server.com',     // Just Domain
    'https://localhost',        // SSL to localhost
    'https://192.168.1.3:9200'  // SSL to IP + Port
];
$clientBuilder = ClientBuilder::create();   // Instantiate a new ClientBuilder
$clientBuilder->setHosts($hosts);           // Set the hosts
$client = $clientBuilder->build();          // Build the client object
```

## 扩展主机配置

client还支持扩展主机配置语法。内联方法依赖于PHP的配置使用`filter_var()`和`parse_url()`方法来验证和提取一个URL的组成部分。不幸的是,这些内置方法遇到问题与特定的边界情况。例如:`filter_var()`不会接受url包含下划线，同样的，`parse_url()`将无法解析如果一个基本的认证密码包含特殊字符串，比如`#` , `?`

出于这个原因,客户端支持延长主机语法提供更强的控制主机初始化。所有的组件验证,所以边界情况凸显在域名不会造成问题。

扩展语法的参数是一个数组为每个主机:

```php
$hosts = [
    // This is effectively equal to: "https://username:password!#$?*abc@foo.com:9200/"
    [
        'host' => 'foo.com',
        'port' => '9200',
        'scheme' => 'https',
        'user' => 'username',
        'pass' => 'password!#$?*abc'
    ],

    // This is equal to "http://localhost:9200/"
    [
        'host' => 'localhost',    // Only host is required
    ]
];
$client = ClientBuilder::create()           // Instantiate a new ClientBuilder
                    ->setHosts($hosts)      // Set the hosts
                    ->build();              // Build the client object
```

只需要主机参数。如果没有提供端口,默认端口为9200。默认的scheme方式是http。

## 授权和加密

对于HTTP授权和SSL加密的详细信息,请参阅[Authorization and SSL](https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/_security.html)．

## 设置重试

默认情况下,客户机将重试n次,其中n =您的集群的节点数量。重试只是执行如果操作结果在“hard”之外:拒绝连接,连接超时,DNS查询超时,等。4xx 和 5xx错误不考虑重试事件,由于节点返回一个操作响应。

如果你想禁用重试,或改变数量,可以与setRetries()方法:

.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://xiaoxiami.gitbook.io/elasticsearch/php-api/pei-zhi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
