Skip to content
This repository has been archived by the owner on Nov 18, 2022. It is now read-only.

Elasticsearch 7 rest client #58

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 20 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ release.properties
*.iws
.idea/
out/
build/
**/buildOutputCleanup/
purplesword marked this conversation as resolved.
Show resolved Hide resolved

# Eclipse
.settings/
Expand Down
37 changes: 9 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ Dropwizard Elasticsearch
[![Coverage Status](https://img.shields.io/coveralls/dropwizard/dropwizard-elasticsearch.svg)](https://coveralls.io/r/dropwizard/dropwizard-elasticsearch)
[![Maven Central](https://img.shields.io/maven-central/v/io.dropwizard.modules/dropwizard-elasticsearch.svg)](http://mvnrepository.com/artifact/io.dropwizard.modules/dropwizard-elasticsearch)

A set of classes for using [Elasticsearch][1] (version 2.3.0 and higher) in a [Dropwizard][2] application.
A set of classes for using [Elasticsearch][1] (version 7.1.0 and higher) in a [Dropwizard][2] application.

The package provides a [lifecycle-managed][3] client class (`ManagedEsClient`), a configuration class with the most
common options (`EsConfiguration`), and some [health checks][4] which can instantly be used in any Dropwizard application.

[1]: http://www.elasticsearch.org/
[2]: http://dropwizard.io/1.2.0/docs
[3]: http://dropwizard.io/1.2.0/docs/manual/core.html#managed-objects
[4]: http://dropwizard.io/1.2.0/docs/manual/core.html#health-checks
[2]: http://dropwizard.io/1.3.0/docs
[3]: http://dropwizard.io/1.3.0/docs/manual/core.html#managed-objects
[4]: http://dropwizard.io/1.3.0/docs/manual/core.html#health-checks


Usage
Expand Down Expand Up @@ -43,33 +43,14 @@ Configuration

The following configuration settings are supported by `EsConfiguration`:

* `nodeClient`: When `true`, `ManagedEsClient` will create a `NodeClient`, otherwise a `TransportClient`; default: `true`
* `servers`: A list of servers for usage with the created TransportClient if `nodeClient` is `false`
* `clusterName`: The name of the Elasticsearch cluster; default: "elasticsearch"
* `settings`: Any additional settings for Elasticsearch, see [Configuration](https://www.elastic.co/guide/en/elasticsearch/reference/2.4/setup-configuration.html)
* `settingsFile`: Any additional settings file for Elasticsearch, see [Configuration](https://www.elastic.co/guide/en/elasticsearch/reference/2.4/setup-configuration.html)

An example configuration file for creating a Node Client could like this:
An example configuration file for creating a High Level Rest Client could like this:

clusterName: MyClusterName
settings:
node.name: MyCustomNodeName

The order of precedence is: `nodeClient`/`servers`/`clusterName` > `settings` > `settingsFile`, meaning that
any setting in `settingsFile` can be overwritten with `settings` which in turn get overwritten by the specific settings
like `clusterName`.

Maven Artifacts
---------------

This project is available on Maven Central. To add it to your project simply add the following dependencies to your
`pom.xml`:

<dependency>
<groupId>io.dropwizard.modules</groupId>
<artifactId>dropwizard-elasticsearch</artifactId>
<version>1.2.0-1</version>
</dependency>
servers:
- http://127.0.0.1:9200
- http://127.0.0.1:9201
- http://127.0.0.1:9202


Support
Expand Down
10 changes: 8 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<groupId>io.dropwizard.modules</groupId>
<artifactId>dropwizard-elasticsearch</artifactId>
<version>1.2.0-2-SNAPSHOT</version>
<version>1.3.0-1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Dropwizard Elasticsearch Bundle</name>
Expand Down Expand Up @@ -69,7 +69,7 @@
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<dropwizard.version>1.3.11</dropwizard.version>
<elasticsearch.version>2.4.6</elasticsearch.version>
<elasticsearch.version>7.1.0</elasticsearch.version>
</properties>

<dependencyManagement>
Expand All @@ -94,6 +94,12 @@
<artifactId>elasticsearch</artifactId>
purplesword marked this conversation as resolved.
Show resolved Hide resolved
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.1.0</version>
purplesword marked this conversation as resolved.
Show resolved Hide resolved
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,61 +1,37 @@
package io.dropwizard.elasticsearch.config;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.net.HostAndPort;
import io.dropwizard.validation.ValidationMethod;
import org.hibernate.validator.constraints.NotEmpty;

import javax.validation.constraints.NotNull;
import org.apache.http.HttpHost;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.validation.constraints.NotNull;

/**
* Configuration class for Elasticsearch related settings.
*/
public class EsConfiguration {
@JsonProperty
@NotNull
private List<HostAndPort> servers = Collections.emptyList();

@JsonProperty
@NotEmpty
private String clusterName = "elasticsearch";

@JsonProperty
private boolean nodeClient = true;

@JsonProperty
@NotNull
private Map<String, String> settings = Collections.emptyMap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a bunch more.. Not sure if did it properly.. Tests will be added soon.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added more tests now the coverage is higher and 100% on health checks.

Could you comment more about what do you think is necessary to be covered in configuration?

I added most of them listed in the documentation (but feel a bit hard to write test unless using testcontainer). Also I sort of feel it's a bit too complicate, maybe it's better to cover some simple common configs and then let sophisticated users do what they want with the ManagedClient(client,sniffer) constructor with their own client and sniffer..


@JsonProperty
private String settingsFile = null;
private List<String> servers = Collections.emptyList();

public List<HostAndPort> getServers() {
public List<String> getServers() {
return servers;
}

public String getClusterName() {
return clusterName;
}

public boolean isNodeClient() {
return nodeClient;
public List<HttpHost> getServersAsHttpHosts() {
ArrayList<HttpHost> httpHosts=new ArrayList<>();
getServers().forEach(hostAndPort -> {
HttpHost httpHost = HttpHost.create(hostAndPort);
if (httpHost.getPort() < 0) {
httpHost = new HttpHost(httpHost.getHostName(), 9200);
}
httpHosts.add(httpHost);
});
return httpHosts;
}

public Map<String, String> getSettings() {
return settings;
}

public String getSettingsFile() {
return settingsFile;
}

@ValidationMethod
@JsonIgnore
public boolean isValidConfig() {
return nodeClient || !servers.isEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package io.dropwizard.elasticsearch.health;

import com.codahale.metrics.health.HealthCheck;

import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.cluster.health.ClusterHealthStatus;

import static com.google.common.base.Preconditions.checkNotNull;
Expand All @@ -12,7 +17,7 @@
* @see <a href="http://www.elasticsearch.org/guide/reference/api/admin-cluster-health/">Admin Cluster Health</a>
*/
public class EsClusterHealthCheck extends HealthCheck {
private final Client client;
private final RestHighLevelClient client;
private final boolean failOnYellow;

/**
Expand All @@ -21,7 +26,7 @@ public class EsClusterHealthCheck extends HealthCheck {
* @param client an Elasticsearch {@link Client} instance connected to the cluster
* @param failOnYellow whether the health check should fail if the cluster health state is yellow
*/
public EsClusterHealthCheck(Client client, boolean failOnYellow) {
public EsClusterHealthCheck(RestHighLevelClient client, boolean failOnYellow) {
this.client = checkNotNull(client);
this.failOnYellow = failOnYellow;
}
Expand All @@ -30,9 +35,9 @@ public EsClusterHealthCheck(Client client, boolean failOnYellow) {
* Construct a new Elasticsearch cluster health check which will fail if the cluster health state is
* {@link ClusterHealthStatus#RED}.
*
* @param client an Elasticsearch {@link Client} instance connected to the cluster
* @param client an Elasticsearch {@link RestHighLevelClient} instance connected to the cluster
*/
public EsClusterHealthCheck(Client client) {
public EsClusterHealthCheck(RestHighLevelClient client) {
this(client, false);
}

Expand All @@ -47,7 +52,9 @@ public EsClusterHealthCheck(Client client) {
*/
@Override
protected Result check() throws Exception {
final ClusterHealthStatus status = client.admin().cluster().prepareHealth().get().getStatus();
ClusterHealthRequest request = new ClusterHealthRequest();
ClusterHealthResponse response = client.cluster().health(request, RequestOptions.DEFAULT);
final ClusterHealthStatus status = response.getStatus();

if (status == ClusterHealthStatus.RED || (failOnYellow && status == ClusterHealthStatus.YELLOW)) {
return Result.unhealthy("Last status: %s", status.name());
Expand Down

This file was deleted.

Loading