scala 프로젝트에서 사용할만한 scala- elasticsearch 라이브러리는 sksamuel이 가장 좋은 것 같다. (아직 갈길이 멀지만.)



sksamuel의 HTTPClient을 잘 사용하고 있다. 

val result = client.execute {
val req = search(indexName).
query {
bool {
//..
}

queryStatement = client.show(req)
req
}
}


중요한 것은 비동기로 되어 있어서 쭉 지나가 버린다. 


동기(synchronous) 코드로 개발하려면 Await.result 또는 Await.ready를 사용한다. 




Await.result를 사용하는 코드는 간단하다. 내부적으로 Future[Either[]] 형태로 Right/Left를 먼저 써야 한다. 


import scala.concurrent.duration._
val response = Await.result(result, atMost = 5.second)

response match {
case Right(success) => {
// ..

}
case Left(e) => {
//..
}
}



Await.ready와 Await.result는 exception 처리방식이 조금 다르다. 나는 아직 완벽한 프로그램이 아니라서 차라리 crash가 나은 듯해서 Await.result를 사용해봤다.



exception이 발생하면 Await.result는 crash exception이 발생하고, 

Await.ready는 exception을 Failure로 감싼다. 

 


Posted by '김용환'
,