git.iko.soy/typeable/bloodhound
on master help sign in
Boosting Query
6acb114Chris Allen
Database/Bloodhound/Types.hs+3−4
@@ -518,10 +518,9 @@ data BoolQuery = BoolQuery { boolQueryMustMatch :: Maybe Query
518mkBoolQuery :: Maybe Query -> Maybe Query -> Maybe [Query] -> BoolQuery
518mkBoolQuery :: Maybe Query -> Maybe Query -> Maybe [Query] -> BoolQuery
519mkBoolQuery must mustNot should = BoolQuery must mustNot should Nothing Nothing Nothing
519mkBoolQuery must mustNot should = BoolQuery must mustNot should Nothing Nothing Nothing
520
520
521−data BoostingQuery = BoostingQuery { positiveQuery :: Maybe Query
521+data BoostingQuery = BoostingQuery { positiveQuery :: Query
522− , negativeQuery :: Maybe Query
522+ , negativeQuery :: Query
523− , positiveBoost :: Maybe Boost
524− , negativeBoost :: Maybe Boost } deriving (Eq, Show)
523+ , negativeBoost :: Boost } deriving (Eq, Show)
525
524
526data CommonTermsQuery =
525data CommonTermsQuery =
527 CommonTermsQuery { commonQuery :: QueryString
526 CommonTermsQuery { commonQuery :: QueryString
Database/Bloodhound/Types/Instances.hs+11−2
@@ -143,16 +143,25 @@ instance ToJSON Query where
143 toJSON (QueryBoolQuery boolQuery) =
143 toJSON (QueryBoolQuery boolQuery) =
144 object [ "bool" .= toJSON boolQuery ]
144 object [ "bool" .= toJSON boolQuery ]
145
145
146+ toJSON (QueryBoostingQuery boostingQuery) =
147+ object ["boosting" .= toJSON boostingQuery]
148+
146
149
147mField :: (ToJSON a, Functor f) => T.Text -> f a -> f (T.Text, Value)
150mField :: (ToJSON a, Functor f) => T.Text -> f a -> f (T.Text, Value)
148mField field = fmap ((field .=) . toJSON)
151mField field = fmap ((field .=) . toJSON)
149
152
150
153
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+
151instance ToJSON BoolQuery where
161instance ToJSON BoolQuery where
152 toJSON (BoolQuery mustM notM shouldM min boost disableCoord) =
162 toJSON (BoolQuery mustM notM shouldM min boost disableCoord) =
153 object filtered
163 object filtered
154− where f field = fmap ((field .=) . toJSON)
155− filtered = catMaybes
164+ where filtered = catMaybes
156 [ mField "must" mustM
165 [ mField "must" mustM
157 , mField "must_not" notM
166 , mField "must_not" notM
158 , mField "should" shouldM
167 , mField "should" shouldM
tests/tests.hs+9
@@ -182,6 +182,15 @@ main = hspec $ do
182 myTweet <- searchTweet search
182 myTweet <- searchTweet search
183 myTweet `shouldBe` Right exampleTweet
183 myTweet `shouldBe` Right exampleTweet
184
184
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+
185 describe "sorting" $ do
194 describe "sorting" $ do
186 it "returns documents in the right order" $ do
195 it "returns documents in the right order" $ do
187 _ <- insertData
196 _ <- insertData