Boosting Query

This commit is contained in:
Chris Allen 2014-04-27 07:22:12 -05:00
parent e484d556d4
commit 6acb114e8e
3 changed files with 23 additions and 6 deletions

View File

@ -518,10 +518,9 @@ data BoolQuery = BoolQuery { boolQueryMustMatch :: Maybe Query
mkBoolQuery :: Maybe Query -> Maybe Query -> Maybe [Query] -> BoolQuery
mkBoolQuery must mustNot should = BoolQuery must mustNot should Nothing Nothing Nothing
data BoostingQuery = BoostingQuery { positiveQuery :: Maybe Query
, negativeQuery :: Maybe Query
, positiveBoost :: Maybe Boost
, negativeBoost :: Maybe Boost } deriving (Eq, Show)
data BoostingQuery = BoostingQuery { positiveQuery :: Query
, negativeQuery :: Query
, negativeBoost :: Boost } deriving (Eq, Show)
data CommonTermsQuery =
CommonTermsQuery { commonQuery :: QueryString

View File

@ -143,16 +143,25 @@ instance ToJSON Query where
toJSON (QueryBoolQuery boolQuery) =
object [ "bool" .= toJSON boolQuery ]
toJSON (QueryBoostingQuery boostingQuery) =
object ["boosting" .= toJSON boostingQuery]
mField :: (ToJSON a, Functor f) => T.Text -> f a -> f (T.Text, Value)
mField field = fmap ((field .=) . toJSON)
instance ToJSON BoostingQuery where
toJSON (BoostingQuery positiveQuery negativeQuery negativeBoost) =
object [ "positive" .= toJSON positiveQuery
, "negative" .= toJSON negativeQuery
, "negative_boost" .= toJSON negativeBoost ]
instance ToJSON BoolQuery where
toJSON (BoolQuery mustM notM shouldM min boost disableCoord) =
object filtered
where f field = fmap ((field .=) . toJSON)
filtered = catMaybes
where filtered = catMaybes
[ mField "must" mustM
, mField "must_not" notM
, mField "should" shouldM

View File

@ -182,6 +182,15 @@ main = hspec $ do
myTweet <- searchTweet search
myTweet `shouldBe` Right exampleTweet
it "returns document for boosting query" $ do
_ <- insertData
let posQuery = QueryMatchQuery $ mkMatchQuery (FieldName "user") (QueryString "bitemyapp")
let negQuery = QueryMatchQuery $ mkMatchQuery (FieldName "user") (QueryString "notmyapp")
let query = QueryBoostingQuery $ BoostingQuery posQuery negQuery (Boost 0.2)
let search = mkSearch (Just query) Nothing
myTweet <- searchTweet search
myTweet `shouldBe` Right exampleTweet
describe "sorting" $ do
it "returns documents in the right order" $ do
_ <- insertData