git.iko.soy/typeable/bloodhound
on master help sign in
regex working
commit833e1eb4c03c2a97d05aab630e9d89f070554c83 authorChris Allen <cma@bitemyapp.com> dateFri 11 Apr 2014, 01:08 (13 years ago) parentd42111c
2 files changed+51−2
Database/Bloodhound/Client.hs+44−2 tests/tests.hs+7
Database/Bloodhound/Client.hs+44−2
@@ -45,6 +45,8 @@ module Database.Bloodhound.Client
4545 , LessThanEq(..)
4646 , GreaterThan(..)
4747 , GreaterThanEq(..)
48+ , Regexp(..)
49+ , RegexpFlags(..)
4850 )
4951 where
5052
@@ -370,8 +372,10 @@ queryStringQuery query = emptyQueryStringQuery { query = query }
370372
371373type FieldName = Text
372374
373−type Cache = Bool -- caching on/off
374−defaultCache = False
375+type Cache = Bool -- caching on/off
376+type CacheName = Text
377+type CacheKey = Text
378+defaultCache = False
375379
376380type Existence = Bool
377381type NullValue = Bool
@@ -392,6 +396,7 @@ data Filter = AndFilter [Filter] Cache
392396 | MissingFilter FieldName Existence NullValue
393397 | PrefixFilter FieldName PrefixValue Cache
394398 | RangeFilter FieldName (Either HalfRange Range) RangeExecution Cache
399+ | RegexpFilter FieldName Regexp RegexpFlags CacheName Cache CacheKey
395400 deriving (Eq, Show)
396401
397402class Monoid a => Seminearring a where
@@ -497,6 +502,15 @@ instance ToJSON Filter where
497502 where
498503 (lessKey, lessVal, greaterKey, greaterVal) = rangeToKV range
499504
505+ toJSON (RegexpFilter fieldName (Regexp regexText) flags cacheName cache cacheKey) =
506+ object ["regexp" .=
507+ object [fieldName .=
508+ object ["value" .= regexText
509+ , "flags" .= toJSON flags]
510+ , "_name" .= cacheName
511+ , "_cache" .= cache
512+ , "_cache_key" .= cacheKey]]
513+
500514instance ToJSON GeoPoint where
501515 toJSON (GeoPoint geoField latLon) =
502516 object [geoField .= toJSON latLon]
@@ -586,6 +600,34 @@ instance ToJSON RangeExecution where
586600 toJSON RangeExecutionIndex = "index"
587601 toJSON RangeExecutionFielddata = "fielddata"
588602
603+newtype Regexp = Regexp Text deriving (Eq, Show)
604+data RegexpFlags = RegexpAll
605+ | Complement
606+ | Interval
607+ | Intersection
608+ | AnyString
609+ | CompInterval
610+ | CompIntersection
611+ | CompAnyString
612+ | IntervalIntersection
613+ | IntervalAnyString
614+ | IntersectionAnyString deriving (Eq, Show)
615+
616+instance ToJSON RegexpFlags where
617+ toJSON RegexpAll = String "ALL"
618+ toJSON Complement = String "COMPLEMENT"
619+ toJSON Interval = String "INTERVAL"
620+ toJSON Intersection = String "INTERSECTION"
621+ toJSON AnyString = String "ANYSTRING"
622+ toJSON CompInterval = String "COMPLEMENT|INTERVAL"
623+ toJSON CompIntersection = String "COMPLEMENT|INTERSECTION"
624+ toJSON CompAnyString = String "COMPLEMENT|ANYSTRING"
625+ toJSON IntervalIntersection = String "INTERVAL|INTERSECTION"
626+ toJSON IntervalAnyString = String "INTERVAL|ANYSTRING"
627+ toJSON IntersectionAnyString = String "INTERSECTION|ANYSTRING"
628+
629+-- phew. Coulda used Agda style case breaking there but, you know, whatever. :)
630+
589631data Term = Term { termField :: Text
590632 , termValue :: Text } deriving (Eq, Show)
591633
tests/tests.hs+7
@@ -196,3 +196,10 @@ main = hspec $ do
196196 let search = Search Nothing (Just filter)
197197 myTweet <- searchTweet search
198198 myTweet `shouldBe` Right exampleTweet
199+
200+ it "returns document for regexp filter" $ do
201+ _ <- insertData
202+ let filter = RegexpFilter "user" (Regexp "bite.*app") RegexpAll "test" False "key"
203+ let search = Search Nothing (Just filter)
204+ myTweet <- searchTweet search
205+ myTweet `shouldBe` Right exampleTweet