Skip to content

Commit 2923836

Browse files
Merge pull request #165 from jillesvangurp/geotile_grid-helpers
add helper functions for geotile_grid and geo_centroid
2 parents 0a6b704 + ed0a04f commit 2923836

File tree

2 files changed

+67
-21
lines changed

2 files changed

+67
-21
lines changed

docs/src/test/kotlin/documentation/manual/search/aggregations.kt

+12-5
Original file line numberDiff line numberDiff line change
@@ -251,17 +251,24 @@ val aggregationsMd = sourceGitRepository.md {
251251
""".trimIndent()
252252

253253
example {
254-
client.search(indexName) {
254+
val response = client.search(indexName) {
255255
resultSize = 0
256256
agg("grid", GeoTileGridAgg(TestGeoDoc::point.name,13)) {
257257
agg("centroid", GeoCentroidAgg(TestGeoDoc::point.name))
258258
}
259259
}
260-
}.let {
261-
it.result.getOrNull()?.let {
262-
mdCodeBlock(DEFAULT_PRETTY_JSON.encodeToString(it), type="json",wrap = true)
260+
261+
println(
262+
DEFAULT_PRETTY_JSON.encodeToString(response)
263+
)
264+
val geoTiles = response.aggregations.geoTileGridResult("grid")
265+
geoTiles.parsedBuckets.forEach { bucket ->
266+
bucket.aggregations.geoCentroid("centroid").let {
267+
val key = bucket.parsed.key
268+
println("$key: ${it.location} - ${it.count} ")
269+
}
263270
}
264-
}
271+
}.printStdOut()
265272

266273
}
267274

search-client/src/commonMain/kotlin/com/jillesvangurp/ktsearch/SearchResponse.kt

+55-16
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
package com.jillesvangurp.ktsearch
55

66
import com.jillesvangurp.ktsearch.repository.ModelSerializationStrategy
7+
import com.jillesvangurp.searchdsls.querydsl.GeoTileGridAgg
8+
import com.jillesvangurp.searchdsls.querydsl.Shape
79
import com.jillesvangurp.serializationext.DEFAULT_JSON
810
import kotlinx.coroutines.flow.Flow
911
import kotlinx.coroutines.flow.map
@@ -131,14 +133,15 @@ inline fun <reified T> Flow<SearchResponse.Hit>.parseHits(
131133
): Flow<T> = map {
132134
it.parseHit<T>()
133135
}
136+
134137
fun <T> Flow<SearchResponse.Hit>.parseHits(
135138
deserializationStrategy: DeserializationStrategy<T>,
136139
json: Json = DEFAULT_JSON
137140
): Flow<T> = map {
138141
it.parseHit(deserializationStrategy, json)
139142
}
140143

141-
fun <T: Any> Flow<SearchResponse.Hit>.parseHits(deserializationStrategy: ModelSerializationStrategy<T>): Flow<T> =
144+
fun <T : Any> Flow<SearchResponse.Hit>.parseHits(deserializationStrategy: ModelSerializationStrategy<T>): Flow<T> =
142145
mapNotNull { hit ->
143146
hit.source?.let { deserializationStrategy.deSerialize(it) }
144147
}
@@ -381,51 +384,51 @@ data class ExtendedStatsBucketResult(
381384
@EncodeDefault
382385
val max: Double = 0.0,
383386
@EncodeDefault
384-
val avg: Double= 0.0,
387+
val avg: Double = 0.0,
385388
@EncodeDefault
386-
val sum: Double= 0.0,
389+
val sum: Double = 0.0,
387390
@SerialName("sum_of_squares")
388391
@EncodeDefault
389-
val sumOfSquares: Double= 0.0,
392+
val sumOfSquares: Double = 0.0,
390393
@EncodeDefault
391-
val variance: Double= 0.0,
394+
val variance: Double = 0.0,
392395
@SerialName("variance_population")
393396
@EncodeDefault
394-
val variancePopulation: Double= 0.0,
397+
val variancePopulation: Double = 0.0,
395398
@SerialName("variance_sampling")
396399
@EncodeDefault
397-
val varianceSampling: Double= 0.0,
400+
val varianceSampling: Double = 0.0,
398401
@SerialName("std_deviation")
399402
@EncodeDefault
400-
val stdDeviation: Double= 0.0,
403+
val stdDeviation: Double = 0.0,
401404
@SerialName("std_deviation_population")
402405
@EncodeDefault
403-
val stdDeviationPopulation: Double= 0.0,
406+
val stdDeviationPopulation: Double = 0.0,
404407
@SerialName("std_deviation_sampling")
405408
@EncodeDefault
406-
val stdDeviationSampling: Double= 0.0,
409+
val stdDeviationSampling: Double = 0.0,
407410
@SerialName("std_deviation_bounds")
408411
@EncodeDefault
409412
val stdDeviationBounds: Bounds = Bounds()
410413
) {
411414
@Serializable
412415
data class Bounds(
413416
@EncodeDefault
414-
val upper: Double= 0.0,
417+
val upper: Double = 0.0,
415418
@EncodeDefault
416-
val lower: Double= 0.0,
419+
val lower: Double = 0.0,
417420
@SerialName("upper_population")
418421
@EncodeDefault
419-
val upperPopulation: Double= 0.0,
422+
val upperPopulation: Double = 0.0,
420423
@SerialName("lower_population")
421424
@EncodeDefault
422-
val lowerPopulation: Double= 0.0,
425+
val lowerPopulation: Double = 0.0,
423426
@SerialName("upper_sampling")
424427
@EncodeDefault
425-
val upperSampling: Double= 0.0,
428+
val upperSampling: Double = 0.0,
426429
@SerialName("lower_sampling")
427430
@EncodeDefault
428-
val lowerSampling: Double= 0.0,
431+
val lowerSampling: Double = 0.0,
429432
)
430433
}
431434

@@ -446,6 +449,42 @@ fun Aggregations?.topHitResult(name: String, json: Json = DEFAULT_JSON): TopHits
446449
fun Aggregations?.topHitResult(name: Enum<*>, json: Json = DEFAULT_JSON): TopHitsAggregationResult =
447450
getAggResult(name.name, json)
448451

452+
@Serializable
453+
data class GeoTileGridBucket(
454+
val key: String,
455+
@SerialName("doc_count")
456+
val docCount: Long,
457+
)
458+
459+
@Serializable
460+
data class GeoTileGridResult(
461+
override val buckets: List<JsonObject>
462+
) : BucketAggregationResult<GeoTileGridBucket>
463+
464+
val GeoTileGridResult.parsedBuckets get() = buckets.map { Bucket(it, GeoTileGridBucket.serializer()) }
465+
466+
fun Aggregations?.geoTileGridResult(name: String, json: Json = DEFAULT_JSON): GeoTileGridResult =
467+
getAggResult(name, json)
468+
469+
fun Aggregations?.geoTileGridResult(name: Enum<*>, json: Json = DEFAULT_JSON): GeoTileGridResult =
470+
getAggResult(name, json)
471+
472+
473+
@Serializable
474+
data class GeoCentroidResult(
475+
val location: Point,
476+
val count: Long,
477+
478+
) {
479+
val pointCoordinate get() = doubleArrayOf(location.lon, location.lat)
480+
481+
@Serializable
482+
data class Point(val lat: Double, val lon: Double)
483+
}
484+
485+
fun Aggregations?.geoCentroid(name: String) = getAggResult<GeoCentroidResult>(name)
486+
fun Aggregations?.geoCentroid(name: Enum<*>) = getAggResult<GeoCentroidResult>(name)
487+
449488
@Serializable
450489
data class SumAggregationResult(
451490
val value: Double,

0 commit comments

Comments
 (0)