git.iko.soy/typeable/bloodhound
on master help sign in
Fix bugs with constant score filter/queries
commit27e244e9d12ff45cc5f17ccd220ff751533cc09c authorMichael Xavier <michael@michaelxavier.net> dateWed 28 Oct 2015, 19:47 (11 years ago) parent1bffa02
3 files changed+48−27
src/Database/Bloodhound/Client.hs+7−5 src/Database/Bloodhound/Types.hs+11−12 tests/tests.hs+30−10
src/Database/Bloodhound/Client.hs+7−5
@@ -57,6 +57,7 @@ module Database.Bloodhound.Client
5757 , isVersionConflict
5858 , isSuccess
5959 , isCreated
60+ , parseEsResponse
6061 )
6162 where
6263
@@ -287,15 +288,16 @@ parseEsResponse :: (MonadBH m, MonadThrow m, FromJSON a) => Reply
287288parseEsResponse reply
288289 | respIsTwoHunna reply = case eitherDecode body of
289290 Right a -> return (Right a)
290− Left _ -> case eitherDecode body of
291− Right e -> return (Left e)
292− -- this case should not be possible
293− Left _ -> explode
294− | otherwise = explode
291+ Left _ -> tryParseError
292+ | otherwise = tryParseError
295293 where body = responseBody reply
296294 stat = responseStatus reply
297295 hdrs = responseHeaders reply
298296 cookies = responseCookieJar reply
297+ tryParseError = case eitherDecode body of
298+ Right e -> return (Left e)
299+ -- this case should not be possible
300+ Left _ -> explode
299301 explode = throwM (StatusCodeException stat hdrs cookies)
300302
301303-- | 'indexExists' enables you to check if an index exists. Returns 'Bool'
src/Database/Bloodhound/Types.hs+11−12
@@ -1825,12 +1825,12 @@ instance ToJSON Query where
18251825 object [ "common" .= commonTermsQuery ]
18261826
18271827 toJSON (ConstantScoreFilter csFilter boost) =
1828− object ["filter" .= object ["constant_score" .= csFilter]
1829− , "boost" .= boost]
1828+ object ["constant_score" .= object ["filter" .= csFilter
1829+ , "boost" .= boost]]
18301830
18311831 toJSON (ConstantScoreQuery query boost) =
1832− object ["query" .= object ["constant_score" .= query]
1833− , "boost" .= boost]
1832+ object ["constant_score" .= object ["query" .= query
1833+ , "boost" .= boost]]
18341834
18351835 toJSON (QueryDisMaxQuery disMaxQuery) =
18361836 object [ "dis_max" .= disMaxQuery ]
@@ -1891,8 +1891,8 @@ instance FromJSON Query where
18911891 <|> queryBoolQuery `taggedWith` "bool"
18921892 <|> queryBoostingQuery `taggedWith` "boosting"
18931893 <|> queryCommonTermsQuery `taggedWith` "common"
1894− <|> constantScoreFilter o
1895− <|> constantScoreQuery o
1894+ <|> constantScoreFilter `taggedWith` "constant_score"
1895+ <|> constantScoreQuery `taggedWith` "constant_score"
18961896 <|> queryDisMaxQuery `taggedWith` "dis_max"
18971897 <|> queryFilteredQuery `taggedWith` "filtered"
18981898 <|> queryFuzzyLikeThisQuery `taggedWith` "fuzzy_like_this"
@@ -1926,15 +1926,14 @@ instance FromJSON Query where
19261926 queryBoolQuery = pure . QueryBoolQuery
19271927 queryBoostingQuery = pure . QueryBoostingQuery
19281928 queryCommonTermsQuery = pure . QueryCommonTermsQuery
1929− --FIXME: these are ambiguous
19301929 constantScoreFilter o = case HM.lookup "filter" o of
1931− Just (Object o') -> ConstantScoreFilter <$> o' .: "constant_score"
1932− <*> o .: "boost"
1930+ Just x -> ConstantScoreFilter <$> parseJSON x
1931+ <*> o .: "boost"
19331932 _ -> fail "Does not appear to be a ConstantScoreFilter"
19341933 constantScoreQuery o = case HM.lookup "query" o of
1935− Just (Object o') -> ConstantScoreQuery <$> o' .: "constant_score"
1936− <*> o .: "boost"
1937− _ -> fail "Does not appear to be a ConstantScoreFilter"
1934+ Just x -> ConstantScoreQuery <$> parseJSON x
1935+ <*> o .: "boost"
1936+ _ -> fail "Does not appear to be a ConstantScoreQuery"
19381937 queryDisMaxQuery = pure . QueryDisMaxQuery
19391938 queryFilteredQuery = pure . QueryFilteredQuery
19401939 queryFuzzyLikeThisQuery = pure . QueryFuzzyLikeThisQuery
tests/tests.hs+30−10
@@ -211,15 +211,15 @@ insertWithSpaceInId = do
211211 _ <- refreshIndex testIndex
212212 return ()
213213
214−searchTweet :: Search -> BH IO (Either String Tweet)
214+searchTweet :: Search -> BH IO (Either EsError Tweet)
215215searchTweet search = do
216216 result <- searchTweets search
217− let myTweet :: Either String Tweet
217+ let myTweet :: Either EsError Tweet
218218 myTweet = grabFirst result
219219 return myTweet
220220
221−searchTweets :: Search -> BH IO (Either String (SearchResult Tweet))
222−searchTweets search = eitherDecode . responseBody <$> searchByIndex testIndex search
221+searchTweets :: Search -> BH IO (Either EsError (SearchResult Tweet))
222+searchTweets search = parseEsResponse =<< searchByIndex testIndex search
223223
224224searchExpectNoResults :: Search -> BH IO ()
225225searchExpectNoResults search = do
@@ -253,19 +253,19 @@ searchTermsAggHint hints = do
253253 forM_ hints $ searchExpectAggs . search
254254 forM_ hints (\x -> searchValidBucketAgg (search x) "users" toTerms)
255255
256−searchTweetHighlight :: Search -> BH IO (Either String (Maybe HitHighlight))
256+searchTweetHighlight :: Search -> BH IO (Either EsError (Maybe HitHighlight))
257257searchTweetHighlight search = do
258258 result <- searchTweets search
259259 let myHighlight = fmap (hitHighlight . head . hits . searchHits) result
260260 return myHighlight
261261
262−searchExpectSource :: Source -> Either String Value -> BH IO ()
262+searchExpectSource :: Source -> Either EsError Value -> BH IO ()
263263searchExpectSource src expected = do
264264 _ <- insertData
265265 let query = QueryMatchQuery $ mkMatchQuery (FieldName "_all") (QueryString "haskell")
266266 let search = (mkSearch (Just query) Nothing) { source = Just src }
267267 reply <- searchAll search
268− let result = eitherDecode (responseBody reply) :: Either String (SearchResult Value)
268+ result <- parseEsResponse reply-- :: Either EsError (SearchResult Value)
269269 let value = grabFirst result
270270 liftIO $
271271 value `shouldBe` expected
@@ -307,11 +307,11 @@ reduceSize f = sized $ \n -> resize (n `div` 2) f
307307getSource :: EsResult a -> Maybe a
308308getSource = fmap _source . foundResult
309309
310−grabFirst :: Either String (SearchResult a) -> Either String a
310+grabFirst :: Either EsError (SearchResult a) -> Either EsError a
311311grabFirst r =
312312 case fmap (hitSource . head . hits . searchHits) r of
313313 (Left e) -> Left e
314− (Right Nothing) -> Left "Source was missing"
314+ (Right Nothing) -> Left (EsError 500 "Source was missing")
315315 (Right (Just x)) -> Right x
316316
317317-------------------------------------------------------------------------------
@@ -611,6 +611,26 @@ main = hspec $ do
611611 liftIO $
612612 myTweet `shouldBe` Right exampleTweet
613613
614+ it "handles constant score queries" $ withTestEnv $ do
615+ _ <- insertData
616+ let query = TermsQuery "user" ("bitemyapp" :| [])
617+ let cfQuery = ConstantScoreQuery query (Boost 1.0)
618+ let filter = IdentityFilter
619+ let search = mkSearch (Just cfQuery) (Just filter)
620+ myTweet <- searchTweet search
621+ liftIO $
622+ myTweet `shouldBe` Right exampleTweet
623+ it "handles constant score filters" $ withTestEnv $ do
624+ _ <- insertData
625+ let query = TermsQuery "user" ("bitemyapp" :| [])
626+ let cfFilter = ConstantScoreFilter IdentityFilter (Boost 1.0)
627+ let boolQuery = mkBoolQuery [query, cfFilter] [] []
628+ let search = mkSearch (Just (QueryBoolQuery boolQuery)) Nothing
629+ myTweet <- searchTweet search
630+ liftIO $
631+ myTweet `shouldBe` Right exampleTweet
632+
633+
614634 it "returns document for terms query and identity filter" $ withTestEnv $ do
615635 _ <- insertData
616636 let query = TermsQuery "user" ("bitemyapp" :| [])
@@ -946,7 +966,7 @@ main = hspec $ do
946966 it "doesn't include source when sources are disabled" $ withTestEnv $ do
947967 searchExpectSource
948968 NoSource
949− (Left "Source was missing")
969+ (Left (EsError 500 "Source was missing"))
950970
951971 it "includes a source" $ withTestEnv $ do
952972 searchExpectSource