Skip to content

Commit

Permalink
#106 Fix json serialisation
Browse files Browse the repository at this point in the history
  • Loading branch information
To-om committed Jun 22, 2022
1 parent d0248e2 commit 5ad8b95
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions app/org/elastic4play/database/DBUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ object DBUtils {
.map {
case f if f.startsWith("+") => f.drop(1) -> fieldSort(f.drop(1)).order(SortOrder.ASC)
case f if f.startsWith("-") => f.drop(1) -> fieldSort(f.drop(1)).order(SortOrder.DESC)
case f if f.length() > 0 => f -> fieldSort(f)
case f if f.nonEmpty => f -> fieldSort(f)
}
// then remove duplicates
// Same as : val fieldSortDefs = byFieldList.groupBy(_._1).map(_._2.head).values.toSeq
Expand All @@ -23,13 +23,25 @@ object DBUtils {
.map(_._2) :+ fieldSort("_doc").order(SortOrder.DESC)
}

/**
* Transform search hit into JsObject
private def toJson(any: Any): JsValue =
any match {
case m: Map[_, _] => JsObject(m.toSeq.map { case (k, v) => k.toString -> toJson(v) })
case s: String => JsString(s)
case l: Long => JsNumber(l)
case i: Int => JsNumber(i)
case d: Double => JsNumber(d)
case f: Float => JsNumber(f)
case b: Boolean => JsBoolean(b)
case null => JsNull
case s: Seq[_] => JsArray(s.map(toJson))
}

/** Transform search hit into JsObject
* This function parses hit source add _type, _routing, _parent, _id, _seqNo and _primaryTerm attributes
*/
def hit2json(hit: SearchHit): JsObject = {
val id = JsString(hit.id)
val body = Json.parse(hit.sourceAsString).as[JsObject]
val body = toJson(hit.sourceAsMap).as[JsObject]
val (parent, model) = (body \ "relations" \ "parent").asOpt[JsString] match {
case Some(p) => p -> (body \ "relations" \ "name").as[JsString]
case None => JsNull -> (body \ "relations").as[JsString]
Expand Down

0 comments on commit 5ad8b95

Please sign in to comment.