Skip to content

Elasticsearch with the Java API client

Uses elasticsearch-java 9.5. The code is an ordinary application for the original database; nothing in it knows about MachNoSQL.

https://es.example.com:9200
https://machnosql.example.com:9200

The address comes from the environment variable ELASTICSEARCH_URL, so switching to MachNoSQL changes configuration only.

Terminal window
export ELASTICSEARCH_URL='https://machnosql.example.com:9200'
mvn -q compile exec:java

Credentials come from ELASTICSEARCH_USERNAME and ELASTICSEARCH_PASSWORD.

The program as it is kept and tested in the Mach Database NoSQL repository; highlighted lines read the connection setting.

src/main/java/example/App.java
// Product search on Elasticsearch, unchanged on MachNoSQL.
package example;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.Refresh;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.core.search.Hit;
import java.io.IOException;
import java.util.List;
public final class App {
public record Product(String name, String brand, double price) {}
public static void main(String[] args) throws IOException {
try (ElasticsearchClient es = ElasticsearchClient.of(b -> b
.host(System.getenv("ELASTICSEARCH_URL"))
.usernameAndPassword(System.getenv("ELASTICSEARCH_USERNAME"), System.getenv("ELASTICSEARCH_PASSWORD")))) {
if (!es.indices().exists(e -> e.index("products")).value()) {
es.indices().create(c -> c.index("products").mappings(m -> m
.properties("name", p -> p.text(t -> t))
.properties("brand", p -> p.keyword(k -> k))
.properties("price", p -> p.float_(f -> f))));
}
List<Product> products = List.of(
new Product("mechanical keyboard", "keyco", 89.0),
new Product("wireless keyboard", "typer", 49.0),
new Product("gaming mouse", "keyco", 39.0));
for (int i = 0; i < products.size(); i++) {
String id = String.valueOf(i + 1);
Product product = products.get(i);
es.index(x -> x.index("products").id(id).document(product).refresh(Refresh.True));
}
SearchResponse<Product> found = es.search(s -> s.index("products")
.query(q -> q.match(m -> m.field("name").query("keyboard")))
.aggregations("brands", a -> a.terms(t -> t.field("brand"))), Product.class);
for (Hit<Product> hit : found.hits().hits()) {
System.out.println(hit.score() + " " + hit.source().name());
}
System.out.println(found.aggregations().get("brands").sterms().buckets().array());
}
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.machnosql</groupId>
<artifactId>elasticsearch-example</artifactId>
<version>1.0</version>
<properties>
<maven.compiler.release>21</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<exec.mainClass>example.App</exec.mainClass>
</properties>
<dependencies>
<dependency>
<groupId>co.elastic.clients</groupId>
<artifactId>elasticsearch-java</artifactId>
<version>9.5.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.22.4</version>
</dependency>
</dependencies>
</project>