我正在尝试使用 DocumentMap 创建批量索引。我像这样映射类
case class Comment(id: String, fromId: String, fromName: String, message: String, creationTime: String, likeCount: Int =0)
extends DocumentMap {
def map = Map("id" -> id, "fromId" -> fromId, "fromName" -> fromName, "message" -> message, "createdTime" -> creationTime, "likeCont" -> likeCount)
}
case class Post(id: String, fromId: String, fromName: String, message: String, fullUrl: String, createdTime: String, updateTime: String, likeCont: Int= 0, comments: List[Comment] = Nil)
extends DocumentMap {
def map = Map("id" -> id, "fromId" -> fromId, "fromName" -> fromName, "message" -> message, "fullUrl" -> fullUrl, "createdTime" -> createdTime, "updateTime" -> updateTime, "likeCount" -> likeCont,
"comments" -> comments)
}
这就是我索引数据的方式(现在我只能索引单个项目),
val test =jsonValue(0).as[Post]
client.execute {
index into "posts/test" doc test
}
我有两个问题
1.我应该在索引之前将属性注释映射为嵌套吗?因为现在所有列表都被索引为单个字符串。
2.如何索引帖子对象列表?现在我只能索引单个对象。
解决方案
1.首先也是非常重要的在索引之前创建一个映射。
2.使用这样的批量索引。
val ops = for (j <- jsonValue) yield index into "posts/test" doc j.as[Post]
client.bulk(ops: _*)
谢谢三木