Permalink
Browse files

Add support for "raw" queries.

* In response to #35
  add support for queries that we do not wish to have escaped.
  • Loading branch information...
1 parent 40a677c commit 5f1ebaa70de4fb99506dcb6a8e1f4f89620b7b2c Holden Karau committed May 14, 2012
@@ -372,9 +372,15 @@ object Ast {
*
* Represents a contiguous series of words to be matched in that order.
*/
- case class Phrase[T](query: T, escaped: Boolean = true) extends Query[T] {
+ case class Phrase[T](query: T, escapeQuery: Boolean = true) extends Query[T] {
/** @inheritdoc */
- def extend(): String = {'"' + escape(query.toString) + '"'}
+ def extend(): String = {
+ if (escapeQuery) {
+ '"' + escape(query.toString) + '"'
+ } else {
+ '"' + query.toString + '"'
+ }
+ }
/** @inheritdoc */
def elasticExtend(qf: List[WeightedField], pf: List[PhraseWeightedField], mm: Option[String]): ElasticQueryBuilder = {
val q = EQueryBuilders.queryString(this.extend())
@@ -387,9 +393,15 @@ object Ast {
* A Phrase Prefix.
* @see Phrase
*/
- case class PhrasePrefix[T](query: T, escaped: Boolean = true) extends Query[T] {
+ case class PhrasePrefix[T](query: T, escapeQuery: Boolean = true) extends Query[T] {
/** @inheritdoc */
- def extend(): String = {'"' + escape(query.toString) + '*' + '"'}
+ def extend(): String = {
+ if (escapeQuery) {
+ '"' + escape(query.toString) + '*' + '"'
+ } else {
+ '"' + query.toString + '*' + '"'
+ }
+ }
/** @inheritdoc */
def elasticExtend(qf: List[WeightedField], pf: List[PhraseWeightedField], mm: Option[String]): ElasticQueryBuilder = {
val q = EQueryBuilders.disMaxQuery()
@@ -428,9 +440,15 @@ object Ast {
/**
* A class representing a Bag of words style query
*/
- case class BagOfWords[T](query: T) extends Query[T] {
+ case class BagOfWords[T](query: T, escapeQuery: Boolean = true) extends Query[T] {
/** @inheritdoc */
- def extend(): String = escape(query.toString)
+ def extend(): String = {
+ if (escapeQuery) {
+ escape(query.toString)
+ } else {
+ query.toString
+ }
+ }
/**
* @inheritdoc
@@ -794,11 +794,23 @@ trait SlashemField[V, M <: Record[M]] extends OwnedField[M] {
def eqs(v: V, b: Float) = Clause[V](self.queryName, Boost(Group(Phrase(v)),b))
def neqs(v: V, b:Float) = Clause[V](self.queryName, Boost(Phrase(v),b),false)
-
//This allows for bag of words style matching.
def contains(v: V) = Clause[V](self.queryName, Group(BagOfWords(v)))
def contains(v: V, b: Float) = Clause[V](self.queryName, Boost(Group(BagOfWords(v)),b))
+
+ //Search with explicit escaping. By normal we escape, set e to false to disable
+ //Note eqs and neqs results in phrase queries!
+ def eqs(v: V, e: Boolean) = Clause[V](self.queryName, Group(Phrase(v,e)))
+ def neqs(v: V, e: Boolean) = Clause[V](self.queryName, Phrase(v,e),false)
+ //With a boost
+ def eqs(v: V, b: Float, e: Boolean) = Clause[V](self.queryName, Boost(Group(Phrase(v,e)),b))
+ def neqs(v: V, b:Float, e: Boolean) = Clause[V](self.queryName, Boost(Phrase(v,e),b),false)
+ //This allows for bag of words style matching.
+ def contains(v: V, e: Boolean) = Clause[V](self.queryName, Group(BagOfWords(v,e)))
+ def contains(v: V, b: Float, e: Boolean) = Clause[V](self.queryName, Boost(Group(BagOfWords(v,e)),b))
+
+
def in(v: Iterable[V]) = Clause[V](self.queryName, groupWithOr(v.map({x: V => Phrase(x)})))
def nin(v: Iterable[V]) = Clause[V](self.queryName, groupWithOr(v.map({x: V => Phrase(x)})),false)
@@ -54,6 +54,16 @@ class QueryTest extends SpecsMatchers with ScalaCheckMatchers {
}
@Test
+ def testProduceCorrectSimpleUnEscapedQueryString {
+ val q = SUserTest where (_.fullname contains("jon*",false))
+ val qp = q.meta.queryParams(q).toList
+ Assert.assertEquals(qp.sortWith(_._1 > _._1),List("q" -> "fullname:(jon*)",
+ "start" -> "0",
+ "rows" -> "10").sortWith(_._1 > _._1))
+ }
+
+
+ @Test
def testProduceCorrectListfieldQueryStrings {
val q = SVenueTest where (_.commentList in List("hi", "there"))
val q2 = SVenueTest where (_.commentList nin List("hi", "there"))

0 comments on commit 5f1ebaa

Please sign in to comment.