简体   繁体   English

在Play框架中使用Elastic4s创建Elasticsearch索引时出现无效的Json错误

[英]Invalid Json error when creating Elasticsearch index with elastic4s in play framework

I'm trying to create an index as shown before but I always get this error: Bad request For request 'POST /initIndex' [Invalid Json] 我试图创建一个如上所示的索引,但我总是收到此错误:错误的请求对于请求'POST / initIndex'[无效的Json]

I'm using elastic4s with play Framework 2.3.x and scala 2.11. 我正在将Elastic4s与Play Framework 2.3.x和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. 编辑:我在一个简单的scala应用程序(一个主程序)上尝试了此代码,并且可以正常工作。 What could ne thé reason ? 请问什么原因?

由于上面的代码不是问题,我想这与您发送的Reads或JSON有关-请提供有关您期望的模型和Reads的更多详细信息

可能是您的示例不完整,但是从上面的代码中,您有两个打开的花括号和一个关闭的花括号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM