Commit 9f0ecc2b authored by 赵威's avatar 赵威

update client

parent 931d4dd8
......@@ -36,9 +36,9 @@ lazy val root = (project in file("."))
libraryDependencies += jsonNative,
libraryDependencies += jsonJackson,
libraryDependencies += "com.sksamuel.elastic4s" %% "elastic4s-http" % "6.7.8",
// libraryDependencies += esCore,
// libraryDependencies += esJava,
//libraryDependencies += "com.sksamuel.elastic4s" %% "elastic4s-http" % "6.7.8",
libraryDependencies += esCore,
libraryDependencies += esJava,
libraryDependencies += flinkCore,
libraryDependencies += flinkScala,
......
......@@ -2,21 +2,21 @@ package com.gmei.up.utils
import scala.collection.mutable.ListBuffer
import scala.util.Try
// import com.sksamuel.elastic4s.http.{ JavaClient, HttpClient }
import com.sksamuel.elastic4s.ElasticClient
import com.sksamuel.elastic4s.http.JavaClient
import org.apache.http.HttpHost
import org.apache.http.auth.{ AuthScope, UsernamePasswordCredentials }
import org.apache.http.impl.client.BasicCredentialsProvider
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder
import org.elasticsearch.client.RestClient
import org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback
import com.sksamuel.elastic4s.http.ElasticDsl._
import com.sksamuel.elastic4s.http.ElasticClient
// import com.sksamuel.elastic4s.requests.searches.{ SearchRequest, SearchResponse }
import com.sksamuel.elastic4s.requests.searches.{ SearchRequest, SearchResponse }
import com.sksamuel.elastic4s.ElasticDsl._
import com.sksamuel.elastic4s.{ HitReader, Hit }
import org.json4s.JsonDSL._
import org.json4s.jackson.JsonMethods._
import org.json4s.JObject
// import org.json4s.jackson.Serialization.write
// import scala.concurrent._
// import scala.concurrent.duration._
// import ExecutionContext.Implicits.global
......@@ -31,15 +31,13 @@ object ESClient {
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)
}
// ElasticClient
def create(hostname: String, port: Int, username: String, password: String): ElasticClient = {
val restClient = RestClient
.builder(new HttpHost(hostname, port, "http"))
.setHttpClientConfigCallback(new ElasticCredentials(username, password))
.build()
ElasticClient.fromRestClient(restClient)
// ElasticClient(JavaClient.fromRestClient(restClient))
ElasticClient(JavaClient.fromRestClient(restClient))
}
}
......@@ -57,51 +55,28 @@ object ES {
// TODO read from config
val client: ElasticClient = ESClient.create("172.16.52.33", 9200, "elastic", "gengmei!@#")
// val bb = multi(
// search("gm-dbmw-diary-read").source(s"""${diaryQuery}"""),
// search("gm-dbmw-tractate-read").source(tractateQuery)
// )
// println(bb.show)
// println(diaryQuery)
// println(tractateQuery)
// // TODO remove await
// val resp = client.execute(bb).await
val bb = multi(
search("gm-dbmw-diary-read").source(s"""${diaryQuery}"""),
search("gm-dbmw-tractate-read").source(tractateQuery)
)
// // println(resp)
println(bb.show)
println(diaryQuery)
println(tractateQuery)
// val a = resp.result.to[Character]
// TODO remove await
val resp = client.execute(bb).await
// println("%%%%%%%%%%%")
// println(a.size)
// a.foreach { x =>
// println(x.id, x.index, x.projects)
// }
// println("%%%%%%%%%%%")
// println(resp)
// resp
val a = resp.result.to[Character]
val a = s"""
{
"query": {
"bool": {
"must": [
{ "term": { "is_online": true } },
{ "range": { "content_level": { "gte": 3 } } }
]
println("%%%%%%%%%%%")
println(a.size)
a.foreach { x =>
println(x.id, x.index, x.projects)
}
},
"size": 10
}
""".stripMargin
val resp = client.execute {
search("gm-dbmw-diary-read").source(a)
}.await
println(resp)
println("%%%%%%%%%%%")
resp
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment