1

I am trying to pass a string of JSON data into an indexing statement like this, where inputDoc is my string of JSON:

def Update(client: ElasticClient, idx:String, `type`: String, inputDoc: String): Unit = {
    val address = idx + "/" + `type`

    client.execute {
      index into address doc inputDoc
    }
  }

I'm getting a compiler error that doc cannot be resolved, I'm assuming because it is looking for either a DocumentSource or DocumentMap rather than the String I am passing.

Based on this documentation it looks to me like I should be able to pass a String that Jackson will marshall into JSON. Based on this thread though, it sounds to me like elastic4s doesn't support indexing a JSON string without creating a template for it.

My question is two-fold: 1) Is it possible to index a JSON string without creating a case class to map the data? 2) If that is possible, is it advisable? Or is that discouraged because it's preferable to know the structure of the data I'm indexing rather than just adding in a JSON string I haven't necessarily checked?

I'm working with ElasticSearch version 2.1.1. Here's my elastic4s dependency:

"com.sksamuel.elastic4s" %% "elastic4s-core" % "2.1.1"
Community
  • 1
  • 1
DanHam
  • 340
  • 2
  • 17

1 Answers1

1

There's no reason why you have to have a strongly typed data structure before you index your data. In some cases that's preferable (if you're working with the data in Scala for example), but in other cases you might just be acting as a gateway (for example, a process that reads from HDFS and indexes the files).

To do what you want, doc expects a DocumentSource, so just create a JsonDocumentSource which wraps a Json string, by doing JsonDocumentSource(yourjsonhere).

So your full example would look something like:

client.execute {
  index into "address/type" doc JsonDocumentSource(inputDoc)
}
sksamuel
  • 16,154
  • 8
  • 60
  • 108