简体   繁体   English

是否可以/建议使用elastic4s为JSON数据的字符串编制索引?

[英]Is it possible/advisable to index a String of JSON data using elastic4s?

I am trying to pass a string of JSON data into an indexing statement like this, where inputDoc is my string of JSON: 我试图将一串JSON数据传递到这样的索引语句中,其中inputDoc是我的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. 我收到一个无法解决doc的编译器错误,我假设是因为它正在寻找DocumentSource或DocumentMap而不是我传递的String。

Based on this documentation it looks to me like I should be able to pass a String that Jackson will marshall into JSON. 根据该文档 ,在我看来,我应该能够传递杰克逊将编入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. 但是基于此线程 ,在我看来,elastic4s不支持在不为其创建模板的情况下索引JSON字符串。

My question is two-fold: 1) Is it possible to index a JSON string without creating a case class to map the data? 我的问题有两个:1)是否可以在不创建案例类来映射数据的情况下为JSON字符串建立索引? 2) If that is possible, is it advisable? 2)如果可行,是否建议? 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? 还是不建议这样做,因为最好是知道我正在索引的数据的结构,而不是仅仅添加一个不一定要检查的JSON字符串?

I'm working with ElasticSearch version 2.1.1. 我正在使用ElasticSearch 2.1.1版。 Here's my elastic4s dependency: 这是我的elastic4s依赖项:

"com.sksamuel.elastic4s" %% "elastic4s-core" % "2.1.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). 在某些情况下,这是更好的选择(例如,如果您在Scala中处理数据),但是在其他情况下,您可能只是充当网关(例如,从HDFS读取文件并为文件建立索引的过程)。

To do what you want, doc expects a DocumentSource , so just create a JsonDocumentSource which wraps a Json string, by doing JsonDocumentSource(yourjsonhere) . 做你想做的, doc需要一个DocumentSource ,所以只需要创建一个JsonDocumentSource它包装JSON字符串,这样做JsonDocumentSource(yourjsonhere)

So your full example would look something like: 因此,完整的示例如下所示:

client.execute {
  index into "address/type" doc JsonDocumentSource(inputDoc)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM