Skip to content

SearchHit.sourceAsString adds superfluous type metadata#3002

Description

@tastyminerals

I am stuggling to figure out how can one deserialize a valid JSON hit string into a SearchHit? We are using JSON string -> SearchHit pattern a lot in our tests and classic elasticsearch library SearchHit allowed to do the following:

    def sourceFixtureAsSearchHit(fileName: String, docId: Int = 1): SearchHit = {
        val fixture = loadUTF8FixtureAsString(fileName)
        val source = new BytesArray(fixture)
        val hit = new SearchHit(docId)
        hit.sourceRef(source)
        hit
    }

The elastic4s SearchHit doesn't provide sourceRef. Hence, we parse (via circe) a JSON string into a Map[String, Any]

val sourceMap = parser.parse(fixture).getOrElse(Json.Null)
  .asObject.map(_.toMap)
  .getOrElse(Map.empty[String, Json])

and then store it into elastic4s SearchHit(_source = sourceMap). This however produces a different JSON representation during serialization back via hit.sourceAsString. For example, the original JSON

   {"document_id" : "0b85846f-2c7b-4cc8-b265-6c3fdf1da815"}

becomes

{
  "document_id": {
   "value": "0b85846f-2c7b-4cc8-b265-6c3fdf1da815",
   "array": false,
   "null": false,
   "boolean": false,
   "number": false,
   "string": true,
   "object": false
 }
}

This drastically increases the resulting string size: 214 lines -> ~3k. So, this doesn't look like the correct way to create SearchHit from strings. So, how does one deserialize a string into SearchHit?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions