2017-06-19 37 views
0

我使用Scala的2.11.11一个类型,elastic4s 5.4.5和elastic4s-瑟茜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) 
    } 

} 

这不会编译:

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) 

根据文档:

只需添加导入您所选择的库下面,然后用在那些规模implicits,你现在可以通过任何键入你喜欢的文档和一个索引将自动派生。

用于弹性4s-circe的import io.circe.generic.auto._import com.sksamuel.elastic4s.circe._

我缺少什么?

+0

您是否尝试过使用不同的JSON编组。例如,你应该没有这样的问题。 – monkjack

回答

0

您需要定义Test对象范围之外的case classes,即在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) 
+0

它不工作... 而我不明白为什么它可以工作,当我把一个自定义键放在'Something' case类内的映射,即如果我尝试索引一个AnotherThing实例只是编译而已。 –