3

在使用 Scala 一段时间并阅读所有地方,尤其是这里

我确信我知道什么时候使用卷发。根据经验,如果我想传递要执行的代码块,我将使用花括号。

这个讨厌的错误是如何使用花括号的 elastic4s DSL 浮出水面的:

bool {
  should {
    matchQuery("title", title)
  }
  must {
    termQuery("tags", category)
  }
}

编译为:

{
  "bool" : {
    "must" : {
      "term" : {
        "tags" : "tech"
      }
    }
  }
}

使用括号时:

bool {
       should (
         matchQuery("title", title)
        ) must (
         termQuery("tags", category)
        )
      }

给出正确的结果:

{
  "bool" : {
    "must" : {
      "term" : {
        "tags" : "tech"
      }
    },
    "should" : {
      "match" : {
        "title" : {
          "query" : "fake",
          "type" : "boolean"
        }
      }
    }
  }
}

这是使用 scala 2.11.6 编译的 - 更令人困惑的是,无论我使用什么,在 intellij 调试器中评估表达式都会给出正确的结果。

我注意到只有最后一个表达式正在被评估,为什么会这样?

4

1 回答 1

8

可能不是大括号的问题,而是中缀符号的问题。看线条

  should {
    matchQuery("title", title)
  }
  must {

must进入下一行,因此它被解释为新表达式,而不是should. 您可能必须将它与右大括号放在同一行

  should {
    matchQuery("title", title)
  } must {
于 2015-04-26T14:33:17.760 回答