Skip to content

Commit

Permalink
Merge pull request #165 from jillesvangurp/geotile_grid-helpers
Browse files Browse the repository at this point in the history
add helper functions for geotile_grid and geo_centroid
  • Loading branch information
jillesvangurp authored Jan 31, 2025
2 parents 0a6b704 + ed0a04f commit 2923836
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 21 deletions.
17 changes: 12 additions & 5 deletions docs/src/test/kotlin/documentation/manual/search/aggregations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -251,17 +251,24 @@ val aggregationsMd = sourceGitRepository.md {
""".trimIndent()

example {
client.search(indexName) {
val response = client.search(indexName) {
resultSize = 0
agg("grid", GeoTileGridAgg(TestGeoDoc::point.name,13)) {
agg("centroid", GeoCentroidAgg(TestGeoDoc::point.name))
}
}
}.let {
it.result.getOrNull()?.let {
mdCodeBlock(DEFAULT_PRETTY_JSON.encodeToString(it), type="json",wrap = true)

println(
DEFAULT_PRETTY_JSON.encodeToString(response)
)
val geoTiles = response.aggregations.geoTileGridResult("grid")
geoTiles.parsedBuckets.forEach { bucket ->
bucket.aggregations.geoCentroid("centroid").let {
val key = bucket.parsed.key
println("$key: ${it.location} - ${it.count} ")
}
}
}
}.printStdOut()

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package com.jillesvangurp.ktsearch

import com.jillesvangurp.ktsearch.repository.ModelSerializationStrategy
import com.jillesvangurp.searchdsls.querydsl.GeoTileGridAgg
import com.jillesvangurp.searchdsls.querydsl.Shape
import com.jillesvangurp.serializationext.DEFAULT_JSON
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
Expand Down Expand Up @@ -131,14 +133,15 @@ inline fun <reified T> Flow<SearchResponse.Hit>.parseHits(
): Flow<T> = map {
it.parseHit<T>()
}

fun <T> Flow<SearchResponse.Hit>.parseHits(
deserializationStrategy: DeserializationStrategy<T>,
json: Json = DEFAULT_JSON
): Flow<T> = map {
it.parseHit(deserializationStrategy, json)
}

fun <T: Any> Flow<SearchResponse.Hit>.parseHits(deserializationStrategy: ModelSerializationStrategy<T>): Flow<T> =
fun <T : Any> Flow<SearchResponse.Hit>.parseHits(deserializationStrategy: ModelSerializationStrategy<T>): Flow<T> =
mapNotNull { hit ->
hit.source?.let { deserializationStrategy.deSerialize(it) }
}
Expand Down Expand Up @@ -381,51 +384,51 @@ data class ExtendedStatsBucketResult(
@EncodeDefault
val max: Double = 0.0,
@EncodeDefault
val avg: Double= 0.0,
val avg: Double = 0.0,
@EncodeDefault
val sum: Double= 0.0,
val sum: Double = 0.0,
@SerialName("sum_of_squares")
@EncodeDefault
val sumOfSquares: Double= 0.0,
val sumOfSquares: Double = 0.0,
@EncodeDefault
val variance: Double= 0.0,
val variance: Double = 0.0,
@SerialName("variance_population")
@EncodeDefault
val variancePopulation: Double= 0.0,
val variancePopulation: Double = 0.0,
@SerialName("variance_sampling")
@EncodeDefault
val varianceSampling: Double= 0.0,
val varianceSampling: Double = 0.0,
@SerialName("std_deviation")
@EncodeDefault
val stdDeviation: Double= 0.0,
val stdDeviation: Double = 0.0,
@SerialName("std_deviation_population")
@EncodeDefault
val stdDeviationPopulation: Double= 0.0,
val stdDeviationPopulation: Double = 0.0,
@SerialName("std_deviation_sampling")
@EncodeDefault
val stdDeviationSampling: Double= 0.0,
val stdDeviationSampling: Double = 0.0,
@SerialName("std_deviation_bounds")
@EncodeDefault
val stdDeviationBounds: Bounds = Bounds()
) {
@Serializable
data class Bounds(
@EncodeDefault
val upper: Double= 0.0,
val upper: Double = 0.0,
@EncodeDefault
val lower: Double= 0.0,
val lower: Double = 0.0,
@SerialName("upper_population")
@EncodeDefault
val upperPopulation: Double= 0.0,
val upperPopulation: Double = 0.0,
@SerialName("lower_population")
@EncodeDefault
val lowerPopulation: Double= 0.0,
val lowerPopulation: Double = 0.0,
@SerialName("upper_sampling")
@EncodeDefault
val upperSampling: Double= 0.0,
val upperSampling: Double = 0.0,
@SerialName("lower_sampling")
@EncodeDefault
val lowerSampling: Double= 0.0,
val lowerSampling: Double = 0.0,
)
}

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

@Serializable
data class GeoTileGridBucket(
val key: String,
@SerialName("doc_count")
val docCount: Long,
)

@Serializable
data class GeoTileGridResult(
override val buckets: List<JsonObject>
) : BucketAggregationResult<GeoTileGridBucket>

val GeoTileGridResult.parsedBuckets get() = buckets.map { Bucket(it, GeoTileGridBucket.serializer()) }

fun Aggregations?.geoTileGridResult(name: String, json: Json = DEFAULT_JSON): GeoTileGridResult =
getAggResult(name, json)

fun Aggregations?.geoTileGridResult(name: Enum<*>, json: Json = DEFAULT_JSON): GeoTileGridResult =
getAggResult(name, json)


@Serializable
data class GeoCentroidResult(
val location: Point,
val count: Long,

) {
val pointCoordinate get() = doubleArrayOf(location.lon, location.lat)

@Serializable
data class Point(val lat: Double, val lon: Double)
}

fun Aggregations?.geoCentroid(name: String) = getAggResult<GeoCentroidResult>(name)
fun Aggregations?.geoCentroid(name: Enum<*>) = getAggResult<GeoCentroidResult>(name)

@Serializable
data class SumAggregationResult(
val value: Double,
Expand Down

0 comments on commit 2923836

Please sign in to comment.