From 3d6302d5c3167b1906c77649de93d92dfa4b7984 Mon Sep 17 00:00:00 2001 From: Andrew Martin Date: Mon, 10 Jun 2019 16:33:35 -0400 Subject: [PATCH 1/2] make search hits total work with ES7 --- src/Database/V5/Bloodhound/Internal/Aggregation.hs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Database/V5/Bloodhound/Internal/Aggregation.hs b/src/Database/V5/Bloodhound/Internal/Aggregation.hs index 7265915a..c5f3df27 100644 --- a/src/Database/V5/Bloodhound/Internal/Aggregation.hs +++ b/src/Database/V5/Bloodhound/Internal/Aggregation.hs @@ -431,9 +431,15 @@ data SearchHits a = , hits :: [Hit a] } deriving (Eq, Show) +parserHitsV7 :: Aeson.Value -> Parser Int +parserHitsV7 obj = + parseJSON obj + <|> + withObject "hits-v7" (\v -> v .: "value") obj + instance (FromJSON a) => FromJSON (SearchHits a) where parseJSON (Object v) = SearchHits <$> - v .: "total" <*> + (v .: "total" >>= parserHitsV7) <*> v .: "max_score" <*> v .: "hits" parseJSON _ = empty From 6fcf96d1df3817bc13802c9ecf04b4dce380871f Mon Sep 17 00:00:00 2001 From: Andrew Martin Date: Thu, 13 Jun 2019 15:25:11 -0400 Subject: [PATCH 2/2] add search settings for track_total_hits --- src/Database/V5/Bloodhound/Client.hs | 6 +++--- src/Database/V5/Bloodhound/Types.hs | 6 ++++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Database/V5/Bloodhound/Client.hs b/src/Database/V5/Bloodhound/Client.hs index 181a2e9c..1e762732 100644 --- a/src/Database/V5/Bloodhound/Client.hs +++ b/src/Database/V5/Bloodhound/Client.hs @@ -1136,7 +1136,7 @@ scanSearch indexName mappingName search = do -- >>> mkSearch (Just query) Nothing -- Search {queryBody = Just (TermQuery (Term {termField = "user", termValue = "bitemyapp"}) Nothing), filterBody = Nothing, searchAfterKey = Nothing, sortBody = Nothing, aggBody = Nothing, highlight = Nothing, trackSortScores = False, from = From 0, size = Size 10, searchType = SearchTypeQueryThenFetch, fields = Nothing, source = Nothing} mkSearch :: Maybe Query -> Maybe Filter -> Search -mkSearch query filter = Search query filter Nothing Nothing Nothing False (From 0) (Size 10) SearchTypeQueryThenFetch Nothing Nothing Nothing Nothing Nothing +mkSearch query filter = Search query filter Nothing Nothing Nothing False (From 0) (Size 10) False SearchTypeQueryThenFetch Nothing Nothing Nothing Nothing Nothing -- | 'mkAggregateSearch' is a helper function that defaults everything in a 'Search' except for -- the 'Query' and the 'Aggregation'. @@ -1146,7 +1146,7 @@ mkSearch query filter = Search query filter Nothing Nothing Nothing False (From -- 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 (From 0) (Size 0) SearchTypeQueryThenFetch Nothing Nothing Nothing Nothing Nothing +mkAggregateSearch query mkSearchAggs = Search query Nothing Nothing (Just mkSearchAggs) Nothing False (From 0) (Size 0) False SearchTypeQueryThenFetch Nothing Nothing Nothing Nothing Nothing -- | 'mkHighlightSearch' is a helper function that defaults everything in a 'Search' except for -- the 'Query' and the 'Aggregation'. @@ -1155,7 +1155,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 (From 0) (Size 10) SearchTypeQueryThenFetch Nothing Nothing Nothing Nothing Nothing +mkHighlightSearch query searchHighlights = Search query Nothing Nothing Nothing (Just searchHighlights) False (From 0) (Size 10) False SearchTypeQueryThenFetch Nothing Nothing Nothing Nothing Nothing -- | '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 diff --git a/src/Database/V5/Bloodhound/Types.hs b/src/Database/V5/Bloodhound/Types.hs index 266c0223..a1b6450b 100644 --- a/src/Database/V5/Bloodhound/Types.hs +++ b/src/Database/V5/Bloodhound/Types.hs @@ -444,6 +444,7 @@ data Search = Search { queryBody :: Maybe Query , trackSortScores :: TrackSortScores , from :: From , size :: Size + , trackTotalHits :: Bool , searchType :: SearchType , searchAfterKey :: Maybe SearchAfterKey , fields :: Maybe [FieldName] @@ -455,14 +456,15 @@ data Search = Search { queryBody :: Maybe Query instance ToJSON Search where toJSON (Search mquery sFilter sort searchAggs - highlight sTrackSortScores sFrom sSize _ sAfter sFields - sScriptFields sSource sSuggest) = + highlight sTrackSortScores sFrom sSize sTrackTotalHits _ + sAfter sFields sScriptFields sSource sSuggest) = omitNulls [ "query" .= query' , "sort" .= sort , "aggregations" .= searchAggs , "highlight" .= highlight , "from" .= sFrom , "size" .= sSize + , "track_total_hits" .= sTrackTotalHits , "track_scores" .= sTrackSortScores , "search_after" .= sAfter , "fields" .= sFields