From 2a2ed660a8e2c3e8b19149048a66b5587aa5b54b Mon Sep 17 00:00:00 2001 From: Maria Knorps Date: Wed, 8 Jan 2020 12:57:39 +0100 Subject: [PATCH 1/6] ADD count endpoint connection --- src/Database/Bloodhound/Client.hs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/Database/Bloodhound/Client.hs b/src/Database/Bloodhound/Client.hs index 6f263881..32c3a6b0 100644 --- a/src/Database/Bloodhound/Client.hs +++ b/src/Database/Bloodhound/Client.hs @@ -57,6 +57,7 @@ module Database.Bloodhound.Client -- ** Searching , searchAll , searchByIndex + , countByIndex , searchByIndices , searchByIndexTemplate , searchByIndicesTemplate @@ -1070,6 +1071,16 @@ searchByIndex :: MonadBH m => IndexName -> Search -> m Reply searchByIndex (IndexName indexName) = bindM2 dispatchSearch url . return where url = joinPath [indexName, "_search"] +-- | 'countByIndex', given a 'Search' and an 'IndexName', will perform that search +-- within an index on an Elasticsearch server. +-- +-- >>> let query = TermQuery (Term "user" "bitemyapp") Nothing +-- >>> let search = mkSearch (Just query) Nothing +-- >>> reply <- runBH' $ countByIndex testIndex search +countByIndex :: MonadBH m => IndexName -> Search -> m Reply +countByIndex (IndexName indexName) = bindM2 dispatchSearch url . return + where url = joinPath [indexName, "_count"] + -- | 'searchByIndices' is a variant of 'searchByIndex' that executes a -- 'Search' over many indices. This is much faster than using -- 'mapM' to 'searchByIndex' over a collection since it only From 6d1580b3639e5e83c56eb5e6cef1932f0db47486 Mon Sep 17 00:00:00 2001 From: Maria Knorps Date: Wed, 8 Jan 2020 14:24:14 +0100 Subject: [PATCH 2/6] simplified query handling for count endpoint --- src/Database/Bloodhound/Client.hs | 26 ++++++++++++++++++++++++-- src/Database/Bloodhound/Types.hs | 11 ++++++++++- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/Database/Bloodhound/Client.hs b/src/Database/Bloodhound/Client.hs index 32c3a6b0..11024b06 100644 --- a/src/Database/Bloodhound/Client.hs +++ b/src/Database/Bloodhound/Client.hs @@ -1051,6 +1051,11 @@ dispatchSearch :: MonadBH m => Text -> Search -> m Reply dispatchSearch url search = post url' (Just (encode search)) where url' = appendSearchTypeParam url (searchType search) +appendCountTypeParam = appendSearchTypeParam +dispatchCount :: MonadBH m => Text -> Count -> m Reply +dispatchCount url count = post url' (Just (encode count)) + where url' = appendCountTypeParam url (countSearchType count) + -- | 'searchAll', given a 'Search', will perform that search against all indexes -- on an Elasticsearch server. Try to avoid doing this if it can be helped. -- @@ -1077,8 +1082,8 @@ searchByIndex (IndexName indexName) = bindM2 dispatchSearch url . return -- >>> let query = TermQuery (Term "user" "bitemyapp") Nothing -- >>> let search = mkSearch (Just query) Nothing -- >>> reply <- runBH' $ countByIndex testIndex search -countByIndex :: MonadBH m => IndexName -> Search -> m Reply -countByIndex (IndexName indexName) = bindM2 dispatchSearch url . return +countByIndex :: MonadBH m => IndexName -> Count -> m Reply +countByIndex (IndexName indexName) = bindM2 dispatchCount url . return where url = joinPath [indexName, "_count"] -- | 'searchByIndices' is a variant of 'searchByIndex' that executes a @@ -1238,6 +1243,23 @@ scanSearch indexName search = do 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 + + +mkCount :: Maybe Query -> Count +mkCount query = Count query SearchTypeQueryThenFetch + +-- | 'mkCountSearch' is a helper function for defaulting additional fields of a 'Search' +-- to Nothing in case you only care about your 'Query' and 'Filter'. Use record update +-- syntax if you want to add things like aggregations or highlights while still using +-- this helper function. +-- +-- >>> let query = TermQuery (Term "user" "bitemyapp") Nothing +-- >>> mkCountSearch (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} +mkCountSearch :: Maybe Query -> Maybe Filter -> Search +mkCountSearch query filter = Search query filter Nothing Nothing Nothing False (From 0) (Size 10) SearchTypeQueryThenFetch Nothing Nothing Nothing Nothing Nothing + + -- | 'mkAggregateSearch' is a helper function that defaults everything in a 'Search' except for -- the 'Query' and the 'Aggregation'. -- diff --git a/src/Database/Bloodhound/Types.hs b/src/Database/Bloodhound/Types.hs index 98c6fc2b..5329a2bf 100644 --- a/src/Database/Bloodhound/Types.hs +++ b/src/Database/Bloodhound/Types.hs @@ -105,6 +105,7 @@ module Database.Bloodhound.Types , JoinRelation(..) , IndexDocumentSettings(..) , Query(..) + , Count(..) , Search(..) , SearchType(..) , SearchResult(..) @@ -459,7 +460,15 @@ data Search = Search { queryBody :: Maybe Query , suggestBody :: Maybe Suggest -- ^ Only one Suggestion request / response per Search is supported. } deriving (Eq, Show) +data Count = Count { countQueryBody :: Maybe Query + , countSearchType :: SearchType + } deriving (Eq, Show) + +instance ToJSON Count where + toJSON (Count mquery _) = + omitNulls [ "query" .= mquery] + instance ToJSON Search where toJSON (Search mquery sFilter sort searchAggs highlight sTrackSortScores sFrom sSize _ sAfter sFields @@ -657,4 +666,4 @@ instance FromJSON GetTemplateScript where v .: "found" ) script - parseJSON _ = empty \ No newline at end of file + parseJSON _ = empty From 6255f10b59e1ec62ba027d1980a086ee0741c9f2 Mon Sep 17 00:00:00 2001 From: Maria Knorps Date: Wed, 8 Jan 2020 14:29:26 +0100 Subject: [PATCH 3/6] expose mkCount --- src/Database/Bloodhound/Client.hs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Database/Bloodhound/Client.hs b/src/Database/Bloodhound/Client.hs index 11024b06..421b9bab 100644 --- a/src/Database/Bloodhound/Client.hs +++ b/src/Database/Bloodhound/Client.hs @@ -67,6 +67,7 @@ module Database.Bloodhound.Client , advanceScroll , refreshIndex , mkSearch + , mkCount , mkAggregateSearch , mkHighlightSearch , mkSearchTemplate From 3d6d1a5677abc377e18ae83d229ad9d91ef50cec Mon Sep 17 00:00:00 2001 From: Maria Knorps Date: Thu, 9 Jan 2020 09:06:18 +0100 Subject: [PATCH 4/6] ADD type for CountResults --- src/Database/Bloodhound/Types.hs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Database/Bloodhound/Types.hs b/src/Database/Bloodhound/Types.hs index 5329a2bf..e780b241 100644 --- a/src/Database/Bloodhound/Types.hs +++ b/src/Database/Bloodhound/Types.hs @@ -539,6 +539,18 @@ newtype Pattern = Pattern Text deriving (Eq, Read, Show) instance ToJSON Pattern where toJSON (Pattern pattern) = toJSON pattern +data CountResult a = + CountResult { crCount :: Int + , crShards :: ShardResult + } + deriving (Eq, Show) + +instance (FromJSON a) => FromJSON (CountResult a) where + parseJSON (Object v) = CountResult <$> + v .: "count" <*> + v .: "_shards" + parseJSON _ = empty + data SearchResult a = SearchResult { took :: Int , timedOut :: Bool From fcd52e2bb271882fc46262eb7db76503bd3b14e3 Mon Sep 17 00:00:00 2001 From: Maria Knorps Date: Thu, 9 Jan 2020 10:38:28 +0100 Subject: [PATCH 5/6] expose `CountResult` --- src/Database/Bloodhound/Types.hs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Database/Bloodhound/Types.hs b/src/Database/Bloodhound/Types.hs index e780b241..36d16e53 100644 --- a/src/Database/Bloodhound/Types.hs +++ b/src/Database/Bloodhound/Types.hs @@ -109,6 +109,7 @@ module Database.Bloodhound.Types , Search(..) , SearchType(..) , SearchResult(..) + , CountResult(..) , ScrollId(..) , HitsTotalRelation(..) , HitsTotal(..) From 10d3da37f99ee242fc92e4919c8869f4e395573d Mon Sep 17 00:00:00 2001 From: Maria Knorps Date: Tue, 4 Feb 2020 19:47:15 +0100 Subject: [PATCH 6/6] Cleanup of description and remove unused function --- src/Database/Bloodhound/Client.hs | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/src/Database/Bloodhound/Client.hs b/src/Database/Bloodhound/Client.hs index 421b9bab..c755b22c 100644 --- a/src/Database/Bloodhound/Client.hs +++ b/src/Database/Bloodhound/Client.hs @@ -1245,20 +1245,14 @@ 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 - -mkCount :: Maybe Query -> Count -mkCount query = Count query SearchTypeQueryThenFetch - --- | 'mkCountSearch' is a helper function for defaulting additional fields of a 'Search' --- to Nothing in case you only care about your 'Query' and 'Filter'. Use record update --- syntax if you want to add things like aggregations or highlights while still using --- this helper function. +-- | 'mkCount' is a helper function for defaulting additional field of a 'Count' +-- and resemble the similar function for Search endpoint -- -- >>> let query = TermQuery (Term "user" "bitemyapp") Nothing --- >>> mkCountSearch (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} -mkCountSearch :: Maybe Query -> Maybe Filter -> Search -mkCountSearch query filter = Search query filter Nothing Nothing Nothing False (From 0) (Size 10) SearchTypeQueryThenFetch Nothing Nothing Nothing Nothing Nothing +-- >>> mkCount (Just query) +-- Count {countQueryBody = Just (TermQuery (Term {termField = "user", termValue = "bitemyapp"}) Nothing), countSearchType = SearchTypeQueryThenFetch} +mkCount :: Maybe Query -> Count +mkCount query = Count query SearchTypeQueryThenFetch -- | 'mkAggregateSearch' is a helper function that defaults everything in a 'Search' except for