Skip to content

Commit

Permalink
Merge pull request tumblr#397 from tumblr/felix-remove_toplevel
Browse files Browse the repository at this point in the history
Remove toplevel parameter from solr
  • Loading branch information
defect committed Feb 26, 2016
2 parents 0ee98b5 + 3a5755f commit 9083a95
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
27 changes: 13 additions & 14 deletions app/collins/solr/CollinsQueryLanguage.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ case class CQLQuery(select: SolrDocType, where: SolrExpression) {
* itself into a Solr Query
*/
sealed trait SolrQueryComponent {
protected[solr] def traverseQueryString(): String = traverseQueryString(true)
protected[solr] def traverseQueryString(toplevel: Boolean): String
def traverseQueryString(): String
}

/**
Expand All @@ -46,15 +45,15 @@ sealed trait SolrValue {
abstract class SolrSingleValue(val valueType: ValueType) extends SolrValue with SolrQueryComponent

case class SolrIntValue(value: Int) extends SolrSingleValue(Integer) {
def traverseQueryString(toplevel: Boolean) = value.toString
def traverseQueryString() = value.toString
}

case class SolrDoubleValue(value: Double) extends SolrSingleValue(Double) {
def traverseQueryString(toplevel: Boolean) = value.toString
def traverseQueryString() = value.toString
}

case class SolrStringValue(value: String, quoteFormat: StringValueFormat = Unquoted) extends SolrSingleValue(String) {
def traverseQueryString(toplevel: Boolean) = quoteFormat.format(value)
def traverseQueryString() = quoteFormat.format(value)

def quoted = copy(quoteFormat = Quoted)
def unquoted = copy(quoteFormat = Unquoted)
Expand All @@ -66,7 +65,7 @@ case class SolrStringValue(value: String, quoteFormat: StringValueFormat = Unquo
}

case class SolrBooleanValue(value: Boolean) extends SolrSingleValue(Boolean) {
def traverseQueryString(toplevel: Boolean) = if (value) "true" else "false"
def traverseQueryString() = if (value) "true" else "false"

}

Expand Down Expand Up @@ -112,12 +111,12 @@ sealed trait SolrExpression extends SolrQueryComponent {
*/
sealed trait TypedSolrExpression extends SolrExpression {
//only type-checked expressions can be turned into solr queries
def toSolrQueryString: String = traverseQueryString(true)
def toSolrQueryString: String = traverseQueryString()
}

sealed trait _EmptySolrQuery extends SolrExpression {
def typeCheck(t: SolrDocType) = Right(TypedEmptySolrQuery)
def traverseQueryString(toplevel: Boolean) = "*:*"
def traverseQueryString() = "*:*"
}

case object EmptySolrQuery extends _EmptySolrQuery
Expand All @@ -134,10 +133,10 @@ abstract class SolrMultiExpr(exprs: Set[SolrExpression], op: String) extends Sol
//NOTE -
def createOp(exprs: Set[SolrExpression]): TypedSolrExpression

def traverseQueryString(toplevel: Boolean) = {
def traverseQueryString() = {
val e = exprs
.map { _.traverseQueryString(false) }.mkString(" %s ".format(op))
if (toplevel) e else "(%s)".format(e)
.map { _.traverseQueryString() }.mkString(" %s ".format(op))
"(%s)".format(e)
}

def typeCheck(t: SolrDocType) = {
Expand Down Expand Up @@ -264,21 +263,21 @@ case class SolrNotOp(expr: SolrExpression) extends SolrSimpleExpr {

def typeCheck(t: SolrDocType) = expr.typeCheck(t).right.map { e => new SolrNotOp(e) with TypedSolrExpression }

def traverseQueryString(toplevel: Boolean) = "-" + expr.traverseQueryString(false)
def traverseQueryString() = "-" + expr.traverseQueryString()

}

case class SolrKeyVal(key: String, value: SolrSingleValue) extends SolrSimpleExpr {

def traverseQueryString(toplevel: Boolean) = key + ":" + value.traverseQueryString(false)
def traverseQueryString() = key + ":" + value.traverseQueryString()

def typeCheck(t: SolrDocType) = typeCheckValue(t, key, value).right.map { case (solrKey, cleanValue) => new SolrKeyVal(solrKey, cleanValue) with TypedSolrExpression }

}

case class SolrKeyRange(key: String, low: Option[SolrSingleValue], high: Option[SolrSingleValue], inclusive: Boolean) extends SolrSimpleExpr {

def traverseQueryString(toplevel: Boolean) = {
def traverseQueryString() = {
val l = low.map { _.traverseQueryString }.getOrElse("*")
val h = high.map { _.traverseQueryString }.getOrElse("*")
if (inclusive) {
Expand Down
12 changes: 6 additions & 6 deletions test/collins/util/SolrSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -662,13 +662,13 @@ class SolrQuerySpec extends mutable.Specification {

"handle character escaping" in {
"quoted" in {
S("04:7d:7b:06:8f:f9", Q).traverseQueryString(false) must_== """"04:7d:7b:06:8f:f9""""
S("04:7d:7b:06:8f:f9", Q).traverseQueryString() must_== """"04:7d:7b:06:8f:f9""""
}
"wildcard" in {
S("04:7d:7b:06:8", R).traverseQueryString(false) must_== """04\:7d\:7b\:06\:8*"""
S("04:7d:7b:06:8", R).traverseQueryString() must_== """04\:7d\:7b\:06\:8*"""
}
"strict unquoted" in {
S("04:7d:7b:06:8f:f9", StrictUnquoted).traverseQueryString(false) must_== """04\:7d\:7b\:06\:8f\:f9"""
S("04:7d:7b:06:8f:f9", StrictUnquoted).traverseQueryString() must_== """04\:7d\:7b\:06\:8f\:f9"""
}
}
}
Expand Down Expand Up @@ -714,10 +714,10 @@ class SolrQuerySpec extends mutable.Specification {
"""foo = [abc, abd]""".query.where.traverseQueryString must_== """foo:[abc TO abd]"""
}
"ANDs" in {
"""foosolr = 3 AND bar = "abcdef" AND baz = true""".query.where.traverseQueryString must_== """foosolr:"3" AND bar:"abcdef" AND baz:"true""""
"""foosolr = 3 AND bar = "abcdef" AND baz = true""".query.where.traverseQueryString must_== """(foosolr:"3" AND bar:"abcdef" AND baz:"true")"""
}
"ORs" in {
"""foosolr = 3 OR bar = "abcdef" OR baz = true""".query.where.traverseQueryString must_== """foosolr:"3" OR bar:"abcdef" OR baz:"true""""
"""foosolr = 3 OR bar = "abcdef" OR baz = true""".query.where.traverseQueryString must_== """(foosolr:"3" OR bar:"abcdef" OR baz:"true")"""
}
"NOT" in {
"""NOT foosolr = 3""".query.where.traverseQueryString must_== """-foosolr:"3""""
Expand All @@ -726,7 +726,7 @@ class SolrQuerySpec extends mutable.Specification {
"""NOT (foosolr = 3 AND foosolr = 5)""".query.where.traverseQueryString must_== """-(foosolr:"3" AND foosolr:"5")"""
}
"nested exprs" in {
"""(foosolr = 3 OR foosolr = 4) AND (bar = true OR (bar = false AND baz = 5))""".query.where.traverseQueryString must_== """(foosolr:"3" OR foosolr:"4") AND (bar:"true" OR (bar:"false" AND baz:"5"))"""
"""(foosolr = 3 OR foosolr = 4) AND (bar = true OR (bar = false AND baz = 5))""".query.where.traverseQueryString must_== """((foosolr:"3" OR foosolr:"4") AND (bar:"true" OR (bar:"false" AND baz:"5")))"""
}
"support unquoted one-word strings" in {
"""foosolr = bar""".query.where.traverseQueryString must_== """foosolr:"bar""""
Expand Down

0 comments on commit 9083a95

Please sign in to comment.