From 354423cba669edc1a2971af2c447d9e16ece6121 Mon Sep 17 00:00:00 2001 From: Michael Xavier Date: Mon, 11 May 2015 21:55:33 -0700 Subject: [PATCH] Newtype over From/Size --- README.md | 4 ++-- src/Database/Bloodhound/Client.hs | 18 +++++++++--------- src/Database/Bloodhound/Types.hs | 8 ++++---- tests/tests.hs | 2 +- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index ad3e809..75bf284 100644 --- a/README.md +++ b/README.md @@ -383,7 +383,7 @@ Search -- exported by the Client module, just defaults some stuff. -- mkSearch :: Maybe Query -> Maybe Filter -> Search --- mkSearch query filter = Search query filter Nothing False 0 10 +-- mkSearch query filter = Search query filter Nothing False (From 0) (Size 10) let query = TermQuery (Term "user" "bitemyapp") Nothing @@ -488,7 +488,7 @@ let sortSpec = DefaultSortSpec $ mkSort (FieldName "age") Ascending -- -> From -> Size -- just add more sortspecs to the list if you want tie-breakers. -let search = Search Nothing (Just IdentityFilter) (Just [sortSpec]) False 0 10 +let search = Search Nothing (Just IdentityFilter) (Just [sortSpec]) False (From 0) (Size 10) ``` diff --git a/src/Database/Bloodhound/Client.hs b/src/Database/Bloodhound/Client.hs index 9603d85..a2ad67d 100644 --- a/src/Database/Bloodhound/Client.hs +++ b/src/Database/Bloodhound/Client.hs @@ -469,9 +469,9 @@ searchByType (IndexName indexName) -- -- >>> let query = TermQuery (Term "user" "bitemyapp") Nothing -- >>> mkSearch (Just query) Nothing --- Search {queryBody = Just (TermQuery (Term {termField = "user", termValue = "bitemyapp"}) Nothing), filterBody = Nothing, sortBody = Nothing, aggBody = Nothing, highlight = Nothing, trackSortScores = False, from = 0, size = 10} +-- 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} mkSearch :: Maybe Query -> Maybe Filter -> Search -mkSearch query filter = Search query filter Nothing Nothing Nothing False 0 10 +mkSearch query filter = Search query filter Nothing Nothing Nothing False (From 0) (Size 10) -- | 'mkAggregateSearch' is a helper function that defaults everything in a 'Search' except for -- the 'Query' and the 'Aggregation'. @@ -481,7 +481,7 @@ mkSearch query filter = Search query filter Nothing Nothing Nothing False 0 10 -- TermsAgg (TermsAggregation {term = Left "user", termInclude = Nothing, termExclude = Nothing, termOrder = Nothing, termMinDocCount = Nothing, termSize = Nothing, termShardSize = Nothing, termCollectMode = Just BreadthFirst, termExecutionHint = Nothing, termAggs = Nothing}) -- >>> let myAggregation = mkAggregateSearch Nothing $ mkAggregations "users" terms mkAggregateSearch :: Maybe Query -> Aggregations -> Search -mkAggregateSearch query mkSearchAggs = Search query Nothing Nothing (Just mkSearchAggs) Nothing False 0 0 +mkAggregateSearch query mkSearchAggs = Search query Nothing Nothing (Just mkSearchAggs) Nothing False (From 0) (Size 0) -- | 'mkHighlightSearch' is a helper function that defaults everything in a 'Search' except for -- the 'Query' and the 'Aggregation'. @@ -490,7 +490,7 @@ mkAggregateSearch query mkSearchAggs = Search query Nothing Nothing (Just mkSear -- >>> let testHighlight = Highlights Nothing [FieldHighlight (FieldName "message") Nothing] -- >>> let search = mkHighlightSearch (Just query) testHighlight mkHighlightSearch :: Maybe Query -> Highlights -> Search -mkHighlightSearch query searchHighlights = Search query Nothing Nothing Nothing (Just searchHighlights) False 0 10 +mkHighlightSearch query searchHighlights = Search query Nothing Nothing Nothing (Just searchHighlights) False (From 0) (Size 10) -- | 'pageSearch' is a helper function that takes a search and assigns the from -- and size fields for the search. The from parameter defines the offset @@ -500,11 +500,11 @@ mkHighlightSearch query searchHighlights = Search query Nothing Nothing Nothing -- >>> let query = QueryMatchQuery $ mkMatchQuery (FieldName "_all") (QueryString "haskell") -- >>> let search = mkSearch (Just query) Nothing -- >>> search --- 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} --- >>> pageSearch 10 100 search --- 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} -pageSearch :: Int -- ^ The result offset - -> Int -- ^ The number of results to return +-- 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} +-- >>> pageSearch (From 10) (Size 100) search +-- 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} +pageSearch :: From -- ^ The result offset + -> Size -- ^ The number of results to return -> Search -- ^ The current seach -> Search -- ^ The paged search pageSearch resultOffset pageSize search = search { from = resultOffset, size = pageSize } diff --git a/src/Database/Bloodhound/Types.hs b/src/Database/Bloodhound/Types.hs index 66d9f2e..c208cb8 100644 --- a/src/Database/Bloodhound/Types.hs +++ b/src/Database/Bloodhound/Types.hs @@ -61,8 +61,8 @@ module Database.Bloodhound.Types , SearchResult(..) , SearchHits(..) , TrackSortScores - , From - , Size + , From(..) + , Size(..) , ShardResult(..) , Hit(..) , Filter(..) @@ -593,8 +593,8 @@ unpackId :: DocId -> Text unpackId (DocId docId) = docId type TrackSortScores = Bool -type From = Int -type Size = Int +newtype From = From Int deriving (Eq, Show, ToJSON) +newtype Size = Size Int deriving (Eq, Show, ToJSON) data Search = Search { queryBody :: Maybe Query , filterBody :: Maybe Filter diff --git a/tests/tests.hs b/tests/tests.hs index 0b94298..e08efdf 100644 --- a/tests/tests.hs +++ b/tests/tests.hs @@ -345,7 +345,7 @@ main = hspec $ do let sortSpec = DefaultSortSpec $ mkSort (FieldName "age") Ascending let search = Search Nothing (Just IdentityFilter) (Just [sortSpec]) Nothing Nothing - False 0 10 + False (From 0) (Size 10) reply <- searchByIndex testIndex search let result = eitherDecode (responseBody reply) :: Either String (SearchResult Tweet) let myTweet = fmap (hitSource . head . hits . searchHits) result