{-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE RecordWildCards #-} {-# LANGUAGE UndecidableInstances #-} ------------------------------------------------------------------------------- -- | -- Module : Database.Bloodhound.Types -- Copyright : (C) 2014, 2018 Chris Allen -- License : BSD-style (see the file LICENSE) -- Maintainer : Chris Allen Text unpackId (DocId docId) = docId type TrackSortScores = Bool data Search = Search { queryBody :: Maybe Query , filterBody :: Maybe Filter , sortBody :: Maybe Sort , aggBody :: Maybe Aggregations , highlight :: Maybe Highlights -- default False , trackSortScores :: TrackSortScores , from :: From , size :: Size , searchType :: SearchType , fields :: Maybe [FieldName] , scriptFields :: Maybe ScriptFields , source :: Maybe Source , suggestBody :: Maybe Suggest -- ^ Only one Suggestion request / response per Search is supported. } deriving (Eq, Show) instance ToJSON Search where toJSON (Search mquery sFilter sort searchAggs highlight sTrackSortScores sFrom sSize _ sFields sScriptFields sSource sSuggest) = omitNulls [ "query" .= query' , "sort" .= sort , "aggregations" .= searchAggs , "highlight" .= highlight , "from" .= sFrom , "size" .= sSize , "track_scores" .= sTrackSortScores , "fields" .= sFields , "script_fields" .= sScriptFields , "_source" .= sSource , "suggest" .= sSuggest] where query' = case sFilter of Nothing -> mquery Just x -> Just . QueryBoolQuery $ mkBoolQuery (maybeToList mquery) [x] [] [] data SearchType = SearchTypeQueryThenFetch | SearchTypeDfsQueryThenFetch deriving (Eq, Show) data Source = NoSource | SourcePatterns PatternOrPatterns | SourceIncludeExclude Include Exclude deriving (Eq, Show) instance ToJSON Source where toJSON NoSource = toJSON False toJSON (SourcePatterns patterns) = toJSON patterns toJSON (SourceIncludeExclude incl excl) = object [ "includes" .= incl, "excludes" .= excl ] data PatternOrPatterns = PopPattern Pattern | PopPatterns [Pattern] deriving (Eq, Read, Show) instance ToJSON PatternOrPatterns where toJSON (PopPattern pattern) = toJSON pattern toJSON (PopPatterns patterns) = toJSON patterns data Include = Include [Pattern] deriving (Eq, Read, Show) data Exclude = Exclude [Pattern] deriving (Eq, Read, Show) instance ToJSON Include where toJSON (Include patterns) = toJSON patterns instance ToJSON Exclude where toJSON (Exclude patterns) = toJSON patterns newtype Pattern = Pattern Text deriving (Eq, Read, Show) instance ToJSON Pattern where toJSON (Pattern pattern) = toJSON pattern data SearchResult a = SearchResult { took :: Int , timedOut :: Bool , shards :: ShardResult , searchHits :: SearchHits a , aggregations :: Maybe AggregationResults , scrollId :: Maybe ScrollId -- ^ Only one Suggestion request / response per -- Search is supported. , suggest :: Maybe NamedSuggestionResponse } deriving (Eq, Show) instance (FromJSON a) => FromJSON (SearchResult a) where parseJSON (Object v) = SearchResult <$> v .: "took" <*> v .: "timed_out" <*> v .: "_shards" <*> v .: "hits" <*> v .:? "aggregations" <*> v .:? "_scroll_id" <*> v .:? "suggest" parseJSON _ = empty newtype ScrollId = ScrollId Text deriving (Eq, Show, Ord, ToJSON, FromJSON)