Cannot generate indexable for a type containing a Map with a custom Key type

时间:2017-06-19 13:55:02

标签: scala elasticsearch elastic4s circe

I'm using scala 2.11.11, elastic4s 5.4.5 and elastic4s-circe 5.4.5

import com.sksamuel.elastic4s.ElasticDsl._
import com.sksamuel.elastic4s.TcpClient
import com.sksamuel.elastic4s.circe._
import io.circe.generic.auto._

object Test {

  val client: TcpClient = ???

  case class Something(a: Map[AnotherThing, Int])
  case class AnotherThing(b: Int)

  val smth = Something(Map.empty)

  client.execute {
    indexInto("index" / "type").doc(smth)
  }

}

This would not compile:

could not find implicit value for evidence parameter of type com.sksamuel.elastic4s.Indexable[net.lizeo.bd4m.storage.Test.Something]
indexInto("index" / "type").doc(smth)

According to the documentation:

Simply add the import for your chosen library below and then with those implicits in scope, you can now pass any type you like to doc and an Indexable will be derived automatically.

With import io.circe.generic.auto._and import com.sksamuel.elastic4s.circe._ for elastic4s-circe.

What am I missing?

2 个答案:

答案 0 :(得分:0)

您需要在case classes对象范围之外定义Test,即在Test类之外。您也可以将它们定义为单独的类。

正确的方法应该是

import com.sksamuel.elastic4s.ElasticDsl._
import com.sksamuel.elastic4s.TcpClient
import com.sksamuel.elastic4s.circe._
import io.circe.generic.auto._

object Test {

  val client: TcpClient = ???

  val smth = Something(Map.empty)

  client.execute {
    indexInto("index" / "type").doc(smth)
  }

}

case class Something(a: Map[AnotherThing, Int])
case class AnotherThing(b: Int)

答案 1 :(得分:0)

我没有使用Circe,但是我使用了SprayJson,也许我的回答可以帮助将来的人们研究这个问题:

对于Spary,我首先坚信getBounds()就足够了,但是Spray需要自定义案例类使用JsonProtocol,如下所示:

import com.sksamuel.elastic4s.sprayjson._

然后将它们都导入到代码的顶部:

object MyJsonProtocol extends DefaultJsonProtocol {
  implicit val smthFormat = jsonFormat1(Something)
  implicit val anotherThgFormat = jsonFormat1(AnotherThing)
}

我认为这是Circe的import MyJsonProtocol._ import com.sksamuel.elastic4s.sprayjson._ 中实现的自动魔术师部分,但也许它不适用于Elastic4s,您必须手动编写。