git.iko.soy/typeable/bloodhound
on master help sign in
Newtype over From/Size
commit354423cba669edc1a2971af2c447d9e16ece6121 authorMichael Xavier <michael@michaelxavier.net> dateMon 11 May 2015, 21:55 (11 years ago) parent5864d1b
4 files changed+16−16
README.md+2−2 src/Database/Bloodhound/Client.hs+9−9 src/Database/Bloodhound/Types.hs+4−4 tests/tests.hs+1−1
README.md+2−2
@@ -383,7 +383,7 @@ Search
383383
384384-- exported by the Client module, just defaults some stuff.
385385-- mkSearch :: Maybe Query -> Maybe Filter -> Search
386−-- mkSearch query filter = Search query filter Nothing False 0 10
386+-- mkSearch query filter = Search query filter Nothing False (From 0) (Size 10)
387387
388388let query = TermQuery (Term "user" "bitemyapp") Nothing
389389
@@ -488,7 +488,7 @@ let sortSpec = DefaultSortSpec $ mkSort (FieldName "age") Ascending
488488-- -> From -> Size
489489
490490-- just add more sortspecs to the list if you want tie-breakers.
491−let search = Search Nothing (Just IdentityFilter) (Just [sortSpec]) False 0 10
491+let search = Search Nothing (Just IdentityFilter) (Just [sortSpec]) False (From 0) (Size 10)
492492
493493```
494494
src/Database/Bloodhound/Client.hs+9−9
@@ -469,9 +469,9 @@ searchByType (IndexName indexName)
469469--
470470-- >>> let query = TermQuery (Term "user" "bitemyapp") Nothing
471471-- >>> mkSearch (Just query) Nothing
472−-- Search {queryBody = Just (TermQuery (Term {termField = "user", termValue = "bitemyapp"}) Nothing), filterBody = Nothing, sortBody = Nothing, aggBody = Nothing, highlight = Nothing, trackSortScores = False, from = 0, size = 10}
472+-- Search {queryBody = Just (TermQuery (Term {termField = "user", termValue = "bitemyapp"}) Nothing), filterBody = Nothing, sortBody = Nothing, aggBody = Nothing, highlight = Nothing, trackSortScores = False, from = From 0, size = Size 10}
473473mkSearch :: Maybe Query -> Maybe Filter -> Search
474−mkSearch query filter = Search query filter Nothing Nothing Nothing False 0 10
474+mkSearch query filter = Search query filter Nothing Nothing Nothing False (From 0) (Size 10)
475475
476476-- | 'mkAggregateSearch' is a helper function that defaults everything in a 'Search' except for
477477-- the 'Query' and the 'Aggregation'.
@@ -481,7 +481,7 @@ mkSearch query filter = Search query filter Nothing Nothing Nothing False 0 10
481481-- TermsAgg (TermsAggregation {term = Left "user", termInclude = Nothing, termExclude = Nothing, termOrder = Nothing, termMinDocCount = Nothing, termSize = Nothing, termShardSize = Nothing, termCollectMode = Just BreadthFirst, termExecutionHint = Nothing, termAggs = Nothing})
482482-- >>> let myAggregation = mkAggregateSearch Nothing $ mkAggregations "users" terms
483483mkAggregateSearch :: Maybe Query -> Aggregations -> Search
484−mkAggregateSearch query mkSearchAggs = Search query Nothing Nothing (Just mkSearchAggs) Nothing False 0 0
484+mkAggregateSearch query mkSearchAggs = Search query Nothing Nothing (Just mkSearchAggs) Nothing False (From 0) (Size 0)
485485
486486-- | 'mkHighlightSearch' is a helper function that defaults everything in a 'Search' except for
487487-- the 'Query' and the 'Aggregation'.
@@ -490,7 +490,7 @@ mkAggregateSearch query mkSearchAggs = Search query Nothing Nothing (Just mkSear
490490-- >>> let testHighlight = Highlights Nothing [FieldHighlight (FieldName "message") Nothing]
491491-- >>> let search = mkHighlightSearch (Just query) testHighlight
492492mkHighlightSearch :: Maybe Query -> Highlights -> Search
493−mkHighlightSearch query searchHighlights = Search query Nothing Nothing Nothing (Just searchHighlights) False 0 10
493+mkHighlightSearch query searchHighlights = Search query Nothing Nothing Nothing (Just searchHighlights) False (From 0) (Size 10)
494494
495495-- | 'pageSearch' is a helper function that takes a search and assigns the from
496496-- and size fields for the search. The from parameter defines the offset
@@ -500,11 +500,11 @@ mkHighlightSearch query searchHighlights = Search query Nothing Nothing Nothing
500500-- >>> let query = QueryMatchQuery $ mkMatchQuery (FieldName "_all") (QueryString "haskell")
501501-- >>> let search = mkSearch (Just query) Nothing
502502-- >>> search
503−-- Search {queryBody = Just (QueryMatchQuery (MatchQuery {matchQueryField = FieldName "_all", matchQueryQueryString = QueryString "haskell", matchQueryOperator = Or, matchQueryZeroTerms = ZeroTermsNone, matchQueryCutoffFrequency = Nothing, matchQueryMatchType = Nothing, matchQueryAnalyzer = Nothing, matchQueryMaxExpansions = Nothing, matchQueryLenient = Nothing})), filterBody = Nothing, sortBody = Nothing, aggBody = Nothing, highlight = Nothing, trackSortScores = False, from = 0, size = 10}
504−-- >>> pageSearch 10 100 search
505−-- Search {queryBody = Just (QueryMatchQuery (MatchQuery {matchQueryField = FieldName "_all", matchQueryQueryString = QueryString "haskell", matchQueryOperator = Or, matchQueryZeroTerms = ZeroTermsNone, matchQueryCutoffFrequency = Nothing, matchQueryMatchType = Nothing, matchQueryAnalyzer = Nothing, matchQueryMaxExpansions = Nothing, matchQueryLenient = Nothing})), filterBody = Nothing, sortBody = Nothing, aggBody = Nothing, highlight = Nothing, trackSortScores = False, from = 10, size = 100}
506−pageSearch :: Int -- ^ The result offset
507− -> Int -- ^ The number of results to return
503+-- Search {queryBody = Just (QueryMatchQuery (MatchQuery {matchQueryField = FieldName "_all", matchQueryQueryString = QueryString "haskell", matchQueryOperator = Or, matchQueryZeroTerms = ZeroTermsNone, matchQueryCutoffFrequency = Nothing, matchQueryMatchType = Nothing, matchQueryAnalyzer = Nothing, matchQueryMaxExpansions = Nothing, matchQueryLenient = Nothing})), filterBody = Nothing, sortBody = Nothing, aggBody = Nothing, highlight = Nothing, trackSortScores = False, from = From 0, size = Size 10}
504+-- >>> pageSearch (From 10) (Size 100) search
505+-- Search {queryBody = Just (QueryMatchQuery (MatchQuery {matchQueryField = FieldName "_all", matchQueryQueryString = QueryString "haskell", matchQueryOperator = Or, matchQueryZeroTerms = ZeroTermsNone, matchQueryCutoffFrequency = Nothing, matchQueryMatchType = Nothing, matchQueryAnalyzer = Nothing, matchQueryMaxExpansions = Nothing, matchQueryLenient = Nothing})), filterBody = Nothing, sortBody = Nothing, aggBody = Nothing, highlight = Nothing, trackSortScores = False, from = From 10, size = Size 100}
506+pageSearch :: From -- ^ The result offset
507+ -> Size -- ^ The number of results to return
508508 -> Search -- ^ The current seach
509509 -> Search -- ^ The paged search
510510pageSearch resultOffset pageSize search = search { from = resultOffset, size = pageSize }
src/Database/Bloodhound/Types.hs+4−4
@@ -61,8 +61,8 @@ module Database.Bloodhound.Types
6161 , SearchResult(..)
6262 , SearchHits(..)
6363 , TrackSortScores
64− , From
65− , Size
64+ , From(..)
65+ , Size(..)
6666 , ShardResult(..)
6767 , Hit(..)
6868 , Filter(..)
@@ -593,8 +593,8 @@ unpackId :: DocId -> Text
593593unpackId (DocId docId) = docId
594594
595595type TrackSortScores = Bool
596−type From = Int
597−type Size = Int
596+newtype From = From Int deriving (Eq, Show, ToJSON)
597+newtype Size = Size Int deriving (Eq, Show, ToJSON)
598598
599599data Search = Search { queryBody :: Maybe Query
600600 , filterBody :: Maybe Filter
tests/tests.hs+1−1
@@ -345,7 +345,7 @@ main = hspec $ do
345345 let sortSpec = DefaultSortSpec $ mkSort (FieldName "age") Ascending
346346 let search = Search Nothing
347347 (Just IdentityFilter) (Just [sortSpec]) Nothing Nothing
348− False 0 10
348+ False (From 0) (Size 10)
349349 reply <- searchByIndex testIndex search
350350 let result = eitherDecode (responseBody reply) :: Either String (SearchResult Tweet)
351351 let myTweet = fmap (hitSource . head . hits . searchHits) result