Skip to content

Commit 235f9dd

Browse files
committed
Add named parameters with ES|QL
1 parent 6d8a440 commit 235f9dd

File tree

1 file changed

+49
-23
lines changed

1 file changed

+49
-23
lines changed

src/test/java/fr/pilato/test/elasticsearch/hlclient/EsClientIT.java

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -931,33 +931,59 @@ void esql() throws IOException, SQLException {
931931
| LIMIT 1
932932
""".replaceFirst("indexName", indexName);
933933

934-
// Using the Raw ES|QL API
935-
try (final BinaryResponse response = client.esql().query(q -> q.query(query)); InputStream is = response.content()) {
936-
// The response object is {"took":5,"columns":[{"name":"name","type":"text"}],"values":[["David"]]}
937-
final ObjectMapper mapper = new ObjectMapper();
938-
final JsonNode node = mapper.readTree(is);
939-
assertNotNull(node);
940-
assertEquals(3, node.size());
941-
assertEquals(1, node.get("columns").size());
942-
assertEquals("name", node.get("columns").get(0).get("name").asText());
943-
assertEquals(1, node.get("values").size());
944-
assertEquals("David", node.get("values").get(0).get(0).asText());
945-
assertTrue(node.get("took").asInt() > 0);
934+
{
935+
// Using the Raw ES|QL API
936+
try (final BinaryResponse response = client.esql().query(q -> q.query(query)); InputStream is = response.content()) {
937+
// The response object is {"took":5,"columns":[{"name":"name","type":"text"}],"values":[["David"]]}
938+
final ObjectMapper mapper = new ObjectMapper();
939+
final JsonNode node = mapper.readTree(is);
940+
assertNotNull(node);
941+
assertEquals(3, node.size());
942+
assertEquals(1, node.get("columns").size());
943+
assertEquals("name", node.get("columns").get(0).get("name").asText());
944+
assertEquals(1, node.get("values").size());
945+
assertEquals("David", node.get("values").get(0).get(0).asText());
946+
assertTrue(node.get("took").asInt() > 0);
947+
}
948+
}
949+
950+
{
951+
// Using the JDBC ResultSet ES|QL API
952+
try (final ResultSet resultSet = client.esql().query(ResultSetEsqlAdapter.INSTANCE, query)) {
953+
assertTrue(resultSet.next());
954+
assertEquals("David", resultSet.getString(1));
955+
} catch (final JsonpMappingException e) {
956+
// This is expected as we have this issue https://github.com/elastic/elasticsearch-java/pull/903
957+
}
946958
}
947959

948-
// Using the JDBC ResultSet ES|QL API
949-
try (final ResultSet resultSet = client.esql().query(ResultSetEsqlAdapter.INSTANCE, query)) {
950-
assertTrue(resultSet.next());
951-
assertEquals("David", resultSet.getString(1));
952-
} catch (final JsonpMappingException e) {
953-
// This is expected as we have this issue https://github.com/elastic/elasticsearch-java/pull/903
960+
{
961+
// Using the Object ES|QL API
962+
final Iterable<Person> persons = client.esql().query(ObjectsEsqlAdapter.of(Person.class), query);
963+
for (final Person person : persons) {
964+
assertNull(person.getId());
965+
assertNotNull(person.getName());
966+
}
954967
}
955968

956-
// Using the Object ES|QL API
957-
final Iterable<Person> persons = client.esql().query(ObjectsEsqlAdapter.of(Person.class), query);
958-
for (final Person person : persons) {
959-
assertNull(person.getId());
960-
assertNotNull(person.getName());
969+
{
970+
// Using named parameters
971+
String parametrizedQuery = """
972+
FROM indexName
973+
| WHERE name == ?name
974+
| KEEP name
975+
| LIMIT 1
976+
""".replaceFirst("indexName", indexName);
977+
978+
// Using the Object ES|QL API
979+
final Iterable<Person> persons = client.esql()
980+
.query(ObjectsEsqlAdapter.of(Person.class), parametrizedQuery,
981+
Map.of("name", "David")
982+
);
983+
for (final Person person : persons) {
984+
assertNull(person.getId());
985+
assertNotNull(person.getName());
986+
}
961987
}
962988
}
963989

0 commit comments

Comments
 (0)