elastic4s
elastic4s copied to clipboard
HitReader supplies Hit trait not SearchHit
I've just started using elastic4s and I have an issue with supplying a HitReader[T] implicit to marshal responses to my domain types. The issue is that the HitReader[T] typeclass provides instances of trait Hit not SearchHit and I need to get the sort values, and Hit doesn't expose this, but SearchHit does.
What I did was simply cast Hit to SearchHit and grab the sort values I need, like so...
override def read(hit: Hit): Try[Location] = {
val searchHit = hit.asInstanceOf[SearchHit]
val distance = searchHit.sort.get(0).asInstanceOf[Double]
val source = hit.sourceAsMap
This works, but it seems like there should be a better way, should the sort values be exposed in the Hit trait? Or perhaps there is a better/preferred way to do this that I'm just not seeing?
I'm using release 7.9.2 as I'm on Elasticsearch 7.9 currently.
This is a good point. I'll make a change in the 8.0 release where I can change the API a bit.