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 ?

4
  • Did you copy paste this code or retype it? Commented Jan 19, 2015 at 18:19
  • @Barry copy past. Even without the = it does the same thing Commented Jan 20, 2015 at 11:14
  • 1
    Can you paste your routes and action you use to call this method? I copied/pasted the code above and in my local it creates the tools index with the appropriate mapping. Commented Jan 20, 2015 at 15:03
  • I was using a POST to the createIndex, I changed it to a GET and it worked... Any idea? Commented Jan 20, 2015 at 21:06

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thnx. I corrected the code and wrote the whole object
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.

1 Comment

The example is incomplete, I wrote the whole example now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.