git.iko.soy/typeable/bloodhound
on master help sign in
lower and higher level scan&scroll functions; added scrollId to
3fa24e2Josh Berman
bloodhound.cabal+1−1
@@ -1,5 +1,5 @@
1name: bloodhound
1name: bloodhound
2−version: 0.6.0.1
2+version: 0.6.0.2
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+37−2
@@ -1,5 +1,5 @@
1−{-# LANGUAGE OverloadedStrings #-}
1+{-# LANGUAGE OverloadedStrings #-}
2−{-# LANGUAGE RecordWildCards #-}
2+{-# LANGUAGE RecordWildCards #-}
3
3
4-------------------------------------------------------------------------------
4-------------------------------------------------------------------------------
5-- |
5-- |
@@ -36,6 +36,7 @@ module Database.Bloodhound.Client
36 , searchAll
36 , searchAll
37 , searchByIndex
37 , searchByIndex
38 , searchByType
38 , searchByType
39+ , scanSearch
39 , refreshIndex
40 , refreshIndex
40 , mkSearch
41 , mkSearch
41 , mkAggregateSearch
42 , mkAggregateSearch
@@ -482,6 +483,40 @@ searchByType (IndexName indexName)
482 (MappingName mappingName) = bindM2 dispatchSearch url . return
483 (MappingName mappingName) = bindM2 dispatchSearch url . return
483 where url = joinPath [indexName, mappingName, "_search"]
484 where url = joinPath [indexName, mappingName, "_search"]
484
485
486+scanSearch' :: MonadBH m => Search -> m (Maybe ScrollId)
487+scanSearch' search = do
488+ let url = joinPath ["_search"]
489+ search' = search { searchType = SearchTypeScan }
490+ resp' <- bindM2 dispatchSearch url (return search')
491+ let msr = decode' $ responseBody resp' :: Maybe (SearchResult ())
492+ msid = maybe Nothing scrollId msr
493+ return msid
494+
495+scroll' :: (FromJSON a, MonadBH m) => Maybe ScrollId -> m ([Hit a], Maybe ScrollId)
496+scroll' Nothing = return ([], Nothing)
497+scroll' (Just sid) = do
498+ url <- joinPath ["_search/scroll?scroll=1m"]
499+ resp' <- post url (Just . L.fromStrict $ T.encodeUtf8 sid)
500+ let msr = decode' $ responseBody resp' :: FromJSON a => Maybe (SearchResult a)
501+ resp = case msr of
502+ Just sr -> (hits $ searchHits sr, scrollId sr)
503+ _ -> ([], Nothing)
504+ return resp
505+
506+simpleAccumilator :: (FromJSON a, MonadBH m) => [Hit a] -> ([Hit a], Maybe ScrollId) -> m ([Hit a], Maybe ScrollId)
507+simpleAccumilator oldHits (newHits, Nothing) = return (oldHits ++ newHits, Nothing)
508+simpleAccumilator oldHits ([], _) = return (oldHits, Nothing)
509+simpleAccumilator oldHits (newHits, msid) = do
510+ (newHits', msid') <- scroll' msid
511+ simpleAccumilator (oldHits ++ newHits) (newHits', msid')
512+
513+scanSearch :: (FromJSON a, MonadBH m) => Search -> m [Hit a]
514+scanSearch search = do
515+ msid <- scanSearch' search
516+ (hits, msid') <- scroll' msid
517+ (totalHits, _) <- simpleAccumilator [] (hits, msid')
518+ return totalHits
519+
485-- | 'mkSearch' is a helper function for defaulting additional fields of a 'Search'
520-- | 'mkSearch' is a helper function for defaulting additional fields of a 'Search'
486-- to Nothing in case you only care about your 'Query' and 'Filter'. Use record update
521-- to Nothing in case you only care about your 'Query' and 'Filter'. Use record update
487-- syntax if you want to add things like aggregations or highlights while still using
522-- syntax if you want to add things like aggregations or highlights while still using
src/Database/Bloodhound/Types.hs+7−2
@@ -60,6 +60,7 @@ module Database.Bloodhound.Types
60 , Search(..)
60 , Search(..)
61 , SearchType(..)
61 , SearchType(..)
62 , SearchResult(..)
62 , SearchResult(..)
63+ , ScrollId
63 , SearchHits(..)
64 , SearchHits(..)
64 , TrackSortScores
65 , TrackSortScores
65 , From(..)
66 , From(..)
@@ -1126,7 +1127,10 @@ data SearchResult a =
1126 , timedOut :: Bool
1127 , timedOut :: Bool
1127 , shards :: ShardResult
1128 , shards :: ShardResult
1128 , searchHits :: SearchHits a
1129 , searchHits :: SearchHits a
1129− , aggregations :: Maybe AggregationResults } deriving (Eq, Show)
1130+ , aggregations :: Maybe AggregationResults
1131+ , scrollId :: Maybe ScrollId } deriving (Eq, Show)
1132+
1133+type ScrollId = Text -- Fixme: Newtype
1130
1134
1131type Score = Maybe Double
1135type Score = Maybe Double
1132
1136
@@ -2157,7 +2161,8 @@ instance (FromJSON a) => FromJSON (SearchResult a) where
2157 v .: "timed_out" <*>
2161 v .: "timed_out" <*>
2158 v .: "_shards" <*>
2162 v .: "_shards" <*>
2159 v .: "hits" <*>
2163 v .: "hits" <*>
2160− v .:? "aggregations"
2164+ v .:? "aggregations" <*>
2165+ v .:? "_scroll_id"
2161 parseJSON _ = empty
2166 parseJSON _ = empty
2162
2167
2163instance (FromJSON a) => FromJSON (SearchHits a) where
2168instance (FromJSON a) => FromJSON (SearchHits a) where