git.iko.soy/typeable/bloodhound
on master help sign in
SearchType type and field in Search, append query param to search endpoint based on search type
c2a362eJosh Berman
src/Database/Bloodhound/Client.hs+25−5
@@ -59,6 +59,7 @@ import Data.ByteString.Lazy.Builder
59import qualified Data.ByteString.Lazy.Char8 as L
59import qualified Data.ByteString.Lazy.Char8 as L
60import Data.Default.Class
60import Data.Default.Class
61import Data.Maybe (fromMaybe)
61import Data.Maybe (fromMaybe)
62+import Data.Monoid ((<>))
62import Data.Text (Text)
63import Data.Text (Text)
63import qualified Data.Text as T
64import qualified Data.Text as T
64import qualified Data.Text.Encoding as T
65import qualified Data.Text.Encoding as T
@@ -160,10 +161,28 @@ joinPath ps = do
160 Server s <- bhServer <$> getBHEnv
161 Server s <- bhServer <$> getBHEnv
161 return $ joinPath' (s:ps)
162 return $ joinPath' (s:ps)
162
163
164+appendQueryParam :: Text -> Text -> Text
165+appendQueryParam originalUrl newParams = originalUrl <> joiner <> newParams
166+ where joiner
167+ | T.any (== '?') originalUrl = "&"
168+ | otherwise = "?"
169+
170+appendSearchTypeParam :: Text -> SearchType -> Text
171+appendSearchTypeParam originalUrl st = appendQueryParam originalUrl (keyEq <> stParams)
172+ where keyEq = "search_type="
173+ stParams
174+ | st == SearchTypeDfsQueryThenFetch = "dfs_query_then_fetch"
175+ | st == SearchTypeCount = "count"
176+ | st == SearchTypeScan = "scan&scroll=1m"
177+ | st == SearchTypeQueryAndFetch = "query_and_fetch"
178+ | st == SearchTypeDfsQueryAndFetch = "dfs_query_and_fetch"
179+ -- used to catch 'SearchTypeQueryThenFetch', which is also the default
180+ | otherwise = "query_then_fetch"
181+
163bindM2 :: (Applicative m, Monad m) => (a -> b -> m c) -> m a -> m b -> m c
182bindM2 :: (Applicative m, Monad m) => (a -> b -> m c) -> m a -> m b -> m c
164bindM2 f ma mb = join (f <$> ma <*> mb)
183bindM2 f ma mb = join (f <$> ma <*> mb)
165
184
166−-- | Convenience function that sets up a mananager and BHEnv and runs
185+-- | Convenience function that sets up a manager and BHEnv and runs
167-- the given set of bloodhound operations. Connections will be
186-- the given set of bloodhound operations. Connections will be
168-- pipelined automatically in accordance with the given manager
187-- pipelined automatically in accordance with the given manager
169-- settings in IO. If you've got your own monad transformer stack, you
188-- settings in IO. If you've got your own monad transformer stack, you
@@ -428,7 +447,8 @@ documentExists (IndexName indexName)
428 where url = joinPath [indexName, mappingName, docId]
447 where url = joinPath [indexName, mappingName, docId]
429
448
430dispatchSearch :: MonadBH m => Text -> Search -> m Reply
449dispatchSearch :: MonadBH m => Text -> Search -> m Reply
431−dispatchSearch url search = post url (Just (encode search))
450+dispatchSearch url search = post url' (Just (encode search))
451+ where url' = appendSearchTypeParam url (searchType search)
432
452
433-- | 'searchAll', given a 'Search', will perform that search against all indexes
453-- | 'searchAll', given a 'Search', will perform that search against all indexes
434-- on an Elasticsearch server. Try to avoid doing this if it can be helped.
454-- on an Elasticsearch server. Try to avoid doing this if it can be helped.
@@ -471,7 +491,7 @@ searchByType (IndexName indexName)
471-- >>> mkSearch (Just query) Nothing
491-- >>> 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 = From 0, size = Size 10}
492-- 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}
473mkSearch :: Maybe Query -> Maybe Filter -> Search
493mkSearch :: Maybe Query -> Maybe Filter -> Search
474−mkSearch query filter = Search query filter Nothing Nothing Nothing False (From 0) (Size 10)
494+mkSearch query filter = Search query filter Nothing Nothing Nothing False (From 0) (Size 10) SearchTypeQueryThenFetch
475
495
476-- | 'mkAggregateSearch' is a helper function that defaults everything in a 'Search' except for
496-- | 'mkAggregateSearch' is a helper function that defaults everything in a 'Search' except for
477-- the 'Query' and the 'Aggregation'.
497-- the 'Query' and the 'Aggregation'.
@@ -481,7 +501,7 @@ mkSearch query filter = Search query filter Nothing Nothing Nothing False (From
481-- TermsAgg (TermsAggregation {term = Left "user", termInclude = Nothing, termExclude = Nothing, termOrder = Nothing, termMinDocCount = Nothing, termSize = Nothing, termShardSize = Nothing, termCollectMode = Just BreadthFirst, termExecutionHint = Nothing, termAggs = Nothing})
501-- TermsAgg (TermsAggregation {term = Left "user", termInclude = Nothing, termExclude = Nothing, termOrder = Nothing, termMinDocCount = Nothing, termSize = Nothing, termShardSize = Nothing, termCollectMode = Just BreadthFirst, termExecutionHint = Nothing, termAggs = Nothing})
482-- >>> let myAggregation = mkAggregateSearch Nothing $ mkAggregations "users" terms
502-- >>> let myAggregation = mkAggregateSearch Nothing $ mkAggregations "users" terms
483mkAggregateSearch :: Maybe Query -> Aggregations -> Search
503mkAggregateSearch :: Maybe Query -> Aggregations -> Search
484−mkAggregateSearch query mkSearchAggs = Search query Nothing Nothing (Just mkSearchAggs) Nothing False (From 0) (Size 0)
504+mkAggregateSearch query mkSearchAggs = Search query Nothing Nothing (Just mkSearchAggs) Nothing False (From 0) (Size 0) SearchTypeQueryThenFetch
485
505
486-- | 'mkHighlightSearch' is a helper function that defaults everything in a 'Search' except for
506-- | 'mkHighlightSearch' is a helper function that defaults everything in a 'Search' except for
487-- the 'Query' and the 'Aggregation'.
507-- the 'Query' and the 'Aggregation'.
@@ -490,7 +510,7 @@ mkAggregateSearch query mkSearchAggs = Search query Nothing Nothing (Just mkSear
490-- >>> let testHighlight = Highlights Nothing [FieldHighlight (FieldName "message") Nothing]
510-- >>> let testHighlight = Highlights Nothing [FieldHighlight (FieldName "message") Nothing]
491-- >>> let search = mkHighlightSearch (Just query) testHighlight
511-- >>> let search = mkHighlightSearch (Just query) testHighlight
492mkHighlightSearch :: Maybe Query -> Highlights -> Search
512mkHighlightSearch :: Maybe Query -> Highlights -> Search
493−mkHighlightSearch query searchHighlights = Search query Nothing Nothing Nothing (Just searchHighlights) False (From 0) (Size 10)
513+mkHighlightSearch query searchHighlights = Search query Nothing Nothing Nothing (Just searchHighlights) False (From 0) (Size 10) SearchTypeQueryThenFetch
494
514
495-- | 'pageSearch' is a helper function that takes a search and assigns the from
515-- | 'pageSearch' is a helper function that takes a search and assigns the from
496-- and size fields for the search. The from parameter defines the offset
516-- and size fields for the search. The from parameter defines the offset
src/Database/Bloodhound/Types.hs+12−2
@@ -58,6 +58,7 @@ module Database.Bloodhound.Types
58 , EsResult(..)
58 , EsResult(..)
59 , Query(..)
59 , Query(..)
60 , Search(..)
60 , Search(..)
61+ , SearchType(..)
61 , SearchResult(..)
62 , SearchResult(..)
62 , SearchHits(..)
63 , SearchHits(..)
63 , TrackSortScores
64 , TrackSortScores
@@ -604,7 +605,16 @@ data Search = Search { queryBody :: Maybe Query
604 -- default False
605 -- default False
605 , trackSortScores :: TrackSortScores
606 , trackSortScores :: TrackSortScores
606 , from :: From
607 , from :: From
607− , size :: Size } deriving (Eq, Show)
608+ , size :: Size
609+ , searchType :: SearchType } deriving (Eq, Show)
610+
611+data SearchType = SearchTypeQueryThenFetch
612+ | SearchTypeDfsQueryThenFetch
613+ | SearchTypeCount
614+ | SearchTypeScan
615+ | SearchTypeQueryAndFetch
616+ | SearchTypeDfsQueryAndFetch
617+ deriving (Eq, Show)
608
618
609data Highlights = Highlights { globalsettings :: Maybe HighlightSettings
619data Highlights = Highlights { globalsettings :: Maybe HighlightSettings
610 , highlightFields :: [FieldHighlight]
620 , highlightFields :: [FieldHighlight]
@@ -1919,7 +1929,7 @@ instance (FromJSON a) => FromJSON (EsResult a) where
1919
1929
1920
1930
1921instance ToJSON Search where
1931instance ToJSON Search where
1922− toJSON (Search query sFilter sort searchAggs highlight sTrackSortScores sFrom sSize) =
1932+ toJSON (Search query sFilter sort searchAggs highlight sTrackSortScores sFrom sSize _) =
1923 omitNulls [ "query" .= query
1933 omitNulls [ "query" .= query
1924 , "filter" .= sFilter
1934 , "filter" .= sFilter
1925 , "sort" .= sort
1935 , "sort" .= sort