Skip to content

Commit 4b421ff

Browse files
committed
fix macros for COUNT(*), update ElasticResponse adding optional SQL and updating results as rows
1 parent c39d9b9 commit 4b421ff

File tree

13 files changed

+284
-189
lines changed

13 files changed

+284
-189
lines changed

core/src/main/scala/app/softnetwork/elastic/client/AggregateApi.scala

Lines changed: 31 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -130,59 +130,41 @@ trait SingleValueAggregateApi
130130
// Execute the search
131131
search(sqlQuery)
132132
.flatMap { response =>
133-
// Parse the response
134-
val parseResult = ElasticResult.fromTry(parseResponse(response))
135-
136-
parseResult match {
137-
// Case 1: Parse successful - process the results
138-
case ElasticSuccess(results) =>
139-
val aggregationResults = results.flatMap { result =>
140-
response.aggregations.map { case (name, aggregation) =>
141-
// Attempt to process each aggregation
142-
val aggregationResult = ElasticResult.attempt {
143-
val value = findAggregation(name, result).orNull match {
144-
case b: Boolean => BooleanValue(b)
145-
case n: Number => NumericValue(n)
146-
case s: String => StringValue(s)
147-
case t: Temporal => TemporalValue(t)
148-
case m: Map[_, Any] => ObjectValue(m.map(kv => kv._1.toString -> kv._2))
149-
case s: Seq[_] if aggregation.multivalued =>
150-
getAggregateValue(s, aggregation.distinct)
151-
case _ => EmptyValue
152-
}
153-
154-
SingleValueAggregateResult(name, aggregation.aggType, value)
155-
}
156-
157-
// Convert failures to results with errors
158-
aggregationResult match {
159-
case ElasticSuccess(result) => result
160-
case ElasticFailure(error) =>
161-
SingleValueAggregateResult(
162-
name,
163-
aggregation.aggType,
164-
EmptyValue,
165-
error = Some(s"Failed to process aggregation: ${error.message}")
166-
)
167-
}
168-
}.toSeq
133+
val results = response.results
134+
val aggregationResults = results.flatMap { result =>
135+
response.aggregations.map { case (name, aggregation) =>
136+
// Attempt to process each aggregation
137+
val aggregationResult = ElasticResult.attempt {
138+
val value = findAggregation(name, result).orNull match {
139+
case b: Boolean => BooleanValue(b)
140+
case n: Number => NumericValue(n)
141+
case s: String => StringValue(s)
142+
case t: Temporal => TemporalValue(t)
143+
case m: Map[_, Any] => ObjectValue(m.map(kv => kv._1.toString -> kv._2))
144+
case s: Seq[_] if aggregation.multivalued =>
145+
getAggregateValue(s, aggregation.distinct)
146+
case _ => EmptyValue
147+
}
148+
149+
SingleValueAggregateResult(name, aggregation.aggType, value)
169150
}
170151

171-
ElasticResult.success(aggregationResults)
152+
// Convert failures to results with errors
153+
aggregationResult match {
154+
case ElasticSuccess(result) => result
155+
case ElasticFailure(error) =>
156+
SingleValueAggregateResult(
157+
name,
158+
aggregation.aggType,
159+
EmptyValue,
160+
error = Some(s"Failed to process aggregation: ${error.message}")
161+
)
162+
}
163+
}.toSeq
164+
}
172165

173-
// Case 2: Parse failed - returning empty results with errors
174-
case ElasticFailure(error) =>
175-
val errorResults = response.aggregations.map { case (name, aggregation) =>
176-
SingleValueAggregateResult(
177-
name,
178-
aggregation.aggType,
179-
EmptyValue,
180-
error = Some(s"Parse error: ${error.message}")
181-
)
182-
}.toSeq
166+
ElasticResult.success(aggregationResults)
183167

184-
ElasticResult.success(errorResults)
185-
}
186168
}
187169
.fold(
188170
// If search() fails, throw an exception

core/src/main/scala/app/softnetwork/elastic/client/ElasticConversion.scala

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ trait ElasticConversion {
4444
m: Manifest[T],
4545
formats: Formats
4646
): Try[Seq[T]] = {
47-
parseResponse(response).map { rows =>
48-
rows.map { row =>
47+
Try(
48+
response.results.map { row =>
4949
convertTo[T](row)(m, formats)
5050
}
51-
}
51+
)
5252
}
5353

5454
// Formatters for elasticsearch ISO 8601 date/time strings
@@ -60,15 +60,17 @@ trait ElasticConversion {
6060
* multi-search (msearch/UNION ALL) responses
6161
*/
6262
def parseResponse(
63-
response: ElasticResponse
63+
results: String,
64+
fieldAliases: Map[String, String],
65+
aggregations: Map[String, ClientAggregation]
6466
): Try[Seq[Map[String, Any]]] = {
65-
val json = mapper.readTree(response.results)
67+
val json = mapper.readTree(results)
6668
// Check if it's a multi-search response (array of responses)
6769
if (json.isArray) {
68-
parseMultiSearchResponse(json, response.fieldAliases, response.aggregations)
70+
parseMultiSearchResponse(json, fieldAliases, aggregations)
6971
} else {
7072
// Single search response
71-
parseSingleSearchResponse(json, response.fieldAliases, response.aggregations)
73+
parseSingleSearchResponse(json, fieldAliases, aggregations)
7274
}
7375
}
7476

0 commit comments

Comments
 (0)