nbcp.myoql.db.es.component.EsBaseQueryClip.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.ListResult
import nbcp.base.enums.JsonStyleScopeEnum
import nbcp.base.extend.*
import nbcp.base.extend.*
import nbcp.base.extend.*
import nbcp.base.extend.*
import nbcp.base.utils.Md5Util
import nbcp.base.utils.ReflectUtil
import nbcp.myoql.db.db
import nbcp.myoql.db.es.base.EsColumnName
import nbcp.myoql.db.es.logger.logGet
import org.elasticsearch.client.Request
import org.elasticsearch.client.Response
import org.slf4j.LoggerFactory
import java.time.LocalDateTime
open class EsBaseQueryClip(tableName: String) : EsClipBase(tableName), IEsWhereable {
companion object {
private val logger = LoggerFactory.getLogger(this::class.java.declaringClass)
}
var routing = "";
var search = SearchBodyClip()
/**
* 批量添加中的添加实体。
*/
override fun whereId(id: String) {
if (id.isEmpty()) {
throw RuntimeException("批量删除时需要指定Id")
}
this.search.ids.add(id)
}
fun selectField(column: String) {
search._source.add(column);
}
@JvmOverloads
fun withRouting(routing: String = "") {
this.routing = routing;
}
/**
* 返回该对象的 Md5。
*/
private fun getCacheKey(): String {
var unKeys = mutableListOf()
unKeys.add(search.toString())
return Md5Util.getBase64Md5(unKeys.joinToString("\n"));
}
fun setLimit(skip: Int, take: Int) {
this.search.skip = skip;
this.search.take = take;
}
/**
* 升序
*/
fun setOrderByAsc(sort : EsColumnName) {
this.orderBy(true, sort)
}
/**
* 降序
*/
fun setOrderByDesc(sort : EsColumnName) {
this.orderBy(false, sort)
}
private fun orderBy(asc: Boolean, column: EsColumnName) {
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)))
}
fun setShould( vararg where: WhereData) {
this.search.query.addShould(*where)
}
fun setMust(vararg where: WhereData) {
this.search.query.addMust(* where )
}
fun setMustNot( vararg where: WhereData) {
this.search.query.addMustNot( *where )
}
var total: Int = -1;
private fun getRestResult(url: String, requestBody: String): Map {
db.affectRowCount = 0;
val request = Request("POST", url);
request.setJsonEntity(requestBody)
var response: Response? = null
val startAt = LocalDateTime.now();
var error: Exception? = null
var responseData: Map? = null
logger.info(request.ToJson())
try {
response = esTemplate.performRequest(request)
db.executeTime = LocalDateTime.now() - startAt
if (response.statusLine.statusCode != 200) {
return mapOf()
}
responseData = response.entity.content
.readContentString()
.FromJson