elastic4s icon indicating copy to clipboard operation
elastic4s copied to clipboard

HitReader supplies Hit trait not SearchHit

Open pwheimann opened this issue 4 years ago • 1 comments

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.

pwheimann avatar Mar 31 '21 18:03 pwheimann

This is a good point. I'll make a change in the 8.0 release where I can change the API a bit.

sksamuel avatar Mar 31 '21 19:03 sksamuel