use of org.elasticsearch.client.Request in project immutables by immutables.
the class IndexOps method mapping.
/**
* Return mapping for current index
*/
Single<Mapping> mapping() {
final String uri = String.format(Locale.ROOT, "/%s/_mapping", index);
final Request request = new Request("GET", uri);
return transport.execute(request).map(response -> mapper.readValue(response.getEntity().getContent(), ObjectNode.class)).map(root -> {
ObjectNode properties = (ObjectNode) root.elements().next().get("mappings");
if (properties == null) {
throw new IllegalStateException(String.format("No mappings found for index %s (after request %s)", index, request));
}
ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
Json.visitMappingProperties(properties, builder::put);
return Mapping.ofElastic(builder.build());
});
}
use of org.elasticsearch.client.Request in project immutables by immutables.
the class IndexOps method exists.
Single<Boolean> exists() {
final String uri = String.format(Locale.ROOT, "/%s/_mapping", index);
final Request request = new Request("GET", uri);
return transport.execute(request).map(x -> true).onErrorResumeNext(e -> Single.just(false));
}
use of org.elasticsearch.client.Request in project immutables by immutables.
the class IndexOps method create.
/**
* Creates index in elastic search given a mapping. Mapping can contain nested fields expressed
* as dots({@code .}).
*
* <p>Example
* <pre>
* {@code
* b.a: long
* b.b: keyword
* }
* </pre>
*
* @param mapping field and field type mapping
* @throws IOException if there is an error
*/
Completable create(Map<String, String> mapping) {
Objects.requireNonNull(mapping, "mapping");
ObjectNode mappings = mapper.createObjectNode();
ObjectNode properties = mappings.with("mappings").with("properties");
for (Map.Entry<String, String> entry : mapping.entrySet()) {
applyMapping(properties, entry.getKey(), entry.getValue());
}
// create index and mapping
try {
final HttpEntity entity = new StringEntity(mapper.writeValueAsString(mappings), ContentType.APPLICATION_JSON);
final Request r = new Request("PUT", "/" + index);
r.setEntity(entity);
return transport.execute(r).ignoreElement();
} catch (JsonProcessingException e) {
throw new UncheckedIOException(e);
}
}
use of org.elasticsearch.client.Request in project immutables by immutables.
the class ElasticsearchOps method insertBulkInternal.
private Single<WriteResult> insertBulkInternal(List<ObjectNode> documents) throws JsonProcessingException {
Objects.requireNonNull(documents, "documents");
if (documents.isEmpty()) {
// nothing to process
return Single.just(WriteResult.empty());
}
final List<String> bulk = new ArrayList<>(documents.size() * 2);
for (ObjectNode doc : documents) {
final ObjectNode header = mapper.createObjectNode();
header.with("index").put("_index", index);
if (doc.has("_id")) {
// check if document has already an _id
header.with("index").set("_id", doc.get("_id"));
doc.remove("_id");
}
bulk.add(header.toString());
bulk.add(mapper().writeValueAsString(doc));
}
final StringEntity entity = new StringEntity(String.join("\n", bulk) + "\n", ContentType.APPLICATION_JSON);
final Request r = new Request("POST", "/_bulk?refresh");
r.setEntity(entity);
return transport.execute(r).map(x -> WriteResult.unknown());
}
use of org.elasticsearch.client.Request in project graylog2-server by Graylog2.
the class ClusterAdapterES7 method clusterStats.
@Override
public ClusterStats clusterStats() {
final Request request = new Request("GET", "/_cluster/stats/nodes/*");
final JsonNode clusterStatsResponseJson = jsonApi.perform(request, "Couldn't read Elasticsearch cluster stats");
final String clusterName = clusterStatsResponseJson.path("cluster_name").asText();
String clusterVersion = null;
if (clusterStatsResponseJson.path("nodes").path("versions").isArray()) {
final ArrayNode versions = (ArrayNode) clusterStatsResponseJson.path("nodes").path("versions");
// We just use the first version in the "versions" array. This is not correct if there are different
// versions running in the cluster, but that is not recommended anyway.
final JsonNode versionNode = versions.path(0);
if (versionNode.getNodeType() != JsonNodeType.MISSING) {
clusterVersion = versionNode.asText();
}
}
final JsonNode countStats = clusterStatsResponseJson.path("nodes").path("count");
final NodesStats nodesStats = NodesStats.create(countStats.path("total").asInt(-1), countStats.path("master_only").asInt(-1), countStats.path("data_only").asInt(-1), countStats.path("master_data").asInt(-1), countStats.path("client").asInt(-1));
final JsonNode clusterIndicesStats = clusterStatsResponseJson.path("indices");
final IndicesStats indicesStats = IndicesStats.create(clusterIndicesStats.path("count").asInt(-1), clusterIndicesStats.path("store").path("size_in_bytes").asLong(-1L), clusterIndicesStats.path("fielddata").path("memory_size_in_bytes").asLong(-1L));
return ClusterStats.create(clusterName, clusterVersion, nodesStats, indicesStats);
}
Aggregations