@@ -47,7 +47,7 @@ module Database.Bloodhound.Types
4747 , toTerms
4848 , toDateHistogram
4949 , omitNulls
50− , BH
50+ , BH(..)
5151 , runBH
5252 , BHEnv(..)
5353 , MonadBH(..)
@@ -75,6 +75,11 @@ module Database.Bloodhound.Types
7575 , TrackSortScores
7676 , From(..)
7777 , Size(..)
78+ , Source(..)
79+ , PatternOrPatterns(..)
80+ , Include(..)
81+ , Exclude(..)
82+ , Pattern(..)
7883 , ShardResult(..)
7984 , Hit(..)
8085 , Filter(..)
@@ -225,6 +230,7 @@ module Database.Bloodhound.Types
225230 ) where
226231
227232import Control.Applicative
233+import Control.Monad.Catch
228234import Control.Monad.Error
229235import Control.Monad.Reader
230236import Control.Monad.State
@@ -232,9 +238,9 @@ import Control.Monad.Writer
232238import Data.Aeson
233239import Data.Aeson.Types (Pair, emptyObject, parseMaybe)
234240import qualified Data.ByteString.Lazy.Char8 as L
235−import Data.List (nub, foldl')
236−import Data.List.NonEmpty (NonEmpty (..), toList)
237241import qualified Data.HashMap.Strict as HM (union)
242+import Data.List (foldl', nub)
243+import Data.List.NonEmpty (NonEmpty (..), toList)
238244import qualified Data.Map.Strict as M
239245import Data.Maybe
240246import Data.Text (Text)
@@ -285,7 +291,10 @@ newtype BH m a = BH {
285291 , MonadError e
286292 , Alternative
287293 , MonadPlus
288− , MonadFix)
294+ , MonadFix
295+ , MonadThrow
296+ , MonadCatch
297+ , MonadMask)
289298
290299instance MonadTrans BH where
291300 lift = BH . lift
@@ -368,7 +377,7 @@ data FieldDefinition =
368377 https://www.elastic.co/guide/en/elasticsearch/reference/1.7/indices-templates.html
369378-}
370379data IndexTemplate =
371− IndexTemplate { templatePattern :: TemplatePattern
380+ IndexTemplate { templatePattern :: TemplatePattern
372381 , templateSettings :: Maybe IndexSettings
373382 , templateMappings :: [Value]
374383 }
@@ -402,9 +411,9 @@ data BulkOperation =
402411{-| 'EsResult' describes the standard wrapper JSON document that you see in
403412 successful Elasticsearch lookups or lookups that couldn't find the document.
404413-}
405−data EsResult a = EsResult { _index :: Text
406− , _type :: Text
407− , _id :: Text
414+data EsResult a = EsResult { _index :: Text
415+ , _type :: Text
416+ , _id :: Text
408417 , foundResult :: Maybe (EsResultFound a)} deriving (Eq, Show)
409418
410419
@@ -412,7 +421,7 @@ data EsResult a = EsResult { _index :: Text
412421 'EsResult' when the document was successfully found.
413422-}
414423data EsResultFound a = EsResultFound { _version :: DocVersion
415− , _source :: a } deriving (Eq, Show)
424+ , _source :: a } deriving (Eq, Show)
416425
417426
418427
@@ -733,11 +742,12 @@ data Search = Search { queryBody :: Maybe Query
733742 -- default False
734743 , trackSortScores :: TrackSortScores
735744 , from :: From
736− , size :: Size
745+ , size :: Size
737746 , searchType :: SearchType
738− , fields :: Maybe [FieldName] } deriving (Eq, Show)
747+ , fields :: Maybe [FieldName]
748+ , source :: Maybe Source } deriving (Eq, Show)
739749
740−data SearchType = SearchTypeQueryThenFetch
750+data SearchType = SearchTypeQueryThenFetch
741751 | SearchTypeDfsQueryThenFetch
742752 | SearchTypeCount
743753 | SearchTypeScan
@@ -745,6 +755,20 @@ data SearchType = SearchTypeQueryThenFetch
745755 | SearchTypeDfsQueryAndFetch
746756 deriving (Eq, Show)
747757
758+data Source =
759+ NoSource
760+ | SourcePatterns PatternOrPatterns
761+ | SourceIncludeExclude Include Exclude
762+ deriving (Show, Eq)
763+
764+data PatternOrPatterns = PopPattern Pattern
765+ | PopPatterns [Pattern] deriving (Eq, Show)
766+
767+data Include = Include [Pattern] deriving (Eq, Show)
768+data Exclude = Exclude [Pattern] deriving (Eq, Show)
769+
770+newtype Pattern = Pattern Text deriving (Eq, Show)
771+
748772data Highlights = Highlights { globalsettings :: Maybe HighlightSettings
749773 , highlightFields :: [FieldHighlight]
750774 } deriving (Show, Eq)
@@ -1031,13 +1055,14 @@ data MatchQuery =
10311055 , matchQueryMatchType :: Maybe MatchQueryType
10321056 , matchQueryAnalyzer :: Maybe Analyzer
10331057 , matchQueryMaxExpansions :: Maybe MaxExpansions
1034− , matchQueryLenient :: Maybe Lenient } deriving (Eq, Show)
1058+ , matchQueryLenient :: Maybe Lenient
1059+ , matchQueryBoost :: Maybe Boost } deriving (Eq, Show)
10351060
10361061{-| 'mkMatchQuery' is a convenience function that defaults the less common parameters,
10371062 enabling you to provide only the 'FieldName' and 'QueryString' to make a 'MatchQuery'
10381063-}
10391064mkMatchQuery :: FieldName -> QueryString -> MatchQuery
1040−mkMatchQuery field query = MatchQuery field query Or ZeroTermsNone Nothing Nothing Nothing Nothing Nothing
1065+mkMatchQuery field query = MatchQuery field query Or ZeroTermsNone Nothing Nothing Nothing Nothing Nothing Nothing
10411066
10421067data MatchQueryType =
10431068 MatchPhrase
@@ -1256,7 +1281,7 @@ data SearchResult a =
12561281 , timedOut :: Bool
12571282 , shards :: ShardResult
12581283 , searchHits :: SearchHits a
1259− , aggregations :: Maybe AggregationResults
1284+ , aggregations :: Maybe AggregationResults
12601285 , scrollId :: Maybe ScrollId } deriving (Eq, Show)
12611286
12621287type ScrollId = Text -- Fixme: Newtype
@@ -1367,7 +1392,7 @@ data ValueCountAggregation = FieldValueCount FieldName
13671392
13681393-- | Single-bucket filter aggregations. See <https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filter-aggregation.html#search-aggregations-bucket-filter-aggregation> for more information.
13691394data FilterAggregation = FilterAggregation { faFilter :: Filter
1370− , faAggs :: Maybe Aggregations} deriving (Eq, Show)
1395+ , faAggs :: Maybe Aggregations} deriving (Eq, Show)
13711396
13721397mkTermsAggregation :: Text -> TermsAggregation
13731398mkTermsAggregation t = TermsAggregation (Left t) Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing
@@ -1967,7 +1992,7 @@ instance ToJSON MatchQuery where
19671992 toJSON (MatchQuery (FieldName fieldName)
19681993 (QueryString mqQueryString) booleanOperator
19691994 zeroTermsQuery cutoffFrequency matchQueryType
1970− analyzer maxExpansions lenient) =
1995+ analyzer maxExpansions lenient boost) =
19711996 object [ fieldName .= omitNulls base ]
19721997 where base = [ "query" .= mqQueryString
19731998 , "operator" .= booleanOperator
@@ -1976,7 +2001,8 @@ instance ToJSON MatchQuery where
19762001 , "type" .= matchQueryType
19772002 , "analyzer" .= analyzer
19782003 , "max_expansions" .= maxExpansions
1979− , "lenient" .= lenient ]
2004+ , "lenient" .= lenient
2005+ , "boost" .= boost ]
19802006
19812007
19822008instance ToJSON MultiMatchQuery where
@@ -2108,7 +2134,7 @@ instance (FromJSON a) => FromJSON (EsResultFound a) where
21082134 parseJSON _ = empty
21092135
21102136instance ToJSON Search where
2111− toJSON (Search query sFilter sort searchAggs highlight sTrackSortScores sFrom sSize _ sFields) =
2137+ toJSON (Search query sFilter sort searchAggs highlight sTrackSortScores sFrom sSize _ sFields sSource) =
21122138 omitNulls [ "query" .= query
21132139 , "filter" .= sFilter
21142140 , "sort" .= sort
@@ -2117,7 +2143,27 @@ instance ToJSON Search where
21172143 , "from" .= sFrom
21182144 , "size" .= sSize
21192145 , "track_scores" .= sTrackSortScores
2120− , "fields" .= sFields]
2146+ , "fields" .= sFields
2147+ , "_source" .= sSource]
2148+
2149+
2150+instance ToJSON Source where
2151+ toJSON NoSource = toJSON False
2152+ toJSON (SourcePatterns patterns) = toJSON patterns
2153+ toJSON (SourceIncludeExclude incl excl) = object [ "include" .= incl, "exclude" .= excl ]
2154+
2155+instance ToJSON PatternOrPatterns where
2156+ toJSON (PopPattern pattern) = toJSON pattern
2157+ toJSON (PopPatterns patterns) = toJSON patterns
2158+
2159+instance ToJSON Include where
2160+ toJSON (Include patterns) = toJSON patterns
2161+
2162+instance ToJSON Exclude where
2163+ toJSON (Exclude patterns) = toJSON patterns
2164+
2165+instance ToJSON Pattern where
2166+ toJSON (Pattern pattern) = toJSON pattern
21212167
21222168
21232169instance ToJSON FieldHighlight where