Commit 233f109e authored by 赵威's avatar 赵威

try es search

parent eb5ecc8a
......@@ -29,9 +29,9 @@ lazy val root = (project in file("."))
name := "streamingUserPortrait",
libraryDependencies += "com.alibaba" % "fastjson" % "1.2.75",
// libraryDependencies += esCore,
// libraryDependencies += esJava,
libraryDependencies += "com.sksamuel.elastic4s" %% "elastic4s-http" % "6.7.8",
libraryDependencies += esCore,
libraryDependencies += esJava,
// libraryDependencies += es,
// libraryDependencies += esCore,
......
package com.gmei.up.utils
import scala.concurrent._
import scala.concurrent.duration._
import ExecutionContext.Implicits.global
import com.sksamuel.elastic4s.http.{ ElasticClient, ElasticProperties, Response }
import com.sksamuel.elastic4s.http.search.SearchResponse
import com.sksamuel.elastic4s.http.ElasticDsl._
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
object ES {
// TODO read config from file
val host = "172.16.52.33"
val port = 9200
val username = "elastic"
val password = "gengmei!@#"
val diaryIndex = "gm-dbmw-diary-read"
val nodes = ElasticProperties(
"http://172.16.52.33:9200,http://172.16.52.19:9200,http://172.16.52.27:9200,http://172.16.52.34:9200,http://172.16.52.48:9200"
)
val client = ElasticClient(nodes)
// import scala.concurrent._
// import scala.concurrent.duration._
// import ExecutionContext.Implicits.global
// import com.sksamuel.elastic4s.ElasticsearchClientUri
// import com.sksamuel.elastic4s.http.{ ElasticClient, ElasticProperties, Response }
// import com.sksamuel.elastic4s.http.search.SearchResponse
import com.sksamuel.elastic4s.ElasticDsl._
// import org.apache.http.client.config.RequestConfig
// import org.apache.http.impl.client.BasicCredentialsProvider
// import org.apache.http.auth.{ AuthScope, UsernamePasswordCredentials }
// import org.apache.http.impl.nio.client.HttpAsyncClientBuilder
// import org.elasticsearch.client.RestClientBuilder.{ HttpClientConfigCallback, RequestConfigCallback }
// // object ES {
// // // TODO read config from file
// // val host = "172.16.52.33"
// // val port = 9200
// // val username = "elastic"
// // val password = "gengmei!@#"
// // val diaryIndex = "gm-dbmw-diary-read"
// // // val nodes = ElasticProperties(
// // // "http://172.16.52.33:9200,http://172.16.52.19:9200,http://172.16.52.27:9200,http://172.16.52.34:9200,http://172.16.52.48:9200"
// // // )
// // // val client = ElasticClient(nodes)
// // def test() {
// // val json =
// // """
// // |{
// // | "query" : {
// // | "match" : {"tags_v3" : ["光子嫩肤", "水光针"]}
// // | }
// // |}
// // |""".stripMargin
// // val resp = client.execute {
// // search("gm-dbmw-diary-read").source(json)
// // }.await
// // println(resp)
// // resp
// // }
// // }
object ESClient {
private class ElasticCredentials(username: String, password: String) extends HttpClientConfigCallback {
val credentials = new UsernamePasswordCredentials(username, password)
val credentialsProvider = new BasicCredentialsProvider()
credentialsProvider.setCredentials(AuthScope.ANY, credentials)
override def customizeHttpClient(
httpClientBuilder: HttpAsyncClientBuilder
): HttpAsyncClientBuilder =
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)
}
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(JavaClient.fromRestClient(restClient))
}
}
object ES {
def test() {
val client = ESClient.create("172.16.52.33", 9200, "elastic", "gengmei!@#")
val json =
"""
|{
......
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