@Bean("EsClient")
  public EsClient provide(Configuration config) {
    Settings.Builder esSettings = Settings.builder();

    // mandatory property defined by bootstrap process
    esSettings.put("cluster.name", config.get(CLUSTER_NAME.getKey()).get());

    boolean clusterEnabled = config.getBoolean(CLUSTER_ENABLED.getKey()).orElse(false);
    boolean searchNode = !clusterEnabled || SEARCH.equals(NodeType.parse(config.get(CLUSTER_NODE_TYPE.getKey()).orElse(null)));
    List<HttpHost> httpHosts;
    if (clusterEnabled && !searchNode) {
      httpHosts = getHttpHosts(config);

      LOGGER.info("Connected to remote Elasticsearch: [{}]", displayedAddresses(httpHosts));
    } else {
      // defaults provided in:
      // * in org.sonar.process.ProcessProperties.Property.SEARCH_HOST
      // * in org.sonar.process.ProcessProperties.Property.SEARCH_PORT
      HostAndPort host = HostAndPort.fromParts(config.get(SEARCH_HOST.getKey()).get(), config.getInt(SEARCH_PORT.getKey()).get());
      httpHosts = Collections.singletonList(toHttpHost(host, config));
      LOGGER.info("Connected to local Elasticsearch: [{}]", displayedAddresses(httpHosts));
    }

    return new EsClient(config.get(CLUSTER_SEARCH_PASSWORD.getKey()).orElse(null),
      config.get(CLUSTER_ES_HTTP_KEYSTORE.getKey()).orElse(null),
      config.get(CLUSTER_ES_HTTP_KEYSTORE_PASSWORD.getKey()).orElse(null),
      httpHosts.toArray(new HttpHost[0]));
  }
@Test
  public void es_client_provider_must_add_default_port_when_not_specified() {
    settings.setProperty(CLUSTER_ENABLED.getKey(), true);
    settings.setProperty(CLUSTER_NODE_TYPE.getKey(), "application");
    settings.setProperty(CLUSTER_SEARCH_HOSTS.getKey(), format("%s,%s:8081", localhostHostname, localhostHostname));

    EsClient client = underTest.provide(settings.asConfig());
    RestHighLevelClient nativeClient = client.nativeClient();
    assertThat(nativeClient.getLowLevelClient().getNodes()).hasSize(2);

    Node node = nativeClient.getLowLevelClient().getNodes().get(0);
    assertThat(node.getHost().getAddress().getHostName()).isEqualTo(localhostHostname);
    assertThat(node.getHost().getPort()).isEqualTo(9001);

    node = nativeClient.getLowLevelClient().getNodes().get(1);
    assertThat(node.getHost().getAddress().getHostName()).isEqualTo(localhostHostname);
    assertThat(node.getHost().getPort()).isEqualTo(8081);

    assertThat(logTester.logs(Level.INFO))
      .has(new Condition<>(s -> s.contains("Connected to remote Elasticsearch: [http://" + localhostHostname + ":9001, http://" + localhostHostname + ":8081]"), ""));
  }