结果文档缺少字段

时间:2016-08-18 20:13:15

标签: scala elasticsearch elastic4s

我一直在使用Elastic4s手册中的这个例子。它正常工作,直到它尝试检索没有在case类中指定的字段的文档。

在本手册中的示例中,假设一个结果只有name且缺少location字段。它会产生这个错误:

  

java.util.NoSuchElementException:找不到密钥:location

我正在寻找一种处理具有不同字段的搜索结果的好方法。

代码示例:

case class Character(name: String, location: String)

implicit object CharacterHitAs extends HitAs[Character] {
  override def as(hit: RichSearchHit): Character = {
Character(hit.sourceAsMap("name").toString, hit.sourceAsMap("location").toString) }}

val resp = client.execute {
search in "gameofthrones" / "characters" query "kings landing"
}.await

val characters :Seq[Character] = resp.as[Character]

1 个答案:

答案 0 :(得分:0)

使用可选参数开发case class时,请使用Option

case class Character(name: String, location: Option[String])

Character("Tyrion Lannister", None)

然后你要做的就是修改你的数据提取器,如果它没有找到数据就传递一个无选项:

val tyrion = Map("location" -> "King's Landing", "name" -> "Cersei Lannister")
val cersei = Map("father" -> "Tywin Lannister?", "name" -> "Cersei Lannister")
val jaime = Map("father" -> "Tywin Lannister", "location" -> "Tower of the Hand")
val characters = List(tyrion, cersei, jaime)

case class Character(name: String, location: Option[String])

characters.map(x => Character(x.getOrElse("name", "A CHARACTER HAS NO NAME"), x.get("location")))

characters.map(...)的结果是:

res0: List[Character] = List(
        Character(Cersei Lannister,Some(King's Landing)), 
        Character(Cersei Lannister,None), 
        Character(A CHARACTER HAS NO NAME NAME,Some(Tower of the Hand)))

RichSearchHit的源代码,sourceAsMap应返回Map个对象:

def sourceAsMap: Map[String, AnyRef] = if (java.sourceAsMap == null) Map.empty else java.sourceAsMap.asScala.toMap

鉴于您使用Map速记,您应该能够将代码转换为:

case class Character(name: String, location: Option[String])

implicit object CharacterHitAs extends HitAs[Character] {
  override def as(hit: RichSearchHit): Character = {
Character(hit.sourceAsMap.getOrElse("name", "A CHARACTER HAS NO NAME"), hit.sourceAsMap.get("location")) }}