git.iko.soy/typeable/bloodhound
on master help sign in
Merge pull request #63 from bermanjosh/scanType
2034616Chris Allen
bloodhound.cabal+1−1
@@ -1,5 +1,5 @@
1name: bloodhound
1name: bloodhound
2−version: 0.7.1.0
2+version: 0.7.2.0
3synopsis: ElasticSearch client library for Haskell
3synopsis: ElasticSearch client library for Haskell
4description: ElasticSearch made awesome for Haskell hackers
4description: ElasticSearch made awesome for Haskell hackers
5homepage: https://github.com/bitemyapp/bloodhound
5homepage: https://github.com/bitemyapp/bloodhound
src/Database/Bloodhound/Client.hs+17−15
@@ -173,16 +173,16 @@ joinPath ps = do
173 return $ joinPath' (s:ps)
173 return $ joinPath' (s:ps)
174
174
175appendSearchTypeParam :: Text -> SearchType -> Text
175appendSearchTypeParam :: Text -> SearchType -> Text
176−appendSearchTypeParam originalUrl st = addQuery [(keyEq, Just stParams)] originalUrl
176+appendSearchTypeParam originalUrl st = addQuery params originalUrl
177− where keyEq = "search_type="
177+ where stText = "search_type"
178− stParams
178+ params
179− | st == SearchTypeDfsQueryThenFetch = "dfs_query_then_fetch"
179+ | st == SearchTypeDfsQueryThenFetch = [(stText, Just "dfs_query_then_fetch")]
180− | st == SearchTypeCount = "count"
180+ | st == SearchTypeCount = [(stText, Just "count")]
181− | st == SearchTypeScan = "scan&scroll=1m"
181+ | st == SearchTypeScan = [(stText, Just "scan"), ("scroll", Just "1m")]
182− | st == SearchTypeQueryAndFetch = "query_and_fetch"
182+ | st == SearchTypeQueryAndFetch = [(stText, Just "query_and_fetch")]
183− | st == SearchTypeDfsQueryAndFetch = "dfs_query_and_fetch"
183+ | st == SearchTypeDfsQueryAndFetch = [(stText, Just "dfs_query_and_fetch")]
184 -- used to catch 'SearchTypeQueryThenFetch', which is also the default
184 -- used to catch 'SearchTypeQueryThenFetch', which is also the default
185− | otherwise = "query_then_fetch"
185+ | otherwise = [(stText, Just "query_then_fetch")]
186
186
187-- | Severely dumbed down query renderer. Assumes your data doesn't
187-- | Severely dumbed down query renderer. Assumes your data doesn't
188-- need any encoding
188-- need any encoding
@@ -543,9 +543,9 @@ searchByType (IndexName indexName)
543 (MappingName mappingName) = bindM2 dispatchSearch url . return
543 (MappingName mappingName) = bindM2 dispatchSearch url . return
544 where url = joinPath [indexName, mappingName, "_search"]
544 where url = joinPath [indexName, mappingName, "_search"]
545
545
546−scanSearch' :: MonadBH m => Search -> m (Maybe ScrollId)
546+scanSearch' :: MonadBH m => IndexName -> MappingName -> Search -> m (Maybe ScrollId)
547−scanSearch' search = do
547+scanSearch' (IndexName indexName) (MappingName mappingName) search = do
548− let url = joinPath ["_search"]
548+ let url = joinPath [indexName, mappingName, "_search"]
549 search' = search { searchType = SearchTypeScan }
549 search' = search { searchType = SearchTypeScan }
550 resp' <- bindM2 dispatchSearch url (return search')
550 resp' <- bindM2 dispatchSearch url (return search')
551 let msr = decode' $ responseBody resp' :: Maybe (SearchResult ())
551 let msr = decode' $ responseBody resp' :: Maybe (SearchResult ())
@@ -570,9 +570,11 @@ simpleAccumilator oldHits (newHits, msid) = do
570 (newHits', msid') <- scroll' msid
570 (newHits', msid') <- scroll' msid
571 simpleAccumilator (oldHits ++ newHits) (newHits', msid')
571 simpleAccumilator (oldHits ++ newHits) (newHits', msid')
572
572
573+-- | 'scanSearch' uses the 'scan&scroll' API of elastic,
574+-- for a given 'IndexName' and 'MappingName',
573−scanSearch :: (FromJSON a, MonadBH m) => Search -> m [Hit a]
575+scanSearch :: (FromJSON a, MonadBH m) => IndexName -> MappingName -> Search -> m [Hit a]
574−scanSearch search = do
576+scanSearch indexName mappingName search = do
575− msid <- scanSearch' search
577+ msid <- scanSearch' indexName mappingName search
576 (hits, msid') <- scroll' msid
578 (hits, msid') <- scroll' msid
577 (totalHits, _) <- simpleAccumilator [] (hits, msid')
579 (totalHits, _) <- simpleAccumilator [] (hits, msid')
578 return totalHits
580 return totalHits
tests/tests.hs+13
@@ -762,3 +762,16 @@ main = hspec $ do
762 enumFrom (pred maxBound :: DocVersion) `shouldBe` [pred maxBound, maxBound]
762 enumFrom (pred maxBound :: DocVersion) `shouldBe` [pred maxBound, maxBound]
763 enumFrom (pred maxBound :: DocVersion) `shouldBe` [pred maxBound, maxBound]
763 enumFrom (pred maxBound :: DocVersion) `shouldBe` [pred maxBound, maxBound]
764 enumFromThen minBound (pred maxBound :: DocVersion) `shouldBe` [minBound, pred maxBound]
764 enumFromThen minBound (pred maxBound :: DocVersion) `shouldBe` [minBound, pred maxBound]
765+
766+ describe "scan&scroll API" $ do
767+ it "returns documents using the scan&scroll API" $ withTestEnv $ do
768+ _ <- insertData
769+ _ <- insertOther
770+ let search = (mkSearch (Just $ MatchAllQuery Nothing) Nothing) { size = (Size 1) }
771+ regular_search <- searchTweet search
772+ scan_search' <- scanSearch testIndex testMapping search :: BH IO [Hit Tweet]
773+ let scan_search = map hitSource scan_search'
774+ liftIO $
775+ regular_search `shouldBe` Right exampleTweet -- Check that the size restrtiction is being honored
776+ liftIO $
777+ scan_search `shouldMatchList` [exampleTweet, otherTweet]