Skip to content

Commit

Permalink
fix sorting api
Browse files Browse the repository at this point in the history
  • Loading branch information
jillesvangurp committed Jul 27, 2022
1 parent 7ffc7b1 commit 63929bb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,11 @@ class SearchDSLTest : AbstractElasticSearchTest(indexPrefix = "search", createIn
sort {
+"tag"
+TestModel::tag
add(TestModel::number, order = SortOrder.DESC, mode = SortMode.MAX)
add(
field = TestModel::number,
order = SortOrder.DESC,
mode = SortMode.MAX
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ import kotlin.reflect.KProperty
annotation class SearchDSLMarker

@SearchDSLMarker
open class ESQuery(
val name: String,
val queryDetails: JsonDsl = JsonDsl(namingConvention = PropertyNamingConvention.ConvertToSnakeCase)
) :
open class ESQuery(val name: String, val queryDetails: JsonDsl = JsonDsl(namingConvention = PropertyNamingConvention.ConvertToSnakeCase)) :
IJsonDsl by queryDetails {

fun toMap(): Map<String, Any> = dslObject { this[name] = queryDetails }
Expand Down Expand Up @@ -76,7 +73,6 @@ class SearchDSL : JsonDsl(namingConvention = PropertyNamingConvention.ConvertToS

enum class SortOrder { ASC, DESC }
enum class SortMode { MIN, MAX, SUM, AVG, MEDIAN }

@Suppress("UNUSED_PARAMETER")
class SortField(field: String, order: SortOrder? = null, mode: SortMode? = null)

Expand All @@ -90,15 +86,15 @@ class SortBuilder {
field: KProperty<*>,
order: SortOrder = SortOrder.DESC,
mode: SortMode? = null,
missing: String?,
missing: String? = null,
block: (JsonDsl.() -> Unit)? = null
) =
add(field = field.name, order = order, mode = mode, missing = missing, block = block)
add(field.name, order, mode, missing, block)

fun add(
field: String,
order: SortOrder = SortOrder.DESC,
mode: SortMode? = null,
mode: SortMode?= null,
missing: String? = null,
block: (JsonDsl.() -> Unit)? = null
) = sortFields.add(withJsonDsl {
Expand All @@ -108,15 +104,15 @@ class SortBuilder {
this["mode"] = mode.name.lowercase()
}
missing?.let {
this["missing"] = it
this["missing"] = missing
}
block?.invoke(this)
}, PropertyNamingConvention.AsIs)
})
}

fun SearchDSL.sort(block: SortBuilder.() -> Unit) {
val builder = SortBuilder()
val builder = SortBuilder()
block.invoke(builder)
this["sort"] = builder.sortFields
}
Expand Down Expand Up @@ -159,19 +155,19 @@ class MultiSearchDSL(internal val jsonDslSerializer: JsonDslSerializer) {
json.add(jsonDslSerializer.serialize(dsl))
}

fun header(headerBlock: MultiSearchHeader.() -> Unit): MultiSearchHeader {
fun header(headerBlock: MultiSearchHeader.()-> Unit) : MultiSearchHeader {
val header = MultiSearchHeader()
headerBlock.invoke(header)
return header
}

infix fun MultiSearchHeader.withQuery(queryBlock: SearchDSL.() -> Unit) {
infix fun MultiSearchHeader.withQuery(queryBlock: SearchDSL.()-> Unit) {
val dsl = SearchDSL()
queryBlock.invoke(dsl)
add(this, dsl)
}

fun withQuery(queryBlock: SearchDSL.() -> Unit) {
fun withQuery(queryBlock: SearchDSL.()-> Unit) {
val dsl = SearchDSL()
queryBlock.invoke(dsl)
add(MultiSearchHeader(), dsl)
Expand Down

0 comments on commit 63929bb

Please sign in to comment.