0

Im trying to create bulk index by using DocumentMap . i map the classes like that

 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)
  }

And this is how im indexing the data (now i can only index single item),

    val test   =jsonValue(0).as[Post]


  client.execute {
    index into "posts/test"  doc  test
  }

I have two questions

1.Should i map the property comments as nested before indexing ?? because now all the list is indexed as a single string.

2.How can index a list of post object?? now i can only index single object.

solution

1.first and very important crate a mapping before indexing.

2.use bulk index like that.

val ops = for (j <- jsonValue) yield index into "posts/test" doc j.as[Post] 

client.bulk(ops: _*)

thanks miki

MIkCode
  • 2,655
  • 5
  • 28
  • 46

1 Answers1

0

You can create a mapping and set the comments field to be typed as nested. Then all the comment fields would be indexed properly. By default it should be using inner type which flattens the fields, so that if comment has a field called author, all the authors would be grouped together.

To index multiple documents, use the bulk API. From the tests:

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" )

sksamuel
  • 16,154
  • 8
  • 60
  • 108
  • I got the mapping part , but what if i have N items of Post object.. How can i create a dynamic bulk syntax... – MIkCode May 05 '14 at 10:18
  • That's what the second part of the answer addresses. Use client.bulk() and just put as many index calls as you want inside the bulk(). – sksamuel May 05 '14 at 15:43
  • basically this what i want to do , val resp = client.bulk ( for (j <- jsonValue) { index into "posts/test" doc j.as[Post] } ) what is correct way to implement it ? – MIkCode May 05 '14 at 20:52
  • try client.bulk { for (j <- jsonValue) yield index into "posts/test" doc j.as[Post] } – sksamuel May 05 '14 at 21:12
  • im getting a type mismatch type mismatch; [error] found : List[com.sksamuel.elastic4s.ElasticDsl.IndexDefinition] [error] required: com.sksamuel.elastic4s.BulkCompatibleDefinition – MIkCode May 05 '14 at 21:19
  • An IndexDefinition is a BulkCompatibleDefinition. It's because the bulk method takes a var arg, so you'd have to do: `val ops = for (j <- jsonValue) yield index into "posts/test" doc j.as[Post]` `client.bulk(ops: _*)` Try that. – sksamuel May 06 '14 at 21:16