git.iko.soy/typeable/bloodhound
on master help sign in
BoolQuery
commite484d556d459205fb207d4a19a3d22dfe3d477db authorChris Allen <cma@bitemyapp.com> dateSun 27 Apr 2014, 06:00 (12 years ago) parentaef9e08
3 files changed+64−28
Database/Bloodhound/Types.hs+11−8 Database/Bloodhound/Types/Instances.hs+43−20 tests/tests.hs+10
Database/Bloodhound/Types.hs+11−8
@@ -12,6 +12,7 @@ module Database.Bloodhound.Types
1212 , unpackId
1313 , mkMatchQuery
1414 , mkMultiMatchQuery
15+ , mkBoolQuery
1516 , Version(..)
1617 , Status(..)
1718 , Existence(..)
@@ -98,6 +99,8 @@ module Database.Bloodhound.Types
9899 , MatchQueryType(..)
99100 , MultiMatchQueryType(..)
100101 , Tiebreaker(..)
102+ , MinimumMatch(..)
103+ , DisableCoord(..)
101104 ) where
102105
103106import Data.Aeson
@@ -222,9 +225,9 @@ newtype Lenient = Lenient Bool deriving (Eq, Show, Generic)
222225newtype Tiebreaker = Tiebreaker Double deriving (Eq, Show, Generic)
223226newtype Boost = Boost Double deriving (Eq, Show, Generic)
224227newtype BoostTerms = BoostTerms Double deriving (Eq, Show)
225−newtype MinimumMatch = MinimumMatch Int deriving (Eq, Show)
228+newtype MinimumMatch = MinimumMatch Int deriving (Eq, Show, Generic)
226229newtype MinimumMatchText = MinimumMatchText Text deriving (Eq, Show)
227−newtype DisableCoord = DisableCoord Bool deriving (Eq, Show)
230+newtype DisableCoord = DisableCoord Bool deriving (Eq, Show, Generic)
228231newtype IgnoreTermFrequency = IgnoreTermFrequency Bool deriving (Eq, Show)
229232newtype MinimumTermFrequency = MinimumTermFrequency Int deriving (Eq, Show)
230233newtype MaxQueryTerms = MaxQueryTerms Int deriving (Eq, Show)
@@ -290,9 +293,6 @@ data Query = TermQuery Term (Maybe Boost)
290293 | QueryRegexpQuery RegexpQuery
291294 deriving (Eq, Show)
292295
293−-- Possible flags are ALL, ANYSTRING, AUTOMATON, COMPLEMENT,
294−-- EMPTY, INTERSECTION, INTERVAL, or NONE. huh?
295−
296296data RegexpQuery =
297297 RegexpQuery { regexpQueryField :: FieldName
298298 , regexpQuery :: Regexp
@@ -515,6 +515,9 @@ data BoolQuery = BoolQuery { boolQueryMustMatch :: Maybe Query
515515 , boolQueryDisableCoord :: Maybe DisableCoord
516516 } deriving (Eq, Show)
517517
518+mkBoolQuery :: Maybe Query -> Maybe Query -> Maybe [Query] -> BoolQuery
519+mkBoolQuery must mustNot should = BoolQuery must mustNot should Nothing Nothing Nothing
520+
518521data BoostingQuery = BoostingQuery { positiveQuery :: Maybe Query
519522 , negativeQuery :: Maybe Query
520523 , positiveBoost :: Maybe Boost
@@ -673,12 +676,12 @@ data ShardResult =
673676 , shardsSuccessful :: Int
674677 , shardsFailed :: Int } deriving (Eq, Show, Generic)
675678
676−maybeJson ToJSON a => Text -> Maybe a -> [Data.Aeson.Types.Internal.Pair]
679+maybeJson :: ToJSON a => Text -> Maybe a -> [(Text, Value)]
677680maybeJson field (Just value) = [field .= toJSON value]
678681maybeJson _ _ = []
679682
680−maybeJsonF :: (ToJSON (f Value), ToJSON a, Functor f) =>
681− Text -> Maybe (f a) -> [Data.Aeson.Types.Internal.Pair]
683+-- maybeJsonF :: (ToJSON (f Value), ToJSON a, Functor f) =>
684+-- Text -> Maybe (f a) -> [(Text, Value)]
682685maybeJsonF field (Just value) = [field .= fmap toJSON value]
683686maybeJsonF _ _ = []
684687
Database/Bloodhound/Types/Instances.hs+43−20
@@ -120,25 +120,46 @@ instance ToJSON Filter where
120120
121121instance ToJSON GeoPoint where
122122 toJSON (GeoPoint (FieldName geoField) latLon) =
123− object [geoField .= toJSON latLon]
123+ object [ geoField .= toJSON latLon ]
124124
125125
126126instance ToJSON Query where
127127 toJSON (TermQuery (Term termField termValue) boost) =
128− object ["term" .=
129− object [termField .= object merged]]
128+ object [ "term" .=
129+ object [termField .= object merged]]
130130 where
131− base = ["value" .= termValue]
131+ base = [ "value" .= termValue ]
132132 boosted = case boost of
133− (Just boostValue) -> ["boost" .= boostValue]
133+ (Just boostValue) -> [ "boost" .= boostValue ]
134134 Nothing -> []
135135 merged = mappend base boosted
136136
137137 toJSON (QueryMatchQuery matchQuery) =
138− object ["match" .= toJSON matchQuery]
138+ object [ "match" .= toJSON matchQuery ]
139139
140140 toJSON (QueryMultiMatchQuery multiMatchQuery) =
141− object ["multi_match" .= toJSON multiMatchQuery]
141+ object [ "multi_match" .= toJSON multiMatchQuery ]
142+
143+ toJSON (QueryBoolQuery boolQuery) =
144+ object [ "bool" .= toJSON boolQuery ]
145+
146+
147+mField :: (ToJSON a, Functor f) => T.Text -> f a -> f (T.Text, Value)
148+mField field = fmap ((field .=) . toJSON)
149+
150+
151+instance ToJSON BoolQuery where
152+ toJSON (BoolQuery mustM notM shouldM min boost disableCoord) =
153+ object filtered
154+ where f field = fmap ((field .=) . toJSON)
155+ filtered = catMaybes
156+ [ mField "must" mustM
157+ , mField "must_not" notM
158+ , mField "should" shouldM
159+ , mField "minimum_should_match" min
160+ , mField "boost" boost
161+ , mField "disable_coord" disableCoord ]
162+
142163
143164instance ToJSON MatchQuery where
144165 toJSON (MatchQuery (FieldName fieldName)
@@ -150,12 +171,12 @@ instance ToJSON MatchQuery where
150171 , "operator" .= toJSON booleanOperator
151172 , "zero_terms_query" .= toJSON zeroTermsQuery]
152173 ++ maybeAdd
153− f field = fmap ((field .=) . toJSON)
154− maybeAdd = catMaybes [ f "cutoff_frequency" cutoffFrequency
155− , f "type" matchQueryType
156− , f "analyzer" analyzer
157− , f "max_expansions" maxExpansions
158− , f "lenient" lenient ]
174+ maybeAdd = catMaybes [ mField "cutoff_frequency" cutoffFrequency
175+ , mField "type" matchQueryType
176+ , mField "analyzer" analyzer
177+ , mField "max_expansions" maxExpansions
178+ , mField "lenient" lenient ]
179+
159180
160181instance ToJSON MultiMatchQuery where
161182 toJSON (MultiMatchQuery fields (QueryString query) boolOp
@@ -165,15 +186,15 @@ instance ToJSON MultiMatchQuery where
165186 , "query" .= query
166187 , "operator" .= toJSON boolOp
167188 , "zero_terms_query" .= toJSON ztQ ]
168− f field = fmap ((field .=) . toJSON)
169− maybeAdd = catMaybes [ f "tiebreaker" tb
170− , f "type" mmqt
171− , f "cutoff_frequency" cf
172− , f "analyzer" analyzer
173− , f "max_expansions" maxEx
174− , f "lenient" lenient ]
189+ maybeAdd = catMaybes [ mField "tiebreaker" tb
190+ , mField "type" mmqt
191+ , mField "cutoff_frequency" cf
192+ , mField "analyzer" analyzer
193+ , mField "max_expansions" maxEx
194+ , mField "lenient" lenient ]
175195 conjoined = baseQuery ++ maybeAdd
176196
197+
177198instance ToJSON MultiMatchQueryType where
178199 toJSON MultiMatchBestFields = "best_fields"
179200 toJSON MultiMatchMostFields = "most_fields"
@@ -205,6 +226,8 @@ instance ToJSON Lenient
205226instance ToJSON Boost
206227instance ToJSON Version
207228instance ToJSON Tiebreaker
229+instance ToJSON MinimumMatch
230+instance ToJSON DisableCoord
208231instance FromJSON Version
209232instance FromJSON IndexName
210233instance FromJSON MappingName
tests/tests.hs+10
@@ -172,6 +172,16 @@ main = hspec $ do
172172 myTweet <- searchTweet search
173173 myTweet `shouldBe` Right exampleTweet
174174
175+ it "returns document for bool query" $ do
176+ _ <- insertData
177+ let innerQuery = QueryMatchQuery $
178+ mkMatchQuery (FieldName "user") (QueryString "bitemyapp")
179+ let query = QueryBoolQuery $
180+ mkBoolQuery (Just innerQuery) Nothing Nothing
181+ let search = mkSearch (Just query) Nothing
182+ myTweet <- searchTweet search
183+ myTweet `shouldBe` Right exampleTweet
184+
175185 describe "sorting" $ do
176186 it "returns documents in the right order" $ do
177187 _ <- insertData