All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.nemerosa.ontrack.model.structure.SearchIndexMappingExtensions.kt Maven / Gradle / Ivy

There is a newer version: 4.4.5
Show newest version
package net.nemerosa.ontrack.model.structure

import kotlin.reflect.KProperty1

@SearchIndexMappingMarker
fun  indexMappings(code: SearchIndexMappingBuilder.() -> Unit): SearchIndexMapping {
    val builder = SearchIndexMappingBuilder()
    builder.code()
    return builder.createMapping()
}

@SearchIndexMappingMarker
class SearchIndexMappingBuilder {

    private val fields = mutableListOf>()

    operator fun KProperty1.unaryPlus(): SearchIndexMappingFieldBuilder {
        val builder = SearchIndexMappingFieldBuilder(this)
        fields.add(builder)
        return builder
    }

    fun type(typeName: String, typeInit: SearchIndexMappingFieldTypeBuilder.() -> Unit = {}) = SearchIndexMappingFieldTypeBuilder(typeName).apply { typeInit() }

    fun id(typeInit: SearchIndexMappingFieldTypeBuilder.() -> Unit = {}) = type("long", typeInit)
    fun keyword(typeInit: SearchIndexMappingFieldTypeBuilder.() -> Unit = {}) = type("keyword", typeInit)
    fun nested(typeInit: SearchIndexMappingFieldTypeBuilder.() -> Unit = {}) = type("nested", typeInit)
    fun text(typeInit: SearchIndexMappingFieldTypeBuilder.() -> Unit = {}) = type("text", typeInit)

    fun createMapping() = SearchIndexMapping(
            fields = fields.map { it.createField() }
    )
}

@SearchIndexMappingMarker
class SearchIndexMappingFieldBuilder(
        private val property: KProperty1
) {

    private val types = mutableListOf()

    infix fun to(typeBuilder: SearchIndexMappingFieldTypeBuilder): SearchIndexMappingFieldBuilder {
        types.add(typeBuilder.createType())
        return this
    }

    fun createField() = SearchIndexMappingField(
            name = property.name,
            types = types.toList()
    )

}

@SearchIndexMappingMarker
class SearchIndexMappingFieldTypeBuilder(private val typeName: String) {

    var index: Boolean? = null
    var scoreBoost: Double? = null

    fun createType(): SearchIndexMappingFieldType {
        return SearchIndexMappingFieldType(
                type = typeName,
                index = index,
                scoreBoost = scoreBoost
        )
    }
}

@DslMarker
annotation class SearchIndexMappingMarker




© 2015 - 2025 Weber Informatics LLC | Privacy Policy