Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make search hits total work with ES7 #253

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Database/V5/Bloodhound/Client.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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'.
Expand All @@ -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'.
Expand All @@ -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
Expand Down
8 changes: 7 additions & 1 deletion src/Database/V5/Bloodhound/Internal/Aggregation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/Database/V5/Bloodhound/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Expand Down