elastic4s icon indicating copy to clipboard operation
elastic4s copied to clipboard

Json4sHitReader should get _id from hit.

Open hanrw opened this issue 4 years ago • 3 comments

    implicit def Json4sHitReader[T](implicit json4s: Serialization, formats: Formats, mf: Manifest[T]): HitReader[T] =
      new HitReader[T] {
        override def read(hit: Hit): Try[T] = Try {
          json4s.read[T](hit.sourceAsString)
        }
      }

hanrw avatar Sep 28 '20 08:09 hanrw

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Nov 27 '20 08:11 stale[bot]

my solution may not good.

object ElasticJson4s {

  object Implicits {

    implicit def Json4sHitReader[T](implicit json4s: Serialization, formats: Formats, mf: Manifest[T]): HitReader[T] =
      new HitReader[T] {
        override def read(hit: Hit): Try[T] = Try {
          val metaFields = s"""{"_id": "${hit.id}","_type": "${hit.`type`}","_index": "${hit.index}" ,"_version": "${hit.version}", """
          val source = metaFields + hit.sourceAsString.substring(1)
          json4s.read[T](source)
        }
      }

    implicit def Json4sAggReader[T](implicit json4s: Serialization, formats: Formats, mf: Manifest[T]): AggReader[T] =
      new AggReader[T] {
        override def read(json: String): Try[T] = Try {
          json4s.read[T](json)
        }
      }

    implicit def Json4sIndexable[T <: AnyRef](implicit json4s: Serialization, formats: Formats): Indexable[T] =
      new Indexable[T] {
        override def json(t: T): String = json4s.write(t)
      }
  }

}

hanrw avatar Dec 06 '20 12:12 hanrw

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Feb 04 '21 15:02 stale[bot]