nbcp.myoql.db.es.component.EsQueryClip.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ktmyoql Show documentation
Show all versions of ktmyoql Show documentation
kotlin orm -- mysql,mongo , just like ktorm
The newest version!
package nbcp.myoql.db.es.component
import nbcp.base.comm.JsonMap
import nbcp.base.comm.ListQueryModel
import nbcp.base.comm.ListResult
import nbcp.base.comm.SortIsAsc
import nbcp.myoql.db.es.base.EsColumnName
import java.io.Serializable
/**
* EsQuery
* https://www.elastic.co/guide/en/elasticsearch/reference/7.6/search.html
*/
class EsQueryClip, E : Serializable>(var esEntity: M) :
EsBaseQueryClip(esEntity.tableName) {
@JvmOverloads
fun routing(routing: String = ""): EsQueryClip {
this.routing = routing;
return this;
}
fun limit(skip: Int, take: Int): EsQueryClip {
setLimit(skip, take)
return this;
}
fun withQueryModel(queryModel: ListQueryModel): EsQueryClip {
return this
.limit(queryModel.skip, queryModel.take)
.withSorts(queryModel.sorts);
}
private fun withSorts(sorts: MutableList): EsQueryClip {
sorts.forEach {
if (it.sort.isNullOrEmpty()) {
return@forEach
}
this.orderBy(EsColumnName(it.sort), it.asc);
}
return this;
}
/**
* 升序
*/
fun orderByAsc(sortFunc: (M) -> EsColumnName): EsQueryClip {
setOrderByAsc(sortFunc(this.esEntity))
return this;
}
/**
* 降序
*/
fun orderByDesc(sortFunc: (M) -> EsColumnName): EsQueryClip {
setOrderByDesc(sortFunc(this.esEntity))
return this
}
private fun orderBy(column: EsColumnName, asc: Boolean): EsQueryClip {
var order_str = "";
if (asc) {
order_str = "asc"
} else {
order_str = "desc"
}
this.search.sort.add(JsonMap(column.toString() to JsonMap("order" to order_str)))
return this
}
fun should(vararg where: (M) -> WhereData): EsQueryClip {
setShould(*where.map { it.invoke(this.esEntity) }.toTypedArray())
return this;
}
fun must(vararg where: (M) -> WhereData): EsQueryClip {
setMust(*where.map { it.invoke(this.esEntity) }.toTypedArray())
return this;
}
fun mustNot(vararg where: (M) -> WhereData): EsQueryClip {
setMustNot(*where.map { it.invoke(this.esEntity) }.toTypedArray())
return this;
}
// fun whereOr(vararg wheres: (M) -> SearchBodyClip): EsQueryClip {
// return whereOr(*wheres.map { it(moerEntity) }.toTypedArray())
// }
//
// fun whereOr(vararg wheres: SearchBodyClip): EsQueryClip {
// if (wheres.any() == false) return this;
//
// return this;
// }
fun select(vararg columns: EsColumnName): EsQueryClip {
this.search._source.addAll(columns.map { it.toString() })
return this;
}
fun select(vararg columns: String): EsQueryClip {
this.search._source.addAll(columns)
return this;
}
fun select(column: (M) -> EsColumnName): EsQueryClip {
this.search._source.add(column(this.esEntity).toString())
return this;
}
fun unSelect(column: (M) -> EsColumnName): EsQueryClip {
return this;
}
@JvmOverloads
fun toList(mapFunc: ((Map) -> Unit)? = null): MutableList {
return toList(esEntity.entityClass, mapFunc)
}
@JvmOverloads
fun toEntity(mapFunc: ((Map) -> Unit)? = null): E? {
this.search.take = 1;
return toList(esEntity.entityClass, mapFunc).firstOrNull();
}
@JvmOverloads
fun toEntity(type: Class, mapFunc: ((Map) -> Unit)? = null): R? {
this.search.take = 1;
return toList(type, mapFunc).firstOrNull();
}
@JvmOverloads
fun toListResult(mapFunc: ((Map) -> Unit)? = null): ListResult {
return toListResult(this.esEntity.entityClass, mapFunc);
}
}