0

我正在尝试使用 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: _*)

谢谢三木

4

1 回答 1

0

您可以创建映射并将注释字段设置为嵌套类型。然后所有评论字段都会被正确索引。默认情况下,它应该使用扁平化字段的内部类型,这样如果评论有一个名为作者的字段,所有作者将被分组在一起。

要索引多个文档,请使用批量 API。从测试中:

client bulk( index into "transport/air" id 1 fields "company" -> "ba", index into "transport/air" id 2 fields "company" -> "aeroflot", index into "transport/air" id 3 fields "company" -> "american air", index into "transport/air" id 4 fields "company" -> "egypt air" )

于 2014-05-04T21:14:28.600 回答