首页 文章

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

提问于
浏览
0

我正在尝试创建一个索引,如前所示,但我总是得到这个错误:错误的请求请求'POST / initIndex'[无效的Json]

我正在使用带有游戏框架2.3.x和scala 2.11的elastic4s .

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

它直接从控制器调用 . 没有其他的

任何的想法 ?

提前致谢 .

编辑:我在一个简单的scala应用程序(主要)上尝试了这个代码,它的工作原理 . 原因是什么?

2 回答

  • 2

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

  • 1

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

相关问题