git.iko.soy/typeable/bloodhound
on master help sign in
Boosting Query
commit6acb114e8ee4dcce7926c005a197ba02bba8bf37 authorChris Allen <cma@bitemyapp.com> dateSun 27 Apr 2014, 07:22 (12 years ago) parente484d55
3 files changed+23−6
Database/Bloodhound/Types.hs+3−4 Database/Bloodhound/Types/Instances.hs+11−2 tests/tests.hs+9
Database/Bloodhound/Types.hs+3−4
@@ -518,10 +518,9 @@ data BoolQuery = BoolQuery { boolQueryMustMatch :: Maybe Query
518518mkBoolQuery :: Maybe Query -> Maybe Query -> Maybe [Query] -> BoolQuery
519519mkBoolQuery must mustNot should = BoolQuery must mustNot should Nothing Nothing Nothing
520520
521−data BoostingQuery = BoostingQuery { positiveQuery :: Maybe Query
522− , negativeQuery :: Maybe Query
523− , positiveBoost :: Maybe Boost
524− , negativeBoost :: Maybe Boost } deriving (Eq, Show)
521+data BoostingQuery = BoostingQuery { positiveQuery :: Query
522+ , negativeQuery :: Query
523+ , negativeBoost :: Boost } deriving (Eq, Show)
525524
526525data CommonTermsQuery =
527526 CommonTermsQuery { commonQuery :: QueryString
Database/Bloodhound/Types/Instances.hs+11−2
@@ -143,16 +143,25 @@ instance ToJSON Query where
143143 toJSON (QueryBoolQuery boolQuery) =
144144 object [ "bool" .= toJSON boolQuery ]
145145
146+ toJSON (QueryBoostingQuery boostingQuery) =
147+ object ["boosting" .= toJSON boostingQuery]
148+
146149
147150mField :: (ToJSON a, Functor f) => T.Text -> f a -> f (T.Text, Value)
148151mField field = fmap ((field .=) . toJSON)
149152
150153
154+instance ToJSON BoostingQuery where
155+ toJSON (BoostingQuery positiveQuery negativeQuery negativeBoost) =
156+ object [ "positive" .= toJSON positiveQuery
157+ , "negative" .= toJSON negativeQuery
158+ , "negative_boost" .= toJSON negativeBoost ]
159+
160+
151161instance ToJSON BoolQuery where
152162 toJSON (BoolQuery mustM notM shouldM min boost disableCoord) =
153163 object filtered
154− where f field = fmap ((field .=) . toJSON)
155− filtered = catMaybes
164+ where filtered = catMaybes
156165 [ mField "must" mustM
157166 , mField "must_not" notM
158167 , mField "should" shouldM
tests/tests.hs+9
@@ -182,6 +182,15 @@ main = hspec $ do
182182 myTweet <- searchTweet search
183183 myTweet `shouldBe` Right exampleTweet
184184
185+ it "returns document for boosting query" $ do
186+ _ <- insertData
187+ let posQuery = QueryMatchQuery $ mkMatchQuery (FieldName "user") (QueryString "bitemyapp")
188+ let negQuery = QueryMatchQuery $ mkMatchQuery (FieldName "user") (QueryString "notmyapp")
189+ let query = QueryBoostingQuery $ BoostingQuery posQuery negQuery (Boost 0.2)
190+ let search = mkSearch (Just query) Nothing
191+ myTweet <- searchTweet search
192+ myTweet `shouldBe` Right exampleTweet
193+
185194 describe "sorting" $ do
186195 it "returns documents in the right order" $ do
187196 _ <- insertData