0

I'm trying to create an index as shown before but I always get this error: Bad request For request 'POST /initIndex' [Invalid Json]

I'm using elastic4s with play Framework 2.3.x and scala 2.11.

import com.sksamuel.elastic4s.{ElasticClient, ElasticsearchClientUri, FrenchLanguageAnalyzer}
import com.sksamuel.elastic4s.ElasticDsl._
import com.sksamuel.elastic4s.mappings.FieldType._
import models.tools.Tool
import org.elasticsearch.action.get.GetResponse
import org.elasticsearch.action.search.SearchResponse

import scala.concurrent.Future

object ToolsDaoImpl {

  private val uri: ElasticsearchClientUri = ElasticsearchClientUri("elasticsearch://localhost:9200")
  private val client = ElasticClient.remote(uri)    

  def createIndex {
    client execute {
      create index name mappings (
        "tool" as (
            "id" typed IntegerType,
            "title" typed StringType analyzer FrenchLanguageAnalyzer,
            "description" typed StringType analyzer FrenchLanguageAnalyzer
          )
        )
    }
  }

  def findById(toolId: Long): Future[GetResponse] = client.execute {
    get id toolId from "tools/tool"
  }

  def findByName(name: String): Future[SearchResponse] = client.execute {
    search in "tools/tool" query name
  }

  def initIndex {
    client.execute {
      index into "tools/tool" doc Tool(id = 1L, title = "Marteau", description = "Peut être utilisé pour differents travaux")
      index into "tools/tool" doc Tool(id = 1L, title = "Perceuse", description = "Placoplatre et béton")
    }
  }

}

case class Tool(id: Long, title: String, city: String, description: String) extends DocumentMap {
  override def map: Map[String, Any] = Map("id" -> id, "title" -> title, "description" -> description)
}

And it is invoked directly from the controller. Nothing else

Any idea ?

Thanks in advance.

Edit: I tried this code on a simple scala app (a main) and it works. What could ne thé reason ?

Marouane Gazanayi
  • 5,063
  • 6
  • 39
  • 58

2 Answers2

2

Since the code above was not the issue I imagine this is with your Reads or JSON you are sending in - please provide more details about the model you are expecting, and the Reads

Barry
  • 1,800
  • 2
  • 25
  • 46
1

It may be that your example is incomplete, but from the code above you have two open curly braces and one one close curly brace.

Joe Fair
  • 21
  • 2