Skip to content

Commit

Permalink
Merge pull request #33 from holdenk/master
Browse files Browse the repository at this point in the history
Fix for in/nin queries
  • Loading branch information
adamalix committed May 10, 2012
2 parents 20f1fa5 + 40a677c commit c4eb3ad
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 9 deletions.
17 changes: 14 additions & 3 deletions README.md
Expand Up @@ -24,6 +24,11 @@ The other hook is only useful if you are using Solr for geospatail information,
we provide a trait called SolrGeoHash which has two required functions, namely
coverString and rectCoverString. Most people will not need to implement this.

### Multiple Solr cores & non-stanrd query paths

Support for multiple cores is done by overriding "core" in the model (wich is an Option[String]).
If you have a non-standard query path you can override queryPath in your model.

## Examples

[QueryTest.scala](https://github.com/foursquare/slashem/blob/master/src/test/scala/com/foursquare/slashem/QueryTest.scala) contains sample queries and shows the corresponding query.
Expand All @@ -48,11 +53,17 @@ to do this:
## Dependencies

lift, joda-time, junit, finagle, jackson. These dependencies are managed by
the build system.
the build system. Note: some of the transitive dependencies may fail to resolve
from the central maven. If you are using sbt you can fix this by adding

## Warnings
ivyXML := (
<dependencies>
<exclude module="jmxtools"/>
<exclude module="jmxri"/>
</dependencies>
)

This is still a very early version. There are likely bugs (sorry!). Let us know
## Warnings still a very early version. There are likely bugs (sorry!). Let us know
if you find any. While we can't promise timely fixes, it will help :)

## Maintainers
Expand Down
9 changes: 8 additions & 1 deletion build.sbt
@@ -1,6 +1,6 @@
name := "slashem"

version := "0.9.13"
version := "0.9.15a"

organization := "com.foursquare"

Expand Down Expand Up @@ -123,4 +123,11 @@ pomExtra := (
<email>aalix@foursquare.com</email>
</developer>
</developers>
)

ivyXML := (
<dependencies>
<exclude module="jmxtools"/>
<exclude module="jmxri"/>
</dependencies>
)
8 changes: 5 additions & 3 deletions src/main/scala/com/foursquare/slashem/Ast.scala
Expand Up @@ -242,7 +242,7 @@ object Ast {
}
def elasticBoost(): Pair[List[String],String] = {
weight match {
case 1.0 => Pair(Nil,"(doc['" + fieldName + "'].value)")
case 1.0 => Pair(Nil,"doc['" + fieldName + "'].value")
case _ => Pair(Nil,"(doc['" + fieldName + "'].value *" + weight.toString + ")")
}
}
Expand Down Expand Up @@ -359,8 +359,10 @@ object Ast {
def extend(): String = "\"\""
/** @inheritdoc */
def elasticExtend(qf: List[WeightedField], pf: List[PhraseWeightedField], mm: Option[String]): ElasticQueryBuilder = {
val q = EQueryBuilders.queryString(this.extend())
qf.map(f => q.field(f.fieldName,f.weight.toFloat))
//An empty query matches no documents, so it is the same as the negation of matchAll
//Note: this is kind of ugly since this is may likely an OR clause or negated up above
//so we should try and avoid generating this
val q = EQueryBuilders.boolQuery.mustNot(EQueryBuilders.matchAllQuery())
q
}
}
Expand Down
22 changes: 20 additions & 2 deletions src/test/scala/com/foursquare/slashem/ElasticQueryTest.scala
Expand Up @@ -78,6 +78,14 @@ class ElasticQueryTest extends SpecsMatchers with ScalaCheckMatchers {
val r = fullQuery fetch()
}

@Test
def simpleBoostTest {
val fullQuery = ESimplePanda.where(_.name contains "lol")
.limit(5).boostField(_.followers)
val r = fullQuery fetch()
}


@Test
def testEmptySearch {
try {
Expand Down Expand Up @@ -305,10 +313,20 @@ class ElasticQueryTest extends SpecsMatchers with ScalaCheckMatchers {
def testListFieldIn {
val response1 = ESimplePanda where (_.favnums in List(2, 3, 4, 5)) fetch()
val response2 = ESimplePanda where (_.favnums in List(99)) fetch()
//val response3 = ESimplePanda where (_.favnums in List()) fetch()
Assert.assertEquals(response1.response.results.length, 2)
Assert.assertEquals(response2.response.results.length, 0)
//Assert.assertEquals(response3.response.results.length, 0)
}

@Test
def testIntListFieldEmptyIn {
val response = ESimplePanda where (_.favnums in List()) fetch()
Assert.assertEquals(response.response.results.length, 0)
}

@Test
def testIntListFieldEmptyNin {
val response = ESimplePanda where (_.favnums nin List()) fetch()
Assert.assertEquals(response.response.results.length, 8)
}

@Test
Expand Down
21 changes: 21 additions & 0 deletions src/test/scala/com/foursquare/slashem/QueryTest.scala
Expand Up @@ -69,6 +69,27 @@ class QueryTest extends SpecsMatchers with ScalaCheckMatchers {
"rows" -> "10").sortWith(_._1 > _._1))
}

@Test
def testProduceCorrectListfieldQueryStringWhenEmpty {
val q = SVenueTest where (_.commentList in List())
val qp = q.meta.queryParams(q).toList
Assert.assertEquals(qp.sortWith(_._1 > _._1),
List("q" -> "commentList:(\"\")",
"start" -> "0",
"rows" -> "10").sortWith(_._1 > _._1))
}
@Test
def testProduceCorrectListfieldQueryStringNinWhenEmpty {
val q = SVenueTest where (_.commentList nin List())
val qp = q.meta.queryParams(q).toList
Assert.assertEquals(qp.sortWith(_._1 > _._1),
List("q" -> "(*:* -commentList:(\"\"))",
"start" -> "0",
"rows" -> "10").sortWith(_._1 > _._1))
}



@Test
def testProduceCorrectSimpleQueryStringContains {
val q = SUserTest where (_.fullname contains "jon")
Expand Down

0 comments on commit c4eb3ad

Please sign in to comment.