Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'master' into optimizeme

  • Loading branch information...
commit ece99e830c210da2ccc5cab67b8589502d3ea20d 2 parents 40aab3d + d6589ed
@holdenk holdenk authored
View
4 build.sbt
@@ -1,6 +1,6 @@
name := "slashem"
-version := "0.10.2"
+version := "0.11.1"
organization := "com.foursquare"
@@ -27,7 +27,7 @@ libraryDependencies <++= (scalaVersion) { scalaVersion =>
"junit" % "junit" % "[4.8.2,)" % "test",
"com.novocode" % "junit-interface" % "[0.7,)" % "test" ,
"org.scala-tools.testing" %% "specs" % specsVersion % "test",
- "org.elasticsearch" % "elasticsearch" % "0.19.3" % "compile" exclude("log4j", "log4j") exclude("com.sun.jmx","jmxri") exclude("com.sun.jdmk","jmxtools") exclude("com.codahale","jerkson_2.8.1") exclude("com.codahale","jerkson") exclude("com.twitter","streamyj_2.8.1") exclude("org.codehaus.jackson" , "jackson-mapper-asl") exclude("org.codehas.jackson" , "jackson-core-asl"),
+ "org.elasticsearch" % "elasticsearch" % "0.19.4" % "compile" exclude("log4j", "log4j") exclude("com.sun.jmx","jmxri") exclude("com.sun.jdmk","jmxtools") exclude("com.codahale","jerkson_2.8.1") exclude("com.codahale","jerkson") exclude("com.twitter","streamyj_2.8.1") exclude("org.codehaus.jackson" , "jackson-mapper-asl") exclude("org.codehas.jackson" , "jackson-core-asl"),
"org.codehaus.jackson" % "jackson-mapper-asl" % "1.8.8",
"org.codehaus.jackson" % "jackson-core-asl" % "1.8.8",
"org.scala-tools.testing" %% "scalacheck" % scalaCheckVersion % "test",
View
61 src/main/scala/com/foursquare/slashem/Schema.scala
@@ -984,6 +984,22 @@ class SlashemLongListField[T <: Record[T]](owner: T) extends LongListField[T](ow
def nin(lst: List[Long]) = Clause[Long](queryName, groupWithOr(lst.map({l: Long => Phrase(l)})),false)
}
+class SlashemObjectIdListField[T <: Record[T]](owner: T) extends ObjectIdListField[T](owner) with SlashemField[List[ObjectId], T] {
+ import Helpers._
+ override def valueBoxFromAny(a: Any) = objectIdBoxFromAny(a)
+ def contains(item: ObjectId) = {
+ Clause[ObjectId](queryName, Phrase(item))
+ }
+ /**
+ * See if this list has any elements in that list.
+ * @param List[ObjectId] the list to check for any intersections.
+ */
+ def in(lst: List[ObjectId]) = Clause[ObjectId](queryName, groupWithOr(lst.map({oid: ObjectId => Phrase(oid)})))
+ def nin(lst: List[ObjectId]) = Clause[ObjectId](queryName, groupWithOr(lst.map({oid: ObjectId => Phrase(oid)})),false)
+}
+
+
+
class SlashemPointField[T <: Record[T]](owner: T) extends PointField[T](owner) with SlashemField[Pair[Double,Double], T] {
def geoDistance(geolat: Double, geolng: Double) = {
GeoDist(this.name,geolat,geolng)
@@ -1075,7 +1091,7 @@ class ObjectIdField[T <: Record[T]](override val owner: T) extends Field[ObjectI
def valueBoxFromAny(a: Any): Box[ObjectId] = objectIdBoxFromAny(a)
- override def setFromAny(a: Any) ={
+ override def setFromAny(a: Any) = {
val vb = valueBoxFromAny(a)
vb.map(set(_))
}
@@ -1191,6 +1207,49 @@ class LongListField[T <: Record[T]](override val owner: T) extends Field[List[Lo
override def valueBox() = e
}
+class ObjectIdListField[T <: Record[T]](override val owner: T) extends Field[List[ObjectId], T] {
+ type ValueType = List[ObjectId]
+ var e: Box[ValueType] = Empty
+
+ def setFromString(s: String) = {
+ Full(set(s.split(" ").map(x => new ObjectId(x)).toList))
+ }
+ // NOTE(simon): See notes for ObjectIdField#objectIdBoxFromAny
+ def objectIdBoxFromAny(a: Any): Box[List[ObjectId]] = {
+ try {
+ a match {
+ case "" => Empty
+ case ar: Array[ObjectId] => Full(ar.toList)
+ case ar: Array[String] => Full(ar.toList.map(x => new ObjectId(x)))
+ case s: String => Full(s.split(" ").map(x => new ObjectId(x)).toList)
+ case _ => Empty
+ }
+ } catch {
+ case _ => Empty
+ }
+ }
+ def valueBoxFromAny(a: Any): Box[List[ObjectId]] = objectIdBoxFromAny(a)
+ override def setFromAny(a: Any) = {
+ val vb = valueBoxFromAny(a)
+ vb.map(set(_))
+ }
+ override def setFromJValue(jv: net.liftweb.json.JsonAST.JValue) = Empty
+ override def liftSetFilterToBox(a: Box[ValueType]) = Empty
+ override def toBoxMyType(a: ValueType) = Empty
+ override def defaultValueBox = Empty
+ override def toValueType(a: Box[MyType]) = null.asInstanceOf[ValueType]
+ override def asJValue() = net.liftweb.json.JsonAST.JNothing
+ override def asJs() = net.liftweb.http.js.JE.JsNull
+ override def toForm = Empty
+ override def set(a: ValueType) = {e = Full(a)
+ a.asInstanceOf[ValueType]}
+ override def get() = e.get
+ override def is() = e.get
+ def value() = e getOrElse Nil
+ override def valueBox() = e
+}
+
+
class StringListField[T <: Record[T]](override val owner: T) extends Field[List[String], T] {
type ValueType = List[String]
var e: Box[ValueType] = Empty
View
28 src/test/scala/com/foursquare/slashem/ElasticQueryTest.scala
@@ -236,6 +236,16 @@ class ElasticQueryTest extends SpecsMatchers with ScalaCheckMatchers {
lastResult.map(doc => Assert.assertEquals(new ObjectId("4c809f4251ada1cdc3790b18"), doc.id.is))
}
@Test
+ def testPhraseOnlyMatch {
+ val phrase = "loler skates"
+ val rPhraseOnly = ESimplePanda where (_.name eqs "loler skates") fetch()
+ val rContains = ESimplePanda where (_.name contains "loler skates") fetch()
+ rPhraseOnly.response.results.map(d => Assert.assertTrue(d.name.value.contains("loler skates")))
+ val phraseCount = rPhraseOnly.response.results.length
+ val containsCount = rContains.response.results.length
+ Assert.assertTrue(containsCount > phraseCount)
+ }
+ //@Test
def testFieldFaceting {
val r = ESimplePanda where (_.name contains "loler skates") facetField(_.foreign) fetch()
Assert.assertEquals(4,r.response.results.length)
@@ -304,6 +314,7 @@ class ElasticQueryTest extends SpecsMatchers with ScalaCheckMatchers {
val response7 = ESimplePanda where (_.hugenums contains 1L) fetch()
val response8 = ESimplePanda where (_.hugenums contains 9L) fetch()
val response9 = ESimplePanda where (_.hugenums contains 9001L) fetch()
+ val response10 = ESimplePanda where (_.favvenueids contains new ObjectId("4daf213893a0096fbaaef003")) fetch()
Assert.assertEquals(response1.response.results.length, 2)
Assert.assertEquals(response2.response.results.length, 1)
Assert.assertEquals(response3.response.results.length, 2)
@@ -313,6 +324,7 @@ class ElasticQueryTest extends SpecsMatchers with ScalaCheckMatchers {
Assert.assertEquals(response7.response.results.length, 2)
Assert.assertEquals(response8.response.results.length, 1)
Assert.assertEquals(response9.response.results.length, 0)
+ Assert.assertEquals(response10.response.results.length, 1)
}
@Test
@@ -320,9 +332,11 @@ class ElasticQueryTest extends SpecsMatchers with ScalaCheckMatchers {
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 (_.termsfield in List("termhit", "lol")) fetch()
+ val response4 = ESimplePanda where (_.favvenueids in List(new ObjectId("4daf213893a0096fbaaef003"))) fetch()
Assert.assertEquals(response1.response.results.length, 2)
Assert.assertEquals(response2.response.results.length, 0)
Assert.assertEquals(response3.response.results.length, 1)
+ Assert.assertEquals(response4.response.results.length, 1)
}
@Test
@@ -342,6 +356,18 @@ class ElasticQueryTest extends SpecsMatchers with ScalaCheckMatchers {
}
@Test
+ def testObjectIdListFieldEmptyIn {
+ val response1 = ESimplePanda where (_.favvenueids in List()) fetch()
+ Assert.assertEquals(response1.response.results.length, 0)
+ }
+
+ @Test
+ def testObjectIdListFieldEmptyNin {
+ val response1 = ESimplePanda where (_.favvenueids nin List()) fetch()
+ Assert.assertEquals(response1.response.results.length, 8)
+ }
+
+ @Test
def testListFieldNin {
val idsWithFavNums = List(new ObjectId("4c809f4251ada1cdc3790b10"),
new ObjectId("4c809f4251ada1cdc3790b11"),
@@ -433,6 +459,7 @@ class ElasticQueryTest extends SpecsMatchers with ScalaCheckMatchers {
val hugenums1 = List(1L, 2L, 3L).asJava
val hugenums2 = List(1L, 9L, 8L).asJava
val hugenums3 = List(7L, 10L, 13L).asJava
+ val venueids1 = List("4daf213893a0096fbaaef003", "49ee02e9f964a52010681fe3", "42b21280f964a5206d251fe3").asJava
val r = client.prepareIndex(ESimplePanda.meta.indexName,ESimplePanda.meta.docType,"4c809f4251ada1cdc3790b10").setSource(jsonBuilder()
.startObject()
.field("name","lolerskates")
@@ -441,6 +468,7 @@ class ElasticQueryTest extends SpecsMatchers with ScalaCheckMatchers {
.field("nicknames", nicknames1)
.field("hugenums", hugenums1)
.field("termsfield", terms1)
+ .field("favvenueids", venueids1)
.endObject()
).execute()
.actionGet();
View
1  src/test/scala/com/foursquare/slashem/ElasticTest.scala
@@ -19,6 +19,7 @@ class ESimplePanda extends ElasticSchema[ESimplePanda] {
object nicknames extends SlashemStringListField(this)
object hugenums extends SlashemLongListField(this)
object termsfield extends SlashemUnanalyzedStringField(this)
+ object favvenueids extends SlashemObjectIdListField(this)
}
object ESimpleGeoPanda extends ESimpleGeoPanda with ElasticMeta[ESimpleGeoPanda] {
Please sign in to comment.
Something went wrong with that request. Please try again.