Skip to content

Commit a1b3d3f

Browse files
committed
Test aggregation with SQL.
Signed-off-by: Youssef Aouichaoui <[email protected]>
1 parent ec77e35 commit a1b3d3f

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/main/java/org/springframework/data/elasticsearch/core/sql/SqlResponse.java

+5
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ public Iterator<Map.Entry<Column, JsonValue>> iterator() {
116116
return row.entrySet().iterator();
117117
}
118118

119+
@Nullable
120+
public JsonValue get(Column column) {
121+
return row.get(column);
122+
}
123+
119124
/**
120125
* This method should attempt to convert the SQL response into a document if the columns selected in an SQL query
121126
* correspond to the fields in the document.

src/test/java/org/springframework/data/elasticsearch/core/sql/SqlOperationsIntegrationTests.java

+15
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.elasticsearch.core.sql;
1717

18+
import static org.assertj.core.api.Assertions.assertThat;
1819
import static org.junit.jupiter.api.Assertions.assertEquals;
1920
import static org.junit.jupiter.api.Assertions.assertFalse;
2021
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -100,6 +101,20 @@ void when_search_with_an_sql_query_and_txt_format() {
100101
"The Elasticsearch Java Client only supports JSON format.");
101102
}
102103

104+
@Test
105+
void when_search_with_an_sql_query_that_has_aggregated_column() {
106+
// Given
107+
SqlQuery query = SqlQuery.builder("SELECT SUM(views) AS TOTAL FROM entity_for_sql").build();
108+
109+
// When
110+
111+
// Then
112+
SqlResponse response = operations.search(query);
113+
assertThat(response.getColumns()).first().extracting(SqlResponse.Column::name).isEqualTo("TOTAL");
114+
assertThat(response.getRows()).hasSize(1).first().extracting(row -> row.get(response.getColumns().get(0)))
115+
.hasToString("3");
116+
}
117+
103118
// begin region
104119
@Document(indexName = "entity_for_sql")
105120
static class EntityForSQL {

0 commit comments

Comments
 (0)