@@ -1,8 +1,10 @@
1+{-# LANGUAGE DeriveDataTypeable #-}
2+{-# LANGUAGE DeriveGeneric #-}
1{-# LANGUAGE FlexibleContexts #-}
3{-# LANGUAGE FlexibleContexts #-}
2{-# LANGUAGE FlexibleInstances #-}
4{-# LANGUAGE FlexibleInstances #-}
3{-# LANGUAGE GeneralizedNewtypeDeriving #-}
5{-# LANGUAGE GeneralizedNewtypeDeriving #-}
4{-# LANGUAGE MultiParamTypeClasses #-}
6{-# LANGUAGE MultiParamTypeClasses #-}
5−{-# LANGUAGE NamedFieldPuns #-}
7+{-# LANGUAGE NoMonomorphismRestriction #-}
6{-# LANGUAGE OverloadedStrings #-}
8{-# LANGUAGE OverloadedStrings #-}
7{-# LANGUAGE RecordWildCards #-}
9{-# LANGUAGE RecordWildCards #-}
8{-# LANGUAGE UndecidableInstances #-}
10{-# LANGUAGE UndecidableInstances #-}
@@ -10,11 +12,11 @@
10-------------------------------------------------------------------------------
12-------------------------------------------------------------------------------
12-- Module : Database.Bloodhound.Types
14-- Module : Database.Bloodhound.Types
13−-- Copyright : (C) 2014, 2018 Chris Allen
15+-- Copyright : (C) 2014 Chris Allen
14-- License : BSD-style (see the file LICENSE)
16-- License : BSD-style (see the file LICENSE)
15-- Maintainer : Chris Allen <cma@bitemyapp.com
17-- Maintainer : Chris Allen <cma@bitemyapp.com
16-- Stability : provisional
18-- Stability : provisional
17−-- Portability : RecordWildCards
19+-- Portability : DeriveGeneric, RecordWildCards
19-- Data types for describing actions and data structures performed to interact
21-- Data types for describing actions and data structures performed to interact
20-- with Elasticsearch. The two main buckets your queries against Elasticsearch
22-- with Elasticsearch. The two main buckets your queries against Elasticsearch
@@ -42,28 +44,16 @@ module Database.Bloodhound.Types
42 , mkTermsAggregation
44 , mkTermsAggregation
43 , mkTermsScriptAggregation
45 , mkTermsScriptAggregation
44 , mkDateHistogram
46 , mkDateHistogram
45− , mkCardinalityAggregation
46 , mkDocVersion
47 , mkDocVersion
48− , mkExtendedStatsAggregation
49 , docVersionNumber
48 , docVersionNumber
52 , toDateHistogram
50 , toDateHistogram
54 , omitNulls
51 , omitNulls
57− , BHEnv
54+ , BHEnv(..)
62 , MonadBH(..)
55 , MonadBH(..)
63 , Version(..)
56 , Version(..)
67 , Status(..)
57 , Status(..)
68 , Existence(..)
58 , Existence(..)
69 , NullValue(..)
59 , NullValue(..)
@@ -71,12 +61,8 @@ module Database.Bloodhound.Types
71 , UpdatableIndexSetting(..)
61 , UpdatableIndexSetting(..)
72 , IndexSettingsSummary(..)
62 , IndexSettingsSummary(..)
73 , AllocationPolicy(..)
63 , AllocationPolicy(..)
75 , ReplicaBounds(..)
64 , ReplicaBounds(..)
76 , Bytes(..)
65 , Bytes(..)
80 , FSType(..)
66 , FSType(..)
81 , InitialShardCount(..)
67 , InitialShardCount(..)
82 , NodeAttrFilter(..)
68 , NodeAttrFilter(..)
@@ -120,8 +106,8 @@ module Database.Bloodhound.Types
120 , Pattern(..)
106 , Pattern(..)
121 , ShardResult(..)
107 , ShardResult(..)
122 , Hit(..)
108 , Hit(..)
124 , Filter(..)
109 , Filter(..)
125 , BoolMatch(..)
111 , BoolMatch(..)
126 , Term(..)
112 , Term(..)
127 , GeoPoint(..)
113 , GeoPoint(..)
@@ -148,22 +134,8 @@ module Database.Bloodhound.Types
148 , RegexpFlags(..)
134 , RegexpFlags(..)
149 , RegexpFlag(..)
135 , RegexpFlag(..)
150 , FieldName(..)
136 , FieldName(..)
154 , Script(..)
137 , Script(..)
155− , ScriptLanguage(..)
161 , IndexName(..)
138 , IndexName(..)
162− , IndexSelection(..)
165− , ForceMergeIndexSettings(..)
166− , defaultForceMergeIndexSettings
167 , TemplateName(..)
139 , TemplateName(..)
168 , TemplatePattern(..)
140 , TemplatePattern(..)
169 , MappingName(..)
141 , MappingName(..)
@@ -187,19 +159,8 @@ module Database.Bloodhound.Types
187 , BoolQuery(..)
159 , BoolQuery(..)
188 , BoostingQuery(..)
160 , BoostingQuery(..)
189 , CommonTermsQuery(..)
161 , CommonTermsQuery(..)
190− , FunctionScoreQuery(..)
193− , FunctionScoreFunctions(..)
194− , ComponentFunctionScoreFunction(..)
195− , FunctionScoreFunction(..)
198− , FieldValueFactor(..)
200− , FactorModifier(..)
201− , FactorMissingFieldValue(..)
202 , DisMaxQuery(..)
162 , DisMaxQuery(..)
203 , FuzzyLikeThisQuery(..)
164 , FuzzyLikeThisQuery(..)
204 , FuzzyLikeFieldQuery(..)
165 , FuzzyLikeFieldQuery(..)
205 , FuzzyQuery(..)
166 , FuzzyQuery(..)
@@ -215,16 +176,10 @@ module Database.Bloodhound.Types
215 , RangeQuery(..)
176 , RangeQuery(..)
216 , RegexpQuery(..)
177 , RegexpQuery(..)
217 , QueryString(..)
178 , QueryString(..)
218− , TemplateQueryInline(..)
219− , TemplateQueryKeyValuePairs(..)
221 , BooleanOperator(..)
179 , BooleanOperator(..)
222 , ZeroTermsQuery(..)
180 , ZeroTermsQuery(..)
223 , CutoffFrequency(..)
181 , CutoffFrequency(..)
224 , Analyzer(..)
182 , Analyzer(..)
228 , MaxExpansions(..)
183 , MaxExpansions(..)
229 , Lenient(..)
184 , Lenient(..)
230 , MatchQueryType(..)
185 , MatchQueryType(..)
@@ -271,108 +226,15 @@ module Database.Bloodhound.Types
271 , CollectionMode(..)
226 , CollectionMode(..)
272 , TermOrder(..)
227 , TermOrder(..)
273 , TermInclusion(..)
228 , TermInclusion(..)
274− , SnapshotRepoSelection(..)
275− , GenericSnapshotRepo(..)
277− , SnapshotRepoConversionError(..)
278− , SnapshotRepoType(..)
279− , GenericSnapshotRepoSettings(..)
280− , SnapshotRepoUpdateSettings(..)
281− , defaultSnapshotRepoUpdateSettings
282− , SnapshotRepoName(..)
283− , SnapshotRepoPattern(..)
284− , SnapshotVerification(..)
285− , SnapshotNodeVerification(..)
292− , NodeBreakersStats(..)
293− , NodeBreakerStats(..)
295− , NodeTransportStats(..)
297− , NodeDataPathStats(..)
298− , NodeFSTotalStats(..)
299− , NodeNetworkStats(..)
300− , NodeThreadPoolsStats(..)
301− , NodeThreadPoolStats(..)
303− , JVMBufferPoolStats(..)
306− , NodeProcessStats(..)
309− , NodeIndicesStats(..)
313− , NodePluginInfo(..)
315− , NodeTransportInfo(..)
316− , BoundTransportAddress(..)
317− , NodeNetworkInfo(..)
319− , NetworkInterfaceName(..)
320− , NodeNetworkInterface(..)
321− , NodeThreadPoolsInfo(..)
322− , NodeThreadPoolInfo(..)
323− , ThreadPoolSize(..)
324− , ThreadPoolType(..)
327− , JVMGCCollector(..)
332− , NodeProcessInfo(..)
333− , FsSnapshotRepo(..)
334− , SnapshotCreateSettings(..)
335− , defaultSnapshotCreateSettings
336− , SnapshotSelection(..)
337− , SnapshotPattern(..)
339− , SnapshotShardFailure(..)
343− , SnapshotRestoreSettings(..)
344− , defaultSnapshotRestoreSettings
345− , RestoreRenamePattern(..)
346− , RestoreRenameToken(..)
350− , RestoreIndexSettings(..)
353− , PhraseSuggester(..)
354− , PhraseSuggesterHighlighter(..)
355− , PhraseSuggesterCollate(..)
357− , SuggestOptions(..)
358− , SuggestResponse(..)
359− , NamedSuggestionResponse(..)
360− , DirectGenerators(..)
361− , mkDirectGenerators
362− , DirectGeneratorSuggestModeTypes (..)
364 , Aggregation(..)
230 , Aggregation(..)
365 , Aggregations
231 , Aggregations
366 , AggregationResults
232 , AggregationResults
368 , Bucket(..)
233 , Bucket(..)
369 , BucketAggregation(..)
234 , BucketAggregation(..)
370 , TermsAggregation(..)
235 , TermsAggregation(..)
371− , MissingAggregation(..)
372 , ValueCountAggregation(..)
236 , ValueCountAggregation(..)
373 , FilterAggregation(..)
237 , FilterAggregation(..)
374− , CardinalityAggregation(..)
375− , MaxAggregation(..)
376 , DateHistogramAggregation(..)
238 , DateHistogramAggregation(..)
377 , DateRangeAggregation(..)
239 , DateRangeAggregation(..)
378 , DateRangeAggRange(..)
240 , DateRangeAggRange(..)
@@ -380,9 +242,6 @@ module Database.Bloodhound.Types
380 , DateMathAnchor(..)
242 , DateMathAnchor(..)
381 , DateMathModifier(..)
243 , DateMathModifier(..)
382 , DateMathUnit(..)
244 , DateMathUnit(..)
383− , TopHitsAggregation(..)
384− , StatisticsAggregation(..)
387 , Highlights(..)
246 , Highlights(..)
388 , FieldHighlight(..)
247 , FieldHighlight(..)
@@ -396,36 +255,655 @@ module Database.Bloodhound.Types
396 , HighlightTag(..)
255 , HighlightTag(..)
397 , HitHighlight
256 , HitHighlight
400 , TermsResult(..)
258 , TermsResult(..)
401 , DateHistogramResult(..)
259 , DateHistogramResult(..)
402 , DateRangeResult(..)
260 , DateRangeResult(..)
403− , TopHitResult(..)
261+ ) where
405− , EsUsername(..)
263+import Control.Applicative
406− , EsPassword(..)
264+import Control.Monad.Catch
407−
265+import Control.Monad.Except
408− , Analysis(..)
266+import Control.Monad.Reader
409− , AnalyzerDefinition(..)
267+import Control.Monad.State
410− , TokenizerDefinition(..)
268+import Control.Monad.Writer
411− , TokenFilterDefinition(..)
269+import Data.Aeson
412− , CharFilterDefinition(..)
270+import Data.Aeson.Types (Pair, Parser, emptyObject,
413− , Ngram(..)
271+ parseMaybe)
414− , TokenChar(..)
272+import qualified Data.ByteString.Lazy.Char8 as L
415− , Shingle(..)
273+import Data.Char
416− , Language(..)
274+import Data.Hashable (Hashable)
417− ) where
275+import qualified Data.HashMap.Strict as HM
418−
276+import Data.List (foldl', nub)
419−import Bloodhound.Import
277+import Data.List.NonEmpty (NonEmpty (..), toList)
420−
278+import qualified Data.Map.Strict as M
421−import Database.Bloodhound.Internal.Aggregation
279+import Data.Maybe
422−import Database.Bloodhound.Internal.Analysis
280+import Data.Text (Text)
423−import Database.Bloodhound.Internal.Client
281+import qualified Data.Text as T
424−import Database.Bloodhound.Internal.Highlight
282+import Data.Time.Calendar
425−import Database.Bloodhound.Internal.Newtypes
283+import Data.Time.Clock (NominalDiffTime, UTCTime)
426−import Database.Bloodhound.Internal.Query
284+import Data.Time.Clock.POSIX
427−import Database.Bloodhound.Internal.Sort
285+import qualified Data.Traversable as DT
428−import Database.Bloodhound.Internal.Suggest
286+import Data.Typeable (Typeable)
287+import qualified Data.Vector as V
289+import GHC.Generics (Generic)
290+import Network.HTTP.Client
291+import qualified Network.HTTP.Types.Method as NHTM
293+import Database.Bloodhound.Types.Class
296+-- >>> :set -XOverloadedStrings
297+-- >>> import Database.Bloodhound
298+-- >>> let testServer = (Server "http://localhost:9200")
299+-- >>> let testIndex = IndexName "twitter"
300+-- >>> let testMapping = MappingName "tweet"
301+-- >>> let defaultIndexSettings = IndexSettings (ShardCount 3) (ReplicaCount 2)
303+-- defaultIndexSettings is exported by Database.Bloodhound as well
304+-- no trailing slashes in servers, library handles building the path.
306+{-| Common environment for Elasticsearch calls. Connections will be
307+ pipelined according to the provided HTTP connection manager.
309+data BHEnv = BHEnv { bhServer :: Server
310+ , bhManager :: Manager
313+{-| All API calls to Elasticsearch operate within
314+ MonadBH. The idea is that it can be easily embedded in your
315+ own monad transformer stack. A default instance for a ReaderT and
316+ alias 'BH' is provided for the simple case.
318+class (Functor m, Applicative m, MonadIO m) => MonadBH m where
321+newtype BH m a = BH {
322+ unBH :: ReaderT BHEnv m a
323+ } deriving ( Functor
337+instance MonadTrans BH where
340+instance (MonadReader r m) => MonadReader r (BH m) where
342+ local f (BH (ReaderT m)) = BH $ ReaderT $ \r ->
345+instance (Functor m, Applicative m, MonadIO m) => MonadBH (BH m) where
346+ getBHEnv = BH getBHEnv
349+instance (Functor m, Applicative m, MonadIO m) => MonadBH (ReaderT BHEnv m) where
352+runBH :: BHEnv -> BH m a -> m a
353+runBH e f = runReaderT (unBH f) e
355+{-| 'Version' is embedded in 'Status' -}
356+data Version = Version { number :: Text
357+ , build_hash :: Text
358+ , build_timestamp :: UTCTime
359+ , build_snapshot :: Bool
360+ , lucene_version :: Text } deriving (Eq, Show, Generic, Typeable)
362+{-| 'Status' is a data type for describing the JSON body returned by
363+ Elasticsearch when you query its status. This was deprecated in 1.2.0.
365+ <http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-status.html#indices-status>
368+data Status = Status { ok :: Maybe Bool
371+ , version :: Version
372+ , tagline :: Text } deriving (Eq, Show, Generic)
374+{-| 'IndexSettings' is used to configure the shards and replicas when you create
375+ an Elasticsearch Index.
377+ <http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-create-index.html>
381+ IndexSettings { indexShards :: ShardCount
382+ , indexReplicas :: ReplicaCount } deriving (Eq, Show, Generic, Typeable)
384+{-| 'defaultIndexSettings' is an 'IndexSettings' with 3 shards and 2 replicas. -}
385+defaultIndexSettings :: IndexSettings
386+defaultIndexSettings = IndexSettings (ShardCount 3) (ReplicaCount 2)
388+{-| 'UpdatableIndexSetting' are settings which may be updated after an index is created.
390+ <https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-update-settings.html>
392+data UpdatableIndexSetting = NumberOfReplicas ReplicaCount
393+ -- ^ The number of replicas each shard has.
394+ | AutoExpandReplicas ReplicaBounds
395+ | BlocksReadOnly Bool
396+ -- ^ Set to True to have the index read only. False to allow writes and metadata changes.
398+ -- ^ Set to True to disable read operations against the index.
400+ -- ^ Set to True to disable write operations against the index.
401+ | BlocksMetaData Bool
402+ -- ^ Set to True to disable metadata operations against the index.
403+ | RefreshInterval NominalDiffTime
404+ -- ^ The async refresh interval of a shard
405+ | IndexConcurrency Int
406+ | FailOnMergeFailure Bool
407+ | TranslogFlushThresholdOps Int
408+ -- ^ When to flush on operations.
409+ | TranslogFlushThresholdSize Bytes
410+ -- ^ When to flush based on translog (bytes) size.
411+ | TranslogFlushThresholdPeriod NominalDiffTime
412+ -- ^ When to flush based on a period of not flushing.
413+ | TranslogDisableFlush Bool
414+ -- ^ Disables flushing. Note, should be set for a short interval and then enabled.
415+ | CacheFilterMaxSize (Maybe Bytes)
416+ -- ^ The maximum size of filter cache (per segment in shard).
417+ | CacheFilterExpire (Maybe NominalDiffTime)
418+ -- ^ The expire after access time for filter cache.
419+ | GatewaySnapshotInterval NominalDiffTime
420+ -- ^ The gateway snapshot interval (only applies to shared gateways).
421+ | RoutingAllocationInclude (NonEmpty NodeAttrFilter)
422+ -- ^ A node matching any rule will be allowed to host shards from the index.
423+ | RoutingAllocationExclude (NonEmpty NodeAttrFilter)
424+ -- ^ A node matching any rule will NOT be allowed to host shards from the index.
425+ | RoutingAllocationRequire (NonEmpty NodeAttrFilter)
426+ -- ^ Only nodes matching all rules will be allowed to host shards from the index.
427+ | RoutingAllocationEnable AllocationPolicy
428+ -- ^ Enables shard allocation for a specific index.
429+ | RoutingAllocationShardsPerNode ShardCount
430+ -- ^ Controls the total number of shards (replicas and primaries) allowed to be allocated on a single node.
431+ | RecoveryInitialShards InitialShardCount
432+ -- ^ When using local gateway a particular shard is recovered only if there can be allocated quorum shards in the cluster.
433+ | GCDeletes NominalDiffTime
434+ | TTLDisablePurge Bool
435+ -- ^ Disables temporarily the purge of expired docs.
436+ | TranslogFSType FSType
437+ | IndexCompoundFormat CompoundFormat
438+ | IndexCompoundOnFlush Bool
439+ | WarmerEnabled Bool
440+ deriving (Eq, Show, Generic, Typeable)
442+data AllocationPolicy = AllocAll
443+ -- ^ Allows shard allocation for all shards.
445+ -- ^ Allows shard allocation only for primary shards.
447+ -- ^ Allows shard allocation only for primary shards for new indices.
449+ -- ^ No shard allocation is allowed
450+ deriving (Eq, Show, Generic, Typeable)
452+data ReplicaBounds = ReplicasBounded Int Int
453+ | ReplicasLowerBounded Int
455+ deriving (Eq, Show, Generic, Typeable)
457+newtype Bytes = Bytes Int deriving (Eq, Show, Generic, Typeable, Ord, ToJSON, FromJSON)
459+data FSType = FSSimple
460+ | FSBuffered deriving (Eq, Show, Generic, Typeable, Ord)
462+data InitialShardCount = QuorumShards
463+ | QuorumMinus1Shards
466+ | ExplicitShards Int
467+ deriving (Eq, Show, Generic, Typeable)
469+data NodeAttrFilter = NodeAttrFilter { nodeAttrFilterName :: NodeAttrName
470+ , nodeAttrFilterValues :: NonEmpty Text}
471+ deriving (Eq, Show, Generic, Ord)
473+newtype NodeAttrName = NodeAttrName Text deriving (Eq, Show, Ord, Generic, Typeable)
475+data CompoundFormat = CompoundFileFormat Bool
476+ | MergeSegmentVsTotalIndex Double
477+ -- ^ percentage between 0 and 1 where 0 is false, 1 is true
478+ deriving (Eq, Show, Generic, Typeable)
480+newtype NominalDiffTimeJSON = NominalDiffTimeJSON { ndtJSON :: NominalDiffTime }
482+data IndexSettingsSummary = IndexSettingsSummary { sSummaryIndexName :: IndexName
483+ , sSummaryFixedSettings :: IndexSettings
484+ , sSummaryUpdateable :: [UpdatableIndexSetting]}
485+ deriving (Eq, Show, Generic, Typeable)
487+{-| 'Reply' and 'Method' are type synonyms from 'Network.HTTP.Types.Method.Method' -}
488+type Reply = Network.HTTP.Client.Response L.ByteString
489+type Method = NHTM.Method
491+{-| 'OpenCloseIndex' is a sum type for opening and closing indices.
493+ <http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-open-close.html>
495+data OpenCloseIndex = OpenIndex | CloseIndex deriving (Eq, Show, Generic, Typeable)
497+data FieldType = GeoPointType
503+ | ByteType deriving (Eq, Show, Generic, Typeable)
505+data FieldDefinition =
506+ FieldDefinition { fieldType :: FieldType } deriving (Eq, Show, Generic, Typeable)
508+{-| An 'IndexTemplate' defines a template that will automatically be
509+ applied to new indices created. The templates include both
510+ 'IndexSettings' and mappings, and a simple 'TemplatePattern' that
511+ controls if the template will be applied to the index created.
512+ Specify mappings as follows: @[toJSON TweetMapping, ...]@
514+ https://www.elastic.co/guide/en/elasticsearch/reference/1.7/indices-templates.html
517+ IndexTemplate { templatePattern :: TemplatePattern
518+ , templateSettings :: Maybe IndexSettings
519+ , templateMappings :: [Value]
523+ MappingField { mappingFieldName :: FieldName
524+ , fieldDefinition :: FieldDefinition } deriving (Eq, Show, Generic, Typeable)
526+{-| Support for type reification of 'Mapping's is currently incomplete, for
527+ now the mapping API verbiage expects a 'ToJSON'able blob.
529+ Indexes have mappings, mappings are schemas for the documents contained in the
530+ index. I'd recommend having only one mapping per index, always having a mapping,
531+ and keeping different kinds of documents separated if possible.
533+data Mapping = Mapping { typeName :: TypeName
534+ , mappingFields :: [MappingField] } deriving (Eq, Show, Generic, Typeable)
536+{-| 'BulkOperation' is a sum type for expressing the four kinds of bulk
537+ operation index, create, delete, and update. 'BulkIndex' behaves like an
538+ "upsert", 'BulkCreate' will fail if a document already exists at the DocId.
540+ <http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-bulk.html#docs-bulk>
543+ BulkIndex IndexName MappingName DocId Value
544+ | BulkCreate IndexName MappingName DocId Value
545+ | BulkDelete IndexName MappingName DocId
546+ | BulkUpdate IndexName MappingName DocId Value deriving (Eq, Show, Generic, Typeable)
548+{-| 'EsResult' describes the standard wrapper JSON document that you see in
549+ successful Elasticsearch lookups or lookups that couldn't find the document.
551+data EsResult a = EsResult { _index :: Text
554+ , foundResult :: Maybe (EsResultFound a)} deriving (Eq, Show, Generic, Typeable)
556+{-| 'EsResultFound' contains the document and its metadata inside of an
557+ 'EsResult' when the document was successfully found.
559+data EsResultFound a = EsResultFound { _version :: DocVersion
560+ , _source :: a } deriving (Eq, Show, Generic, Typeable)
562+{-| 'EsError' is the generic type that will be returned when there was a
563+ problem. If you can't parse the expected response, its a good idea to
566+data EsError = EsError { errorStatus :: Int
567+ , errorMessage :: Text } deriving (Eq, Show, Generic, Typeable)
569+{-| 'EsProtocolException' ideally should never be thrown. It will only
570+be thrown when Bloodhound can't parse a response returned by the
571+ElasticSearch server. This should be reported as a bug to the bug
572+tracker. Be sure to include the body.
574+data EsProtocolException = EsProtocolException { esProtoExBody :: L.ByteString }
575+ deriving (Eq, Show, Generic, Typeable)
577+instance Exception EsProtocolException
579+data IndexAlias = IndexAlias { srcIndex :: IndexName
580+ , indexAlias :: IndexAliasName } deriving (Eq, Show, Generic, Typeable)
582+newtype IndexAliasName = IndexAliasName { indexAliasName :: IndexName } deriving (Eq, Show, Generic, ToJSON)
584+data IndexAliasAction = AddAlias IndexAlias IndexAliasCreate
585+ | RemoveAlias IndexAlias deriving (Show, Eq, Generic, Typeable)
587+data IndexAliasCreate = IndexAliasCreate { aliasCreateRouting :: Maybe AliasRouting
588+ , aliasCreateFilter :: Maybe Filter}
589+ deriving (Show, Eq, Generic, Typeable)
591+data AliasRouting = AllAliasRouting RoutingValue
592+ | GranularAliasRouting (Maybe SearchAliasRouting) (Maybe IndexAliasRouting)
593+ deriving (Show, Eq, Generic, Typeable)
595+newtype SearchAliasRouting = SearchAliasRouting (NonEmpty RoutingValue) deriving (Show, Eq, Generic, Typeable)
597+newtype IndexAliasRouting = IndexAliasRouting RoutingValue deriving (Show, Eq, Generic, ToJSON, FromJSON, Typeable)
599+newtype RoutingValue = RoutingValue { routingValue :: Text } deriving (Show, Eq, Generic, ToJSON, FromJSON, Typeable)
601+newtype IndexAliasesSummary = IndexAliasesSummary { indexAliasesSummary :: [IndexAliasSummary] } deriving (Show, Eq, Generic, Typeable)
603+{-| 'IndexAliasSummary' is a summary of an index alias configured for a server. -}
604+data IndexAliasSummary = IndexAliasSummary { indexAliasSummaryAlias :: IndexAlias
605+ , indexAliasSummaryCreate :: IndexAliasCreate} deriving (Show, Eq, Generic, Typeable)
607+{-| 'DocVersion' is an integer version number for a document between 1
608+and 9.2e+18 used for <<https://www.elastic.co/guide/en/elasticsearch/guide/current/optimistic-concurrency-control.html optimistic concurrency control>>.
610+newtype DocVersion = DocVersion {
611+ docVersionNumber :: Int
612+ } deriving (Eq, Show, Generic, Ord, ToJSON)
614+-- | Smart constructor for in-range doc version
615+mkDocVersion :: Int -> Maybe DocVersion
617+ | i >= (docVersionNumber minBound) && i <= (docVersionNumber maxBound) =
619+ | otherwise = Nothing
622+{-| 'ExternalDocVersion' is a convenience wrapper if your code uses its
623+own version numbers instead of ones from ES.
625+newtype ExternalDocVersion = ExternalDocVersion DocVersion
626+ deriving (Eq, Show, Generic, Ord, Bounded, Enum, ToJSON)
628+{-| 'VersionControl' is specified when indexing documents as a
629+optimistic concurrency control.
631+data VersionControl = NoVersionControl
632+ -- ^ Don't send a version. This is a pure overwrite.
633+ | InternalVersion DocVersion
634+ -- ^ Use the default ES versioning scheme. Only
635+ -- index the document if the version is the same
636+ -- as the one specified. Only applicable to
637+ -- updates, as you should be getting Version from
639+ | ExternalGT ExternalDocVersion
640+ -- ^ Use your own version numbering. Only index
641+ -- the document if the version is strictly higher
642+ -- OR the document doesn't exist. The given
643+ -- version will be used as the new version number
644+ -- for the stored document. N.B. All updates must
645+ -- increment this number, meaning there is some
646+ -- global, external ordering of updates.
647+ | ExternalGTE ExternalDocVersion
648+ -- ^ Use your own version numbering. Only index
649+ -- the document if the version is equal or higher
650+ -- than the stored version. Will succeed if there
651+ -- is no existing document. The given version will
652+ -- be used as the new version number for the
653+ -- stored document. Use with care, as this could
654+ -- result in data loss.
655+ | ForceVersion ExternalDocVersion
656+ -- ^ The document will always be indexed and the
657+ -- given version will be the new version. This is
658+ -- typically used for correcting errors. Use with
659+ -- care, as this could result in data loss.
660+ deriving (Show, Eq, Generic, Ord)
662+{-| 'DocumentParent' is used to specify a parent document.
664+newtype DocumentParent = DocumentParent DocId
665+ deriving (Eq, Show, Generic, Typeable)
667+{-| 'IndexDocumentSettings' are special settings supplied when indexing
668+a document. For the best backwards compatiblity when new fields are
669+added, you should probably prefer to start with 'defaultIndexDocumentSettings'
671+data IndexDocumentSettings =
672+ IndexDocumentSettings { idsVersionControl :: VersionControl
673+ , idsParent :: Maybe DocumentParent
674+ } deriving (Eq, Show, Generic, Typeable)
676+{-| Reasonable default settings. Chooses no version control and no parent.
678+defaultIndexDocumentSettings :: IndexDocumentSettings
679+defaultIndexDocumentSettings = IndexDocumentSettings NoVersionControl Nothing
681+{-| 'Sort' is a synonym for a list of 'SortSpec's. Sort behavior is order
682+ dependent with later sorts acting as tie-breakers for earlier sorts.
684+type Sort = [SortSpec]
686+{-| The two main kinds of 'SortSpec' are 'DefaultSortSpec' and
687+ 'GeoDistanceSortSpec'. The latter takes a 'SortOrder', 'GeoPoint', and
688+ 'DistanceUnit' to express "nearness" to a single geographical point as a
691+<http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#search-request-sort>
693+data SortSpec = DefaultSortSpec DefaultSort
694+ | GeoDistanceSortSpec SortOrder GeoPoint DistanceUnit deriving (Eq, Show, Generic, Typeable)
696+{-| 'DefaultSort' is usually the kind of 'SortSpec' you'll want. There's a
697+ 'mkSort' convenience function for when you want to specify only the most
700+<http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#search-request-sort>
703+ DefaultSort { sortFieldName :: FieldName
704+ , sortOrder :: SortOrder
706+ , ignoreUnmapped :: Bool
707+ , sortMode :: Maybe SortMode
708+ , missingSort :: Maybe Missing
709+ , nestedFilter :: Maybe Filter } deriving (Eq, Show, Generic, Typeable)
711+{-| 'SortOrder' is 'Ascending' or 'Descending', as you might expect. These get
712+ encoded into "asc" or "desc" when turned into JSON.
714+<http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#search-request-sort>
716+data SortOrder = Ascending
717+ | Descending deriving (Eq, Show, Generic, Typeable)
719+{-| 'Missing' prescribes how to handle missing fields. A missing field can be
720+ sorted last, first, or using a custom value as a substitute.
722+<http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#_missing_values>
724+data Missing = LastMissing
726+ | CustomMissing Text deriving (Eq, Show, Generic, Typeable)
728+{-| 'SortMode' prescribes how to handle sorting array/multi-valued fields.
730+http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#_sort_mode_option
732+data SortMode = SortMin
735+ | SortAvg deriving (Eq, Show, Generic, Typeable)
737+{-| 'mkSort' defaults everything but the 'FieldName' and the 'SortOrder' so
738+ that you can concisely describe the usual kind of 'SortSpec's you want.
740+mkSort :: FieldName -> SortOrder -> DefaultSort
741+mkSort fieldName sOrder = DefaultSort fieldName sOrder False Nothing Nothing Nothing
743+{-| 'Cache' is for telling ES whether it should cache a 'Filter' not.
744+ 'Query's cannot be cached.
746+type Cache = Bool -- caching on/off
747+defaultCache :: Cache
750+{-| 'PrefixValue' is used in 'PrefixQuery' as the main query component.
752+type PrefixValue = Text
754+{-| 'BooleanOperator' is the usual And/Or operators with an ES compatible
755+ JSON encoding baked in. Used all over the place.
757+data BooleanOperator = And | Or deriving (Eq, Show, Generic, Typeable)
759+{-| 'ShardCount' is part of 'IndexSettings'
761+newtype ShardCount = ShardCount Int deriving (Eq, Show, Generic, ToJSON, Typeable)
763+{-| 'ReplicaCount' is part of 'IndexSettings'
765+newtype ReplicaCount = ReplicaCount Int deriving (Eq, Show, Generic, ToJSON, Typeable)
767+{-| 'Server' is used with the client functions to point at the ES instance
769+newtype Server = Server Text deriving (Eq, Show, Generic, Typeable)
771+{-| 'IndexName' is used to describe which index to query/create/delete
773+newtype IndexName = IndexName Text deriving (Eq, Generic, Show, ToJSON, FromJSON, Typeable)
775+{-| 'TemplateName' is used to describe which template to query/create/delete
777+newtype TemplateName = TemplateName Text deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
779+{-| 'TemplatePattern' represents a pattern which is matched against index names
781+newtype TemplatePattern = TemplatePattern Text deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
783+{-| 'MappingName' is part of mappings which are how ES describes and schematizes
784+ the data in the indices.
786+newtype MappingName = MappingName Text deriving (Eq, Generic, Show, ToJSON, FromJSON, Typeable)
788+{-| 'DocId' is a generic wrapper value for expressing unique Document IDs.
789+ Can be set by the user or created by ES itself. Often used in client
790+ functions for poking at specific documents.
792+newtype DocId = DocId Text deriving (Eq, Generic, Show, ToJSON, FromJSON, Typeable)
794+{-| 'QueryString' is used to wrap query text bodies, be they human written or not.
796+newtype QueryString = QueryString Text deriving (Eq, Generic, Show, ToJSON, FromJSON, Typeable)
798+{-| 'FieldName' is used all over the place wherever a specific field within
799+ a document needs to be specified, usually in 'Query's or 'Filter's.
801+newtype FieldName = FieldName Text deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
804+{-| 'Script' is often used in place of 'FieldName' to specify more
805+complex ways of extracting a value from a document.
807+newtype Script = Script { scriptText :: Text } deriving (Eq, Show, Generic, Typeable)
809+{-| 'CacheName' is used in 'RegexpFilter' for describing the
810+ 'CacheKey' keyed caching behavior.
812+newtype CacheName = CacheName Text deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
814+{-| 'CacheKey' is used in 'RegexpFilter' to key regex caching.
817+ CacheKey Text deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
819+ Existence Bool deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
821+ NullValue Bool deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
822+newtype CutoffFrequency =
823+ CutoffFrequency Double deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
825+ Analyzer Text deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
826+newtype MaxExpansions =
827+ MaxExpansions Int deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
829+{-| 'Lenient', if set to true, will cause format based failures to be
830+ ignored. I don't know what the bloody default is, Elasticsearch
831+ documentation didn't say what it was. Let me know if you figure it out.
834+ Lenient Bool deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
836+ Tiebreaker Double deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
838+ Boost Double deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
840+ BoostTerms Double deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
842+{-| 'MinimumMatch' controls how many should clauses in the bool query should
843+ match. Can be an absolute value (2) or a percentage (30%) or a
844+ combination of both.
846+newtype MinimumMatch =
847+ MinimumMatch Int deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
848+newtype DisableCoord =
849+ DisableCoord Bool deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
850+newtype IgnoreTermFrequency =
851+ IgnoreTermFrequency Bool deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
852+newtype MinimumTermFrequency =
853+ MinimumTermFrequency Int deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
854+newtype MaxQueryTerms =
855+ MaxQueryTerms Int deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
857+ Fuzziness Double deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
859+{-| 'PrefixLength' is the prefix length used in queries, defaults to 0. -}
860+newtype PrefixLength =
861+ PrefixLength Int deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
863+ TypeName Text deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
864+newtype PercentMatch =
865+ PercentMatch Double deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
867+ StopWord Text deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
869+ QueryPath Text deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
871+{-| Allowing a wildcard at the beginning of a word (eg "*ing") is particularly
872+ heavy, because all terms in the index need to be examined, just in case
873+ they match. Leading wildcards can be disabled by setting
874+ 'AllowLeadingWildcard' to false. -}
875+newtype AllowLeadingWildcard =
876+ AllowLeadingWildcard Bool deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
877+newtype LowercaseExpanded =
878+ LowercaseExpanded Bool deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
879+newtype EnablePositionIncrements =
880+ EnablePositionIncrements Bool deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
882+{-| By default, wildcard terms in a query are not analyzed.
883+ Setting 'AnalyzeWildcard' to true enables best-effort analysis.
885+newtype AnalyzeWildcard = AnalyzeWildcard Bool deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
887+{-| 'GeneratePhraseQueries' defaults to false.
889+newtype GeneratePhraseQueries =
890+ GeneratePhraseQueries Bool deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
892+{-| 'Locale' is used for string conversions - defaults to ROOT.
894+newtype Locale = Locale Text deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
895+newtype MaxWordLength = MaxWordLength Int deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
896+newtype MinWordLength = MinWordLength Int deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
898+{-| 'PhraseSlop' sets the default slop for phrases, 0 means exact
899+ phrase matches. Default is 0.
901+newtype PhraseSlop = PhraseSlop Int deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
902+newtype MinDocFrequency = MinDocFrequency Int deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
903+newtype MaxDocFrequency = MaxDocFrequency Int deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
905+-- | Newtype wrapper to parse ES's concerning tendency to in some APIs return a floating point number of milliseconds since epoch ಠ_ಠ
906+newtype POSIXMS = POSIXMS { posixMS :: UTCTime }
430{-| 'unpackId' is a silly convenience function that gets used once.
908{-| 'unpackId' is a silly convenience function that gets used once.
@@ -433,6 +911,8 @@ unpackId :: DocId -> Text
433unpackId (DocId docId) = docId
911unpackId (DocId docId) = docId
435type TrackSortScores = Bool
913type TrackSortScores = Bool
914+newtype From = From Int deriving (Eq, Show, Generic, ToJSON)
915+newtype Size = Size Int deriving (Eq, Show, Generic, ToJSON)
437data Search = Search { queryBody :: Maybe Query
917data Search = Search { queryBody :: Maybe Query
438 , filterBody :: Maybe Filter
918 , filterBody :: Maybe Filter
@@ -444,74 +924,537 @@ data Search = Search { queryBody :: Maybe Query
444 , from :: From
924 , from :: From
445 , size :: Size
925 , size :: Size
446 , searchType :: SearchType
926 , searchType :: SearchType
447− , searchAfterKey :: Maybe SearchAfterKey
448 , fields :: Maybe [FieldName]
927 , fields :: Maybe [FieldName]
449− , scriptFields :: Maybe ScriptFields
450− , source :: Maybe Source
928+ , source :: Maybe Source } deriving (Eq, Show, Generic, Typeable)
451− , suggestBody :: Maybe Suggest -- ^ Only one Suggestion request / response per Search is supported.
452− } deriving (Eq, Show)
455−instance ToJSON Search where
456− toJSON (Search mquery sFilter sort searchAggs
457− highlight sTrackSortScores sFrom sSize _ sAfter sFields
458− sScriptFields sSource sSuggest) =
459− omitNulls [ "query" .= query'
461− , "aggregations" .= searchAggs
462− , "highlight" .= highlight
465− , "track_scores" .= sTrackSortScores
466− , "search_after" .= sAfter
467− , "fields" .= sFields
468− , "script_fields" .= sScriptFields
469− , "_source" .= sSource
470− , "suggest" .= sSuggest]
472− where query' = case sFilter of
477− $ mkBoolQuery (maybeToList mquery)
480data SearchType = SearchTypeQueryThenFetch
930data SearchType = SearchTypeQueryThenFetch
481 | SearchTypeDfsQueryThenFetch
931 | SearchTypeDfsQueryThenFetch
934+ | SearchTypeQueryAndFetch
935+ | SearchTypeDfsQueryAndFetch
482− deriving (Eq, Show)
936+ deriving (Eq, Show, Generic, Typeable)
484data Source =
938data Source =
486 | SourcePatterns PatternOrPatterns
940 | SourcePatterns PatternOrPatterns
487 | SourceIncludeExclude Include Exclude
941 | SourceIncludeExclude Include Exclude
488− deriving (Eq, Show)
942+ deriving (Show, Eq, Generic, Typeable)
490−instance ToJSON Source where
491− toJSON NoSource = toJSON False
492− toJSON (SourcePatterns patterns) = toJSON patterns
493− toJSON (SourceIncludeExclude incl excl) = object [ "includes" .= incl, "excludes" .= excl ]
495data PatternOrPatterns = PopPattern Pattern
944data PatternOrPatterns = PopPattern Pattern
496− | PopPatterns [Pattern] deriving (Eq, Read, Show)
945+ | PopPatterns [Pattern] deriving (Eq, Show, Generic, Typeable)
498−instance ToJSON PatternOrPatterns where
947+data Include = Include [Pattern] deriving (Eq, Show, Generic, Typeable)
499− toJSON (PopPattern pattern) = toJSON pattern
948+data Exclude = Exclude [Pattern] deriving (Eq, Show, Generic, Typeable)
500− toJSON (PopPatterns patterns) = toJSON patterns
502−data Include = Include [Pattern] deriving (Eq, Read, Show)
950+newtype Pattern = Pattern Text deriving (Eq, Show, Generic, Typeable)
503−data Exclude = Exclude [Pattern] deriving (Eq, Read, Show)
505−instance ToJSON Include where
952+data Highlights = Highlights { globalsettings :: Maybe HighlightSettings
506− toJSON (Include patterns) = toJSON patterns
953+ , highlightFields :: [FieldHighlight]
954+ } deriving (Show, Eq, Generic, Typeable)
508−instance ToJSON Exclude where
956+data FieldHighlight = FieldHighlight FieldName (Maybe HighlightSettings)
509− toJSON (Exclude patterns) = toJSON patterns
957+ deriving (Show, Eq, Generic, Typeable)
511−newtype Pattern = Pattern Text deriving (Eq, Read, Show)
513−instance ToJSON Pattern where
960+data HighlightSettings = Plain PlainHighlight
514− toJSON (Pattern pattern) = toJSON pattern
961+ | Postings PostingsHighlight
962+ | FastVector FastVectorHighlight
963+ deriving (Show, Eq, Generic, Typeable)
964+data PlainHighlight =
965+ PlainHighlight { plainCommon :: Maybe CommonHighlight
966+ , plainNonPost :: Maybe NonPostings } deriving (Show, Eq, Generic, Typeable)
968+ -- This requires that index_options are set to 'offset' in the mapping.
969+data PostingsHighlight = PostingsHighlight (Maybe CommonHighlight) deriving (Show, Eq, Generic, Typeable)
971+-- This requires that term_vector is set to 'with_positions_offsets' in the mapping.
972+data FastVectorHighlight =
973+ FastVectorHighlight { fvCommon :: Maybe CommonHighlight
974+ , fvNonPostSettings :: Maybe NonPostings
975+ , boundaryChars :: Maybe Text
976+ , boundaryMaxScan :: Maybe Int
977+ , fragmentOffset :: Maybe Int
978+ , matchedFields :: [Text]
979+ , phraseLimit :: Maybe Int
980+ } deriving (Show, Eq, Generic, Typeable)
982+data CommonHighlight =
983+ CommonHighlight { order :: Maybe Text
984+ , forceSource :: Maybe Bool
985+ , tag :: Maybe HighlightTag
986+ , encoder :: Maybe HighlightEncoder
987+ , noMatchSize :: Maybe Int
988+ , highlightQuery :: Maybe Query
989+ , requireFieldMatch :: Maybe Bool
990+ } deriving (Show, Eq, Generic, Typeable)
992+-- Settings that are only applicable to FastVector and Plain highlighters.
994+ NonPostings { fragmentSize :: Maybe Int
995+ , numberOfFragments :: Maybe Int} deriving (Show, Eq, Generic, Typeable)
997+data HighlightEncoder = DefaultEncoder
999+ deriving (Show, Eq, Generic, Typeable)
1001+-- NOTE: Should the tags use some kind of HTML type, rather than Text?
1002+data HighlightTag = TagSchema Text
1003+ | CustomTags ([Text], [Text]) -- Only uses more than the first value in the lists if fvh
1004+ deriving (Show, Eq, Generic, Typeable)
1008+ TermQuery Term (Maybe Boost)
1009+ | TermsQuery Text (NonEmpty Text)
1010+ | QueryMatchQuery MatchQuery
1011+ | QueryMultiMatchQuery MultiMatchQuery
1012+ | QueryBoolQuery BoolQuery
1013+ | QueryBoostingQuery BoostingQuery
1014+ | QueryCommonTermsQuery CommonTermsQuery
1015+ | ConstantScoreFilter Filter Boost
1016+ | ConstantScoreQuery Query Boost
1017+ | QueryDisMaxQuery DisMaxQuery
1018+ | QueryFilteredQuery FilteredQuery
1019+ | QueryFuzzyLikeThisQuery FuzzyLikeThisQuery
1020+ | QueryFuzzyLikeFieldQuery FuzzyLikeFieldQuery
1021+ | QueryFuzzyQuery FuzzyQuery
1022+ | QueryHasChildQuery HasChildQuery
1023+ | QueryHasParentQuery HasParentQuery
1024+ | IdsQuery MappingName [DocId]
1025+ | QueryIndicesQuery IndicesQuery
1026+ | MatchAllQuery (Maybe Boost)
1027+ | QueryMoreLikeThisQuery MoreLikeThisQuery
1028+ | QueryMoreLikeThisFieldQuery MoreLikeThisFieldQuery
1029+ | QueryNestedQuery NestedQuery
1030+ | QueryPrefixQuery PrefixQuery
1031+ | QueryQueryStringQuery QueryStringQuery
1032+ | QuerySimpleQueryStringQuery SimpleQueryStringQuery
1033+ | QueryRangeQuery RangeQuery
1034+ | QueryRegexpQuery RegexpQuery
1035+ deriving (Eq, Show, Generic, Typeable)
1038+ RegexpQuery { regexpQueryField :: FieldName
1039+ , regexpQuery :: Regexp
1040+ , regexpQueryFlags :: RegexpFlags
1041+ , regexpQueryBoost :: Maybe Boost
1042+ } deriving (Eq, Show, Generic, Typeable)
1045+ RangeQuery { rangeQueryField :: FieldName
1046+ , rangeQueryRange :: RangeValue
1047+ , rangeQueryBoost :: Boost } deriving (Eq, Show, Generic, Typeable)
1049+mkRangeQuery :: FieldName -> RangeValue -> RangeQuery
1050+mkRangeQuery f r = RangeQuery f r (Boost 1.0)
1052+data SimpleQueryStringQuery =
1053+ SimpleQueryStringQuery
1054+ { simpleQueryStringQuery :: QueryString
1055+ , simpleQueryStringField :: Maybe FieldOrFields
1056+ , simpleQueryStringOperator :: Maybe BooleanOperator
1057+ , simpleQueryStringAnalyzer :: Maybe Analyzer
1058+ , simpleQueryStringFlags :: Maybe (NonEmpty SimpleQueryFlag)
1059+ , simpleQueryStringLowercaseExpanded :: Maybe LowercaseExpanded
1060+ , simpleQueryStringLocale :: Maybe Locale
1061+ } deriving (Eq, Show, Generic, Typeable)
1063+data SimpleQueryFlag =
1068+ | SimpleQueryPrefix
1069+ | SimpleQueryPhrase
1070+ | SimpleQueryPrecedence
1071+ | SimpleQueryEscape
1072+ | SimpleQueryWhitespace
1075+ | SimpleQuerySlop deriving (Eq, Show, Generic, Typeable)
1077+-- use_dis_max and tie_breaker when fields are plural?
1078+data QueryStringQuery =
1080+ { queryStringQuery :: QueryString
1081+ , queryStringDefaultField :: Maybe FieldName
1082+ , queryStringOperator :: Maybe BooleanOperator
1083+ , queryStringAnalyzer :: Maybe Analyzer
1084+ , queryStringAllowLeadingWildcard :: Maybe AllowLeadingWildcard
1085+ , queryStringLowercaseExpanded :: Maybe LowercaseExpanded
1086+ , queryStringEnablePositionIncrements :: Maybe EnablePositionIncrements
1087+ , queryStringFuzzyMaxExpansions :: Maybe MaxExpansions
1088+ , queryStringFuzziness :: Maybe Fuzziness
1089+ , queryStringFuzzyPrefixLength :: Maybe PrefixLength
1090+ , queryStringPhraseSlop :: Maybe PhraseSlop
1091+ , queryStringBoost :: Maybe Boost
1092+ , queryStringAnalyzeWildcard :: Maybe AnalyzeWildcard
1093+ , queryStringGeneratePhraseQueries :: Maybe GeneratePhraseQueries
1094+ , queryStringMinimumShouldMatch :: Maybe MinimumMatch
1095+ , queryStringLenient :: Maybe Lenient
1096+ , queryStringLocale :: Maybe Locale
1097+ } deriving (Eq, Show, Generic, Typeable)
1099+mkQueryStringQuery :: QueryString -> QueryStringQuery
1100+mkQueryStringQuery qs =
1101+ QueryStringQuery qs Nothing Nothing
1102+ Nothing Nothing Nothing Nothing
1103+ Nothing Nothing Nothing Nothing
1104+ Nothing Nothing Nothing Nothing
1107+data FieldOrFields = FofField FieldName
1108+ | FofFields (NonEmpty FieldName) deriving (Eq, Show, Generic, Typeable)
1112+ { prefixQueryField :: FieldName
1113+ , prefixQueryPrefixValue :: Text
1114+ , prefixQueryBoost :: Maybe Boost } deriving (Eq, Show, Generic, Typeable)
1118+ { nestedQueryPath :: QueryPath
1119+ , nestedQueryScoreType :: ScoreType
1120+ , nestedQuery :: Query } deriving (Eq, Show, Generic, Typeable)
1122+data MoreLikeThisFieldQuery =
1123+ MoreLikeThisFieldQuery
1124+ { moreLikeThisFieldText :: Text
1125+ , moreLikeThisFieldFields :: FieldName
1126+ -- default 0.3 (30%)
1127+ , moreLikeThisFieldPercentMatch :: Maybe PercentMatch
1128+ , moreLikeThisFieldMinimumTermFreq :: Maybe MinimumTermFrequency
1129+ , moreLikeThisFieldMaxQueryTerms :: Maybe MaxQueryTerms
1130+ , moreLikeThisFieldStopWords :: Maybe (NonEmpty StopWord)
1131+ , moreLikeThisFieldMinDocFrequency :: Maybe MinDocFrequency
1132+ , moreLikeThisFieldMaxDocFrequency :: Maybe MaxDocFrequency
1133+ , moreLikeThisFieldMinWordLength :: Maybe MinWordLength
1134+ , moreLikeThisFieldMaxWordLength :: Maybe MaxWordLength
1135+ , moreLikeThisFieldBoostTerms :: Maybe BoostTerms
1136+ , moreLikeThisFieldBoost :: Maybe Boost
1137+ , moreLikeThisFieldAnalyzer :: Maybe Analyzer
1138+ } deriving (Eq, Show, Generic, Typeable)
1140+data MoreLikeThisQuery =
1142+ { moreLikeThisText :: Text
1143+ , moreLikeThisFields :: Maybe (NonEmpty FieldName)
1144+ -- default 0.3 (30%)
1145+ , moreLikeThisPercentMatch :: Maybe PercentMatch
1146+ , moreLikeThisMinimumTermFreq :: Maybe MinimumTermFrequency
1147+ , moreLikeThisMaxQueryTerms :: Maybe MaxQueryTerms
1148+ , moreLikeThisStopWords :: Maybe (NonEmpty StopWord)
1149+ , moreLikeThisMinDocFrequency :: Maybe MinDocFrequency
1150+ , moreLikeThisMaxDocFrequency :: Maybe MaxDocFrequency
1151+ , moreLikeThisMinWordLength :: Maybe MinWordLength
1152+ , moreLikeThisMaxWordLength :: Maybe MaxWordLength
1153+ , moreLikeThisBoostTerms :: Maybe BoostTerms
1154+ , moreLikeThisBoost :: Maybe Boost
1155+ , moreLikeThisAnalyzer :: Maybe Analyzer
1156+ } deriving (Eq, Show, Generic, Typeable)
1160+ { indicesQueryIndices :: [IndexName]
1161+ , indicesQuery :: Query
1163+ , indicesQueryNoMatch :: Maybe Query } deriving (Eq, Show, Generic, Typeable)
1165+data HasParentQuery =
1167+ { hasParentQueryType :: TypeName
1168+ , hasParentQuery :: Query
1169+ , hasParentQueryScoreType :: Maybe ScoreType } deriving (Eq, Show, Generic, Typeable)
1171+data HasChildQuery =
1173+ { hasChildQueryType :: TypeName
1174+ , hasChildQuery :: Query
1175+ , hasChildQueryScoreType :: Maybe ScoreType } deriving (Eq, Show, Generic, Typeable)
1181+ | ScoreTypeNone deriving (Eq, Show, Generic, Typeable)
1184+ FuzzyQuery { fuzzyQueryField :: FieldName
1185+ , fuzzyQueryValue :: Text
1186+ , fuzzyQueryPrefixLength :: PrefixLength
1187+ , fuzzyQueryMaxExpansions :: MaxExpansions
1188+ , fuzzyQueryFuzziness :: Fuzziness
1189+ , fuzzyQueryBoost :: Maybe Boost
1190+ } deriving (Eq, Show, Generic, Typeable)
1192+data FuzzyLikeFieldQuery =
1193+ FuzzyLikeFieldQuery
1194+ { fuzzyLikeField :: FieldName
1195+ -- anaphora is good for the soul.
1196+ , fuzzyLikeFieldText :: Text
1197+ , fuzzyLikeFieldMaxQueryTerms :: MaxQueryTerms
1198+ , fuzzyLikeFieldIgnoreTermFrequency :: IgnoreTermFrequency
1199+ , fuzzyLikeFieldFuzziness :: Fuzziness
1200+ , fuzzyLikeFieldPrefixLength :: PrefixLength
1201+ , fuzzyLikeFieldBoost :: Boost
1202+ , fuzzyLikeFieldAnalyzer :: Maybe Analyzer
1203+ } deriving (Eq, Show, Generic, Typeable)
1205+data FuzzyLikeThisQuery =
1207+ { fuzzyLikeFields :: [FieldName]
1208+ , fuzzyLikeText :: Text
1209+ , fuzzyLikeMaxQueryTerms :: MaxQueryTerms
1210+ , fuzzyLikeIgnoreTermFrequency :: IgnoreTermFrequency
1211+ , fuzzyLikeFuzziness :: Fuzziness
1212+ , fuzzyLikePrefixLength :: PrefixLength
1213+ , fuzzyLikeBoost :: Boost
1214+ , fuzzyLikeAnalyzer :: Maybe Analyzer
1215+ } deriving (Eq, Show, Generic, Typeable)
1217+data FilteredQuery =
1219+ { filteredQuery :: Query
1220+ , filteredFilter :: Filter } deriving (Eq, Show, Generic, Typeable)
1223+ DisMaxQuery { disMaxQueries :: [Query]
1225+ , disMaxTiebreaker :: Tiebreaker
1226+ , disMaxBoost :: Maybe Boost
1227+ } deriving (Eq, Show, Generic, Typeable)
1230+ MatchQuery { matchQueryField :: FieldName
1231+ , matchQueryQueryString :: QueryString
1232+ , matchQueryOperator :: BooleanOperator
1233+ , matchQueryZeroTerms :: ZeroTermsQuery
1234+ , matchQueryCutoffFrequency :: Maybe CutoffFrequency
1235+ , matchQueryMatchType :: Maybe MatchQueryType
1236+ , matchQueryAnalyzer :: Maybe Analyzer
1237+ , matchQueryMaxExpansions :: Maybe MaxExpansions
1238+ , matchQueryLenient :: Maybe Lenient
1239+ , matchQueryBoost :: Maybe Boost } deriving (Eq, Show, Generic, Typeable)
1241+{-| 'mkMatchQuery' is a convenience function that defaults the less common parameters,
1242+ enabling you to provide only the 'FieldName' and 'QueryString' to make a 'MatchQuery'
1244+mkMatchQuery :: FieldName -> QueryString -> MatchQuery
1245+mkMatchQuery field query = MatchQuery field query Or ZeroTermsNone Nothing Nothing Nothing Nothing Nothing Nothing
1247+data MatchQueryType =
1249+ | MatchPhrasePrefix deriving (Eq, Show, Generic, Typeable)
1251+data MultiMatchQuery =
1252+ MultiMatchQuery { multiMatchQueryFields :: [FieldName]
1253+ , multiMatchQueryString :: QueryString
1254+ , multiMatchQueryOperator :: BooleanOperator
1255+ , multiMatchQueryZeroTerms :: ZeroTermsQuery
1256+ , multiMatchQueryTiebreaker :: Maybe Tiebreaker
1257+ , multiMatchQueryType :: Maybe MultiMatchQueryType
1258+ , multiMatchQueryCutoffFrequency :: Maybe CutoffFrequency
1259+ , multiMatchQueryAnalyzer :: Maybe Analyzer
1260+ , multiMatchQueryMaxExpansions :: Maybe MaxExpansions
1261+ , multiMatchQueryLenient :: Maybe Lenient } deriving (Eq, Show, Generic, Typeable)
1263+{-| 'mkMultiMatchQuery' is a convenience function that defaults the less common parameters,
1264+ enabling you to provide only the list of 'FieldName's and 'QueryString' to
1265+ make a 'MultiMatchQuery'.
1268+mkMultiMatchQuery :: [FieldName] -> QueryString -> MultiMatchQuery
1269+mkMultiMatchQuery matchFields query =
1270+ MultiMatchQuery matchFields query
1271+ Or ZeroTermsNone Nothing Nothing Nothing Nothing Nothing Nothing
1273+data MultiMatchQueryType =
1274+ MultiMatchBestFields
1275+ | MultiMatchMostFields
1276+ | MultiMatchCrossFields
1278+ | MultiMatchPhrasePrefix deriving (Eq, Show, Generic, Typeable)
1281+ BoolQuery { boolQueryMustMatch :: [Query]
1282+ , boolQueryMustNotMatch :: [Query]
1283+ , boolQueryShouldMatch :: [Query]
1284+ , boolQueryMinimumShouldMatch :: Maybe MinimumMatch
1285+ , boolQueryBoost :: Maybe Boost
1286+ , boolQueryDisableCoord :: Maybe DisableCoord
1287+ } deriving (Eq, Show, Generic, Typeable)
1289+mkBoolQuery :: [Query] -> [Query] -> [Query] -> BoolQuery
1290+mkBoolQuery must mustNot should =
1291+ BoolQuery must mustNot should Nothing Nothing Nothing
1293+data BoostingQuery =
1294+ BoostingQuery { positiveQuery :: Query
1295+ , negativeQuery :: Query
1296+ , negativeBoost :: Boost } deriving (Eq, Show, Generic, Typeable)
1298+data CommonTermsQuery =
1299+ CommonTermsQuery { commonField :: FieldName
1300+ , commonQuery :: QueryString
1301+ , commonCutoffFrequency :: CutoffFrequency
1302+ , commonLowFreqOperator :: BooleanOperator
1303+ , commonHighFreqOperator :: BooleanOperator
1304+ , commonMinimumShouldMatch :: Maybe CommonMinimumMatch
1305+ , commonBoost :: Maybe Boost
1306+ , commonAnalyzer :: Maybe Analyzer
1307+ , commonDisableCoord :: Maybe DisableCoord
1308+ } deriving (Eq, Show, Generic, Typeable)
1310+data CommonMinimumMatch =
1311+ CommonMinimumMatchHighLow MinimumMatchHighLow
1312+ | CommonMinimumMatch MinimumMatch
1313+ deriving (Eq, Show, Generic, Typeable)
1315+data MinimumMatchHighLow =
1316+ MinimumMatchHighLow { lowFreq :: MinimumMatch
1317+ , highFreq :: MinimumMatch } deriving (Eq, Show, Generic, Typeable)
1319+data Filter = AndFilter [Filter] Cache
1320+ | OrFilter [Filter] Cache
1321+ | NotFilter Filter Cache
1323+ | BoolFilter BoolMatch
1324+ | ExistsFilter FieldName -- always cached
1325+ | GeoBoundingBoxFilter GeoBoundingBoxConstraint
1326+ | GeoDistanceFilter GeoPoint Distance DistanceType OptimizeBbox Cache
1327+ | GeoDistanceRangeFilter GeoPoint DistanceRange
1328+ | GeoPolygonFilter FieldName [LatLon]
1329+ | IdsFilter MappingName [DocId]
1331+ | MissingFilter FieldName Existence NullValue
1332+ | PrefixFilter FieldName PrefixValue Cache
1333+ | QueryFilter Query Cache
1334+ | RangeFilter FieldName RangeValue RangeExecution Cache
1335+ | RegexpFilter FieldName Regexp RegexpFlags CacheName Cache CacheKey
1336+ | TermFilter Term Cache
1337+ deriving (Eq, Show, Generic, Typeable)
1339+data ZeroTermsQuery = ZeroTermsNone
1340+ | ZeroTermsAll deriving (Eq, Show, Generic, Typeable)
1342+data RangeExecution = RangeExecutionIndex
1343+ | RangeExecutionFielddata deriving (Eq, Show, Generic, Typeable)
1345+newtype Regexp = Regexp Text deriving (Eq, Show, Generic, Typeable, FromJSON)
1347+data RegexpFlags = AllRegexpFlags
1349+ | SomeRegexpFlags (NonEmpty RegexpFlag) deriving (Eq, Show, Generic, Typeable)
1351+data RegexpFlag = AnyString
1356+ | Interval deriving (Eq, Show, Generic, Typeable)
1358+newtype LessThan = LessThan Double deriving (Eq, Show, Generic, Typeable)
1359+newtype LessThanEq = LessThanEq Double deriving (Eq, Show, Generic, Typeable)
1360+newtype GreaterThan = GreaterThan Double deriving (Eq, Show, Generic, Typeable)
1361+newtype GreaterThanEq = GreaterThanEq Double deriving (Eq, Show, Generic, Typeable)
1363+newtype LessThanD = LessThanD UTCTime deriving (Eq, Show, Generic, Typeable)
1364+newtype LessThanEqD = LessThanEqD UTCTime deriving (Eq, Show, Generic, Typeable)
1365+newtype GreaterThanD = GreaterThanD UTCTime deriving (Eq, Show, Generic, Typeable)
1366+newtype GreaterThanEqD = GreaterThanEqD UTCTime deriving (Eq, Show, Generic, Typeable)
1368+data RangeValue = RangeDateLte LessThanEqD
1369+ | RangeDateLt LessThanD
1370+ | RangeDateGte GreaterThanEqD
1371+ | RangeDateGt GreaterThanD
1372+ | RangeDateGtLt GreaterThanD LessThanD
1373+ | RangeDateGteLte GreaterThanEqD LessThanEqD
1374+ | RangeDateGteLt GreaterThanEqD LessThanD
1375+ | RangeDateGtLte GreaterThanD LessThanEqD
1376+ | RangeDoubleLte LessThanEq
1377+ | RangeDoubleLt LessThan
1378+ | RangeDoubleGte GreaterThanEq
1379+ | RangeDoubleGt GreaterThan
1380+ | RangeDoubleGtLt GreaterThan LessThan
1381+ | RangeDoubleGteLte GreaterThanEq LessThanEq
1382+ | RangeDoubleGteLt GreaterThanEq LessThan
1383+ | RangeDoubleGtLte GreaterThan LessThanEq
1384+ deriving (Eq, Show, Generic, Typeable)
1386+rangeValueToPair :: RangeValue -> [Pair]
1387+rangeValueToPair rv = case rv of
1388+ RangeDateLte (LessThanEqD t) -> ["lte" .= t]
1389+ RangeDateGte (GreaterThanEqD t) -> ["gte" .= t]
1390+ RangeDateLt (LessThanD t) -> ["lt" .= t]
1391+ RangeDateGt (GreaterThanD t) -> ["gt" .= t]
1392+ RangeDateGteLte (GreaterThanEqD l) (LessThanEqD g) -> ["gte" .= l, "lte" .= g]
1393+ RangeDateGtLte (GreaterThanD l) (LessThanEqD g) -> ["gt" .= l, "lte" .= g]
1394+ RangeDateGteLt (GreaterThanEqD l) (LessThanD g) -> ["gte" .= l, "lt" .= g]
1395+ RangeDateGtLt (GreaterThanD l) (LessThanD g) -> ["gt" .= l, "lt" .= g]
1396+ RangeDoubleLte (LessThanEq t) -> ["lte" .= t]
1397+ RangeDoubleGte (GreaterThanEq t) -> ["gte" .= t]
1398+ RangeDoubleLt (LessThan t) -> ["lt" .= t]
1399+ RangeDoubleGt (GreaterThan t) -> ["gt" .= t]
1400+ RangeDoubleGteLte (GreaterThanEq l) (LessThanEq g) -> ["gte" .= l, "lte" .= g]
1401+ RangeDoubleGtLte (GreaterThan l) (LessThanEq g) -> ["gt" .= l, "lte" .= g]
1402+ RangeDoubleGteLt (GreaterThanEq l) (LessThan g) -> ["gte" .= l, "lt" .= g]
1403+ RangeDoubleGtLt (GreaterThan l) (LessThan g) -> ["gt" .= l, "lt" .= g]
1405+data Term = Term { termField :: Text
1406+ , termValue :: Text } deriving (Eq, Show, Generic, Typeable)
1408+data BoolMatch = MustMatch Term Cache
1409+ | MustNotMatch Term Cache
1410+ | ShouldMatch [Term] Cache deriving (Eq, Show, Generic, Typeable)
1412+-- "memory" or "indexed"
1413+data GeoFilterType = GeoFilterMemory
1414+ | GeoFilterIndexed deriving (Eq, Show, Generic, Typeable)
1416+data LatLon = LatLon { lat :: Double
1417+ , lon :: Double } deriving (Eq, Show, Generic, Typeable)
1419+data GeoBoundingBox =
1420+ GeoBoundingBox { topLeft :: LatLon
1421+ , bottomRight :: LatLon } deriving (Eq, Show, Generic, Typeable)
1423+data GeoBoundingBoxConstraint =
1424+ GeoBoundingBoxConstraint { geoBBField :: FieldName
1425+ , constraintBox :: GeoBoundingBox
1426+ , bbConstraintcache :: Cache
1427+ , geoType :: GeoFilterType
1428+ } deriving (Eq, Show, Generic, Typeable)
1431+ GeoPoint { geoField :: FieldName
1432+ , latLon :: LatLon} deriving (Eq, Show, Generic, Typeable)
1434+data DistanceUnit = Miles
1442+ | NauticalMiles deriving (Eq, Show, Generic, Typeable)
1444+data DistanceType = Arc
1445+ | SloppyArc -- doesn't exist <1.0
1446+ | Plane deriving (Eq, Show, Generic, Typeable)
1448+data OptimizeBbox = OptimizeGeoFilterType GeoFilterType
1449+ | NoOptimizeBbox deriving (Eq, Show, Generic, Typeable)
1452+ Distance { coefficient :: Double
1453+ , unit :: DistanceUnit } deriving (Eq, Show, Generic, Typeable)
1455+data DistanceRange =
1456+ DistanceRange { distanceFrom :: Distance
1457+ , distanceTo :: Distance } deriving (Eq, Show, Generic, Typeable)
516data SearchResult a =
1459data SearchResult a =
517 SearchResult { took :: Int
1460 SearchResult { took :: Int
@@ -519,24 +1462,2085 @@ data SearchResult a =
519 , shards :: ShardResult
1462 , shards :: ShardResult
520 , searchHits :: SearchHits a
1463 , searchHits :: SearchHits a
521 , aggregations :: Maybe AggregationResults
1464 , aggregations :: Maybe AggregationResults
522− , scrollId :: Maybe ScrollId
1465+ , scrollId :: Maybe ScrollId } deriving (Eq, Show, Generic, Typeable)
523− -- ^ Only one Suggestion request / response per
524− -- Search is supported.
525− , suggest :: Maybe NamedSuggestionResponse
529−instance (FromJSON a) => FromJSON (SearchResult a) where
1467+newtype ScrollId = ScrollId Text deriving (Eq, Show, Generic, Ord, ToJSON, FromJSON)
530− parseJSON (Object v) = SearchResult <$>
1468+
531− v .: "took" <*>
1469+type Score = Maybe Double
532− v .: "timed_out" <*>
1470+
533− v .: "_shards" <*>
1471+data SearchHits a =
534− v .: "hits" <*>
1472+ SearchHits { hitsTotal :: Int
535− v .:? "aggregations" <*>
1473+ , maxScore :: Score
536− v .:? "_scroll_id" <*>
1474+ , hits :: [Hit a] } deriving (Eq, Show, Generic, Typeable)
537− v .:? "suggest"
1475+
538− parseJSON _ = empty
1476+
1477+instance Monoid (SearchHits a) where
1478+ mempty = SearchHits 0 Nothing mempty
1479+ mappend (SearchHits ta ma ha) (SearchHits tb mb hb) =
1480+ SearchHits (ta + tb) (max ma mb) (ha <> hb)
1484+ Hit { hitIndex :: IndexName
1485+ , hitType :: MappingName
1486+ , hitDocId :: DocId
1487+ , hitScore :: Score
1488+ , hitSource :: Maybe a
1489+ , hitHighlight :: Maybe HitHighlight } deriving (Eq, Show, Generic, Typeable)
1492+ ShardResult { shardTotal :: Int
1493+ , shardsSuccessful :: Int
1494+ , shardsFailed :: Int } deriving (Eq, Show, Generic, Typeable)
1496+type HitHighlight = M.Map Text [Text]
1498+showText :: Show a => a -> Text
1499+showText = T.pack . show
1501+readMay :: Read a => String -> Maybe a
1502+readMay s = case reads s of
1503+ (a, ""):_ -> Just a
1506+parseReadText :: Read a => Text -> Parser a
1507+parseReadText = maybe mzero return . readMay . T.unpack
1509+type Aggregations = M.Map Text Aggregation
1511+emptyAggregations :: Aggregations
1512+emptyAggregations = M.empty
1514+mkAggregations :: Text -> Aggregation -> Aggregations
1515+mkAggregations name aggregation = M.insert name aggregation emptyAggregations
1517+data TermOrder = TermOrder{ termSortField :: Text
1518+ , termSortOrder :: SortOrder } deriving (Eq, Show, Generic, Typeable)
1520+data TermInclusion = TermInclusion Text
1521+ | TermPattern Text Text deriving (Eq, Show, Generic, Typeable)
1523+data CollectionMode = BreadthFirst
1524+ | DepthFirst deriving (Eq, Show, Generic, Typeable)
1526+data ExecutionHint = Ordinals
1528+ | GlobalOrdinalsHash
1529+ | GlobalOrdinalsLowCardinality
1530+ | Map deriving (Eq, Show, Generic, Typeable)
1532+data TimeInterval = Weeks
1536+ | Seconds deriving (Eq)
1538+data Interval = Year
1546+ | FractionalInterval Float TimeInterval deriving (Eq, Show, Generic, Typeable)
1548+data Aggregation = TermsAgg TermsAggregation
1549+ | DateHistogramAgg DateHistogramAggregation
1550+ | ValueCountAgg ValueCountAggregation
1551+ | FilterAgg FilterAggregation
1552+ | DateRangeAgg DateRangeAggregation deriving (Eq, Show, Generic, Typeable)
1555+data TermsAggregation = TermsAggregation { term :: Either Text Text
1556+ , termInclude :: Maybe TermInclusion
1557+ , termExclude :: Maybe TermInclusion
1558+ , termOrder :: Maybe TermOrder
1559+ , termMinDocCount :: Maybe Int
1560+ , termSize :: Maybe Int
1561+ , termShardSize :: Maybe Int
1562+ , termCollectMode :: Maybe CollectionMode
1563+ , termExecutionHint :: Maybe ExecutionHint
1564+ , termAggs :: Maybe Aggregations
1565+ } deriving (Eq, Show, Generic, Typeable)
1567+data DateHistogramAggregation = DateHistogramAggregation { dateField :: FieldName
1568+ , dateInterval :: Interval
1569+ , dateFormat :: Maybe Text
1570+ -- pre and post deprecated in 1.5
1571+ , datePreZone :: Maybe Text
1572+ , datePostZone :: Maybe Text
1573+ , datePreOffset :: Maybe Text
1574+ , datePostOffset :: Maybe Text
1575+ , dateAggs :: Maybe Aggregations
1576+ } deriving (Eq, Show, Generic, Typeable)
1579+data DateRangeAggregation = DateRangeAggregation { draField :: FieldName
1580+ , draFormat :: Maybe Text
1581+ , draRanges :: NonEmpty DateRangeAggRange
1582+ } deriving (Eq, Show, Generic, Typeable)
1584+data DateRangeAggRange = DateRangeFrom DateMathExpr
1585+ | DateRangeTo DateMathExpr
1586+ | DateRangeFromAndTo DateMathExpr DateMathExpr deriving (Eq, Show, Generic, Typeable)
1588+-- | See <https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#date-math> for more information.
1589+data DateMathExpr = DateMathExpr DateMathAnchor [DateMathModifier] deriving (Eq, Show, Generic, Typeable)
1592+-- | Starting point for a date range. This along with the 'DateMathModifiers' gets you the date ES will start from.
1593+data DateMathAnchor = DMNow
1594+ | DMDate Day deriving (Eq, Show, Generic, Typeable)
1596+data DateMathModifier = AddTime Int DateMathUnit
1597+ | SubtractTime Int DateMathUnit
1598+ | RoundDownTo DateMathUnit deriving (Eq, Show, Generic, Typeable)
1600+data DateMathUnit = DMYear
1606+ | DMSecond deriving (Eq, Show, Generic, Typeable)
1608+-- | See <https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-valuecount-aggregation.html> for more information.
1609+data ValueCountAggregation = FieldValueCount FieldName
1610+ | ScriptValueCount Script deriving (Eq, Show, Generic, Typeable)
1612+-- | 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.
1613+data FilterAggregation = FilterAggregation { faFilter :: Filter
1614+ , faAggs :: Maybe Aggregations} deriving (Eq, Show, Generic, Typeable)
1616+mkTermsAggregation :: Text -> TermsAggregation
1617+mkTermsAggregation t = TermsAggregation (Left t) Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing
1619+mkTermsScriptAggregation :: Text -> TermsAggregation
1620+mkTermsScriptAggregation t = TermsAggregation (Right t) Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing
1622+mkDateHistogram :: FieldName -> Interval -> DateHistogramAggregation
1623+mkDateHistogram t i = DateHistogramAggregation t i Nothing Nothing Nothing Nothing Nothing Nothing
1625+instance ToJSON Version where
1626+ toJSON Version {..} = object ["number" .= number
1627+ ,"build_hash" .= build_hash
1628+ ,"build_timestamp" .= build_timestamp
1629+ ,"build_snapshot" .= build_snapshot
1630+ ,"lucene_version" .= lucene_version]
1632+instance FromJSON Version where
1633+ parseJSON = withObject "Version" parse
1634+ where parse o = Version
1636+ <*> o .: "build_hash"
1637+ <*> o .: "build_timestamp"
1638+ <*> o .: "build_snapshot"
1639+ <*> o .: "lucene_version"
1641+instance ToJSON TermOrder where
1642+ toJSON (TermOrder termSortField termSortOrder) = object [termSortField .= termSortOrder]
1644+instance ToJSON TermInclusion where
1645+ toJSON (TermInclusion x) = toJSON x
1646+ toJSON (TermPattern pattern flags) = omitNulls [ "pattern" .= pattern,
1649+instance ToJSON CollectionMode where
1650+ toJSON BreadthFirst = "breadth_first"
1651+ toJSON DepthFirst = "depth_first"
1653+instance ToJSON ExecutionHint where
1654+ toJSON Ordinals = "ordinals"
1655+ toJSON GlobalOrdinals = "global_ordinals"
1656+ toJSON GlobalOrdinalsHash = "global_ordinals_hash"
1657+ toJSON GlobalOrdinalsLowCardinality = "global_ordinals_low_cardinality"
1660+instance ToJSON Interval where
1661+ toJSON Year = "year"
1662+ toJSON Quarter = "quarter"
1663+ toJSON Month = "month"
1664+ toJSON Week = "week"
1666+ toJSON Hour = "hour"
1667+ toJSON Minute = "minute"
1668+ toJSON Second = "second"
1669+ toJSON (FractionalInterval fraction interval) = toJSON $ show fraction ++ show interval
1671+instance Show TimeInterval where
1678+instance ToJSON Aggregation where
1679+ toJSON (TermsAgg (TermsAggregation term include exclude order minDocCount size shardSize collectMode executionHint termAggs)) =
1680+ omitNulls ["terms" .= omitNulls [ toJSON' term,
1681+ "include" .= include,
1682+ "exclude" .= exclude,
1684+ "min_doc_count" .= minDocCount,
1686+ "shard_size" .= shardSize,
1687+ "collect_mode" .= collectMode,
1688+ "execution_hint" .= executionHint
1690+ "aggs" .= termAggs ]
1692+ toJSON' x = case x of { Left y -> "field" .= y; Right y -> "script" .= y }
1694+ toJSON (DateHistogramAgg (DateHistogramAggregation field interval format preZone postZone preOffset postOffset dateHistoAggs)) =
1695+ omitNulls ["date_histogram" .= omitNulls [ "field" .= field,
1696+ "interval" .= interval,
1697+ "format" .= format,
1698+ "pre_zone" .= preZone,
1699+ "post_zone" .= postZone,
1700+ "pre_offset" .= preOffset,
1701+ "post_offset" .= postOffset
1703+ "aggs" .= dateHistoAggs ]
1704+ toJSON (ValueCountAgg a) = object ["value_count" .= v]
1705+ where v = case a of
1706+ (FieldValueCount (FieldName n)) -> object ["field" .= n]
1707+ (ScriptValueCount (Script s)) -> object ["script" .= s]
1708+ toJSON (FilterAgg (FilterAggregation filt ags)) =
1709+ omitNulls [ "filter" .= filt
1711+ toJSON (DateRangeAgg a) = object [ "date_range" .= a
1714+instance ToJSON DateRangeAggregation where
1715+ toJSON DateRangeAggregation {..} =
1716+ omitNulls [ "field" .= draField
1717+ , "format" .= draFormat
1718+ , "ranges" .= toList draRanges
1721+instance ToJSON DateRangeAggRange where
1722+ toJSON (DateRangeFrom e) = object [ "from" .= e ]
1723+ toJSON (DateRangeTo e) = object [ "to" .= e ]
1724+ toJSON (DateRangeFromAndTo f t) = object [ "from" .= f, "to" .= t ]
1726+instance ToJSON DateMathExpr where
1727+ toJSON (DateMathExpr a mods) = String (fmtA a <> mconcat (fmtMod <$> mods))
1728+ where fmtA DMNow = "now"
1729+ fmtA (DMDate date) = case toGregorian date of
1730+ (y,m,d) -> showText y <> "-" <>
1731+ showText m <> "-" <>
1733+ fmtMod (AddTime n u) = "+" <> showText n <> fmtU u
1734+ fmtMod (SubtractTime n u) = "-" <> showText n <> fmtU u
1735+ fmtMod (RoundDownTo u) = "/" <> fmtU u
1741+ fmtU DMMinute = "m"
1742+ fmtU DMSecond = "s"
1745+type AggregationResults = M.Map Text Value
1747+class BucketAggregation a where
1749+ docCount :: a -> Int
1750+ aggs :: a -> Maybe AggregationResults
1753+data Bucket a = Bucket { buckets :: [a]} deriving (Show)
1755+data TermsResult = TermsResult { termKey :: Text
1756+ , termsDocCount :: Int
1757+ , termsAggs :: Maybe AggregationResults } deriving (Show)
1759+data DateHistogramResult = DateHistogramResult { dateKey :: Int
1760+ , dateKeyStr :: Maybe Text
1761+ , dateDocCount :: Int
1762+ , dateHistogramAggs :: Maybe AggregationResults } deriving (Show)
1764+data DateRangeResult = DateRangeResult { dateRangeKey :: Text
1765+ , dateRangeFrom :: Maybe UTCTime
1766+ , dateRangeFromAsString :: Maybe Text
1767+ , dateRangeTo :: Maybe UTCTime
1768+ , dateRangeToAsString :: Maybe Text
1769+ , dateRangeDocCount :: Int
1770+ , dateRangeAggs :: Maybe AggregationResults } deriving (Show, Eq, Generic, Typeable)
1772+toTerms :: Text -> AggregationResults -> Maybe (Bucket TermsResult)
1773+toTerms t a = M.lookup t a >>= deserialize
1774+ where deserialize = parseMaybe parseJSON
1776+toDateHistogram :: Text -> AggregationResults -> Maybe (Bucket DateHistogramResult)
1777+toDateHistogram t a = M.lookup t a >>= deserialize
1778+ where deserialize = parseMaybe parseJSON
1780+instance BucketAggregation TermsResult where
1782+ docCount = termsDocCount
1785+instance BucketAggregation DateHistogramResult where
1786+ key = showText . dateKey
1787+ docCount = dateDocCount
1788+ aggs = dateHistogramAggs
1790+instance BucketAggregation DateRangeResult where
1792+ docCount = dateRangeDocCount
1793+ aggs = dateRangeAggs
1795+instance (FromJSON a, BucketAggregation a) => FromJSON (Bucket a) where
1796+ parseJSON (Object v) = Bucket <$>
1798+ parseJSON _ = mempty
1800+instance FromJSON TermsResult where
1801+ parseJSON (Object v) = TermsResult <$>
1803+ v .: "doc_count" <*>
1804+ v .:? "aggregations"
1805+ parseJSON _ = mempty
1807+instance FromJSON DateHistogramResult where
1808+ parseJSON (Object v) = DateHistogramResult <$>
1810+ v .:? "key_as_string" <*>
1811+ v .: "doc_count" <*>
1812+ v .:? "aggregations"
1813+ parseJSON _ = mempty
1815+instance FromJSON DateRangeResult where
1816+ parseJSON = withObject "DateRangeResult" parse
1817+ where parse v = DateRangeResult <$>
1819+ (fmap posixMS <$> v .:? "from") <*>
1820+ v .:? "from_as_string" <*>
1821+ (fmap posixMS <$> v .:? "to") <*>
1822+ v .:? "to_as_string" <*>
1823+ v .: "doc_count" <*>
1824+ v .:? "aggregations"
1826+instance FromJSON POSIXMS where
1827+ parseJSON = withScientific "POSIXMS" (return . parse)
1828+ where parse n = let n' = truncate n :: Integer
1829+ in POSIXMS (posixSecondsToUTCTime (fromInteger (n' `div` 1000)))
1831+instance Monoid Filter where
1832+ mempty = IdentityFilter
1833+ mappend a b = AndFilter [a, b] defaultCache
1835+instance Seminearring Filter where
1836+ a <||> b = OrFilter [a, b] defaultCache
1838+instance ToJSON Filter where
1839+ toJSON (AndFilter filters cache) =
1841+ object [ "filters" .= fmap toJSON filters
1842+ , "_cache" .= cache]]
1844+ toJSON (OrFilter filters cache) =
1846+ object [ "filters" .= fmap toJSON filters
1847+ , "_cache" .= cache]]
1849+ toJSON (NotFilter notFilter cache) =
1851+ object ["filter" .= notFilter
1852+ , "_cache" .= cache]]
1854+ toJSON (IdentityFilter) =
1855+ object ["match_all" .= object []]
1857+ toJSON (TermFilter (Term termFilterField termFilterValue) cache) =
1858+ object ["term" .= object base]
1859+ where base = [termFilterField .= termFilterValue,
1862+ toJSON (ExistsFilter (FieldName fieldName)) =
1863+ object ["exists" .= object
1864+ ["field" .= fieldName]]
1866+ toJSON (BoolFilter boolMatch) =
1867+ object ["bool" .= boolMatch]
1869+ toJSON (GeoBoundingBoxFilter bbConstraint) =
1870+ object ["geo_bounding_box" .= bbConstraint]
1872+ toJSON (GeoDistanceFilter (GeoPoint (FieldName distanceGeoField) geoDistLatLon)
1873+ distance distanceType optimizeBbox cache) =
1874+ object ["geo_distance" .=
1875+ object ["distance" .= distance
1876+ , "distance_type" .= distanceType
1877+ , "optimize_bbox" .= optimizeBbox
1878+ , distanceGeoField .= geoDistLatLon
1879+ , "_cache" .= cache]]
1881+ toJSON (GeoDistanceRangeFilter (GeoPoint (FieldName gddrField) drLatLon)
1882+ (DistanceRange geoDistRangeDistFrom drDistanceTo)) =
1883+ object ["geo_distance_range" .=
1884+ object ["from" .= geoDistRangeDistFrom
1885+ , "to" .= drDistanceTo
1886+ , gddrField .= drLatLon]]
1888+ toJSON (GeoPolygonFilter (FieldName geoPolygonFilterField) latLons) =
1889+ object ["geo_polygon" .=
1890+ object [geoPolygonFilterField .=
1891+ object ["points" .= fmap toJSON latLons]]]
1893+ toJSON (IdsFilter (MappingName mappingName) values) =
1895+ object ["type" .= mappingName
1896+ , "values" .= fmap unpackId values]]
1898+ toJSON (LimitFilter limit) =
1899+ object ["limit" .= object ["value" .= limit]]
1901+ toJSON (MissingFilter (FieldName fieldName) (Existence existence) (NullValue nullValue)) =
1902+ object ["missing" .=
1903+ object ["field" .= fieldName
1904+ , "existence" .= existence
1905+ , "null_value" .= nullValue]]
1907+ toJSON (PrefixFilter (FieldName fieldName) fieldValue cache) =
1908+ object ["prefix" .=
1909+ object [fieldName .= fieldValue
1910+ , "_cache" .= cache]]
1912+ toJSON (QueryFilter query False) =
1913+ object ["query" .= toJSON query ]
1914+ toJSON (QueryFilter query True) =
1915+ object ["fquery" .=
1916+ object [ "query" .= toJSON query
1917+ , "_cache" .= True ]]
1919+ toJSON (RangeFilter (FieldName fieldName) rangeValue rangeExecution cache) =
1921+ object [ fieldName .= object (rangeValueToPair rangeValue)
1922+ , "execution" .= rangeExecution
1923+ , "_cache" .= cache]]
1925+ toJSON (RegexpFilter (FieldName fieldName)
1926+ (Regexp regexText) flags (CacheName cacheName) cache (CacheKey cacheKey)) =
1927+ object ["regexp" .=
1928+ object [fieldName .=
1929+ object ["value" .= regexText
1930+ , "flags" .= flags]
1931+ , "_name" .= cacheName
1932+ , "_cache" .= cache
1933+ , "_cache_key" .= cacheKey]]
1935+instance FromJSON Filter where
1936+ parseJSON = withObject "Filter" parse
1937+ where parse o = andFilter `taggedWith` "and"
1938+ <|> orFilter `taggedWith` "or"
1939+ <|> notFilter `taggedWith` "not"
1940+ <|> identityFilter `taggedWith` "match_all"
1941+ <|> boolFilter `taggedWith` "bool"
1942+ <|> existsFilter `taggedWith` "exists"
1943+ <|> geoBoundingBoxFilter `taggedWith` "geo_bounding_box"
1944+ <|> geoDistanceFilter `taggedWith` "geo_distance"
1945+ <|> geoDistanceRangeFilter `taggedWith` "geo_distance_range"
1946+ <|> geoPolygonFilter `taggedWith` "geo_polygon"
1947+ <|> idsFilter `taggedWith` "ids"
1948+ <|> limitFilter `taggedWith` "limit"
1949+ <|> missingFilter `taggedWith` "missing"
1950+ <|> prefixFilter `taggedWith` "prefix"
1951+ <|> queryFilter `taggedWith` "query"
1952+ <|> fqueryFilter `taggedWith` "fquery"
1953+ <|> rangeFilter `taggedWith` "range"
1954+ <|> regexpFilter `taggedWith` "regexp"
1955+ <|> termFilter `taggedWith` "term"
1956+ where taggedWith parser k = parser =<< o .: k
1957+ andFilter o = AndFilter <$> o .: "filters"
1958+ <*> o .:? "_cache" .!= defaultCache
1959+ orFilter o = OrFilter <$> o .: "filters"
1960+ <*> o .:? "_cache" .!= defaultCache
1961+ notFilter o = NotFilter <$> o .: "filter"
1962+ <*> o .: "_cache" .!= defaultCache
1963+ identityFilter :: Object -> Parser Filter
1965+ | HM.null m = pure IdentityFilter
1966+ | otherwise = fail ("Identityfilter expected empty object but got " <> show m)
1967+ boolFilter = pure . BoolFilter
1968+ existsFilter o = ExistsFilter <$> o .: "field"
1969+ geoBoundingBoxFilter = pure . GeoBoundingBoxFilter
1970+ geoDistanceFilter o = do
1971+ case HM.toList (deleteSeveral ["distance", "distance_type", "optimize_bbox", "_cache"] o) of
1973+ gp <- GeoPoint (FieldName fn) <$> parseJSON v
1974+ GeoDistanceFilter gp <$> o .: "distance"
1975+ <*> o .: "distance_type"
1976+ <*> o .: "optimize_bbox"
1977+ <*> o .:? "_cache" .!= defaultCache
1978+ _ -> fail "Could not find GeoDistanceFilter field name"
1979+ geoDistanceRangeFilter o = do
1980+ case HM.toList (deleteSeveral ["from", "to"] o) of
1982+ gp <- GeoPoint (FieldName fn) <$> parseJSON v
1983+ rng <- DistanceRange <$> o .: "from" <*> o .: "to"
1984+ return (GeoDistanceRangeFilter gp rng)
1985+ _ -> fail "Could not find GeoDistanceRangeFilter field name"
1986+ geoPolygonFilter = fieldTagged $ \fn o -> GeoPolygonFilter fn <$> o .: "points"
1987+ idsFilter o = IdsFilter <$> o .: "type"
1989+ limitFilter o = LimitFilter <$> o .: "value"
1990+ missingFilter o = MissingFilter <$> o .: "field"
1991+ <*> o .: "existence"
1992+ <*> o .: "null_value"
1993+ prefixFilter o = case HM.toList (HM.delete "_cache" o) of
1994+ [(fn, String v)] -> PrefixFilter (FieldName fn) v <$> o .:? "_cache" .!= defaultCache
1995+ _ -> fail "Could not parse PrefixFilter"
1997+ queryFilter q = pure (QueryFilter q False)
1998+ fqueryFilter o = QueryFilter <$> o .: "query" <*> pure True
1999+ rangeFilter o = case HM.toList (deleteSeveral ["execution", "_cache"] o) of
2000+ [(fn, v)] -> RangeFilter (FieldName fn)
2002+ <*> o .: "execution"
2003+ <*> o .:? "_cache" .!= defaultCache
2004+ _ -> fail "Could not find field name for RangeFilter"
2005+ regexpFilter o = case HM.toList (deleteSeveral ["_name", "_cache", "_cache_key"] o) of
2006+ [(fn, Object o')] -> RegexpFilter (FieldName fn)
2010+ <*> o .:? "_cache" .!= defaultCache
2011+ <*> o .: "_cache_key"
2012+ _ -> fail "Could not find field name for RegexpFilter"
2013+ termFilter o = case HM.toList (HM.delete "_cache" o) of
2014+ [(termField, String termVal)] -> TermFilter (Term termField termVal)
2015+ <$> o .:? "_cache" .!= defaultCache
2016+ _ -> fail "Could not find term field for TermFilter"
2018+fieldTagged :: Monad m => (FieldName -> Object -> m a) -> Object -> m a
2019+fieldTagged f o = case HM.toList o of
2020+ [(k, Object o')] -> f (FieldName k) o'
2021+ _ -> fail "Expected object with 1 field-named key"
2023+instance ToJSON GeoPoint where
2024+ toJSON (GeoPoint (FieldName geoPointField) geoPointLatLon) =
2025+ object [ geoPointField .= geoPointLatLon ]
2028+instance ToJSON Query where
2029+ toJSON (TermQuery (Term termQueryField termQueryValue) boost) =
2031+ object [termQueryField .= object merged]]
2033+ base = [ "value" .= termQueryValue ]
2034+ boosted = maybe [] (return . ("boost" .=)) boost
2035+ merged = mappend base boosted
2037+ toJSON (TermsQuery fieldName terms) =
2038+ object [ "terms" .= object conjoined ]
2039+ where conjoined = [fieldName .= terms]
2040+ toJSON (IdsQuery idsQueryMappingName docIds) =
2041+ object [ "ids" .= object conjoined ]
2042+ where conjoined = [ "type" .= idsQueryMappingName
2043+ , "values" .= fmap toJSON docIds ]
2045+ toJSON (QueryQueryStringQuery qQueryStringQuery) =
2046+ object [ "query_string" .= qQueryStringQuery ]
2048+ toJSON (QueryMatchQuery matchQuery) =
2049+ object [ "match" .= matchQuery ]
2051+ toJSON (QueryMultiMatchQuery multiMatchQuery) =
2052+ toJSON multiMatchQuery
2054+ toJSON (QueryBoolQuery boolQuery) =
2055+ object [ "bool" .= boolQuery ]
2057+ toJSON (QueryBoostingQuery boostingQuery) =
2058+ object [ "boosting" .= boostingQuery ]
2060+ toJSON (QueryCommonTermsQuery commonTermsQuery) =
2061+ object [ "common" .= commonTermsQuery ]
2063+ toJSON (ConstantScoreFilter csFilter boost) =
2064+ object ["constant_score" .= object ["filter" .= csFilter
2065+ , "boost" .= boost]]
2067+ toJSON (ConstantScoreQuery query boost) =
2068+ object ["constant_score" .= object ["query" .= query
2069+ , "boost" .= boost]]
2071+ toJSON (QueryDisMaxQuery disMaxQuery) =
2072+ object [ "dis_max" .= disMaxQuery ]
2074+ toJSON (QueryFilteredQuery qFilteredQuery) =
2075+ object [ "filtered" .= qFilteredQuery ]
2077+ toJSON (QueryFuzzyLikeThisQuery fuzzyQuery) =
2078+ object [ "fuzzy_like_this" .= fuzzyQuery ]
2080+ toJSON (QueryFuzzyLikeFieldQuery fuzzyFieldQuery) =
2081+ object [ "fuzzy_like_this_field" .= fuzzyFieldQuery ]
2083+ toJSON (QueryFuzzyQuery fuzzyQuery) =
2084+ object [ "fuzzy" .= fuzzyQuery ]
2086+ toJSON (QueryHasChildQuery childQuery) =
2087+ object [ "has_child" .= childQuery ]
2089+ toJSON (QueryHasParentQuery parentQuery) =
2090+ object [ "has_parent" .= parentQuery ]
2092+ toJSON (QueryIndicesQuery qIndicesQuery) =
2093+ object [ "indices" .= qIndicesQuery ]
2095+ toJSON (MatchAllQuery boost) =
2096+ object [ "match_all" .= omitNulls [ "boost" .= boost ] ]
2098+ toJSON (QueryMoreLikeThisQuery query) =
2099+ object [ "more_like_this" .= query ]
2101+ toJSON (QueryMoreLikeThisFieldQuery query) =
2102+ object [ "more_like_this_field" .= query ]
2104+ toJSON (QueryNestedQuery query) =
2105+ object [ "nested" .= query ]
2107+ toJSON (QueryPrefixQuery query) =
2108+ object [ "prefix" .= query ]
2110+ toJSON (QueryRangeQuery query) =
2111+ object [ "range" .= query ]
2113+ toJSON (QueryRegexpQuery query) =
2114+ object [ "regexp" .= query ]
2116+ toJSON (QuerySimpleQueryStringQuery query) =
2117+ object [ "simple_query_string" .= query ]
2119+instance FromJSON Query where
2120+ parseJSON v = withObject "Query" parse v
2121+ where parse o = termQuery `taggedWith` "term"
2122+ <|> termsQuery `taggedWith` "terms"
2123+ <|> idsQuery `taggedWith` "ids"
2124+ <|> queryQueryStringQuery `taggedWith` "query_string"
2125+ <|> queryMatchQuery `taggedWith` "match"
2126+ <|> queryMultiMatchQuery
2127+ <|> queryBoolQuery `taggedWith` "bool"
2128+ <|> queryBoostingQuery `taggedWith` "boosting"
2129+ <|> queryCommonTermsQuery `taggedWith` "common"
2130+ <|> constantScoreFilter `taggedWith` "constant_score"
2131+ <|> constantScoreQuery `taggedWith` "constant_score"
2132+ <|> queryDisMaxQuery `taggedWith` "dis_max"
2133+ <|> queryFilteredQuery `taggedWith` "filtered"
2134+ <|> queryFuzzyLikeThisQuery `taggedWith` "fuzzy_like_this"
2135+ <|> queryFuzzyLikeFieldQuery `taggedWith` "fuzzy_like_this_field"
2136+ <|> queryFuzzyQuery `taggedWith` "fuzzy"
2137+ <|> queryHasChildQuery `taggedWith` "has_child"
2138+ <|> queryHasParentQuery `taggedWith` "has_parent"
2139+ <|> queryIndicesQuery `taggedWith` "indices"
2140+ <|> matchAllQuery `taggedWith` "match_all"
2141+ <|> queryMoreLikeThisQuery `taggedWith` "more_like_this"
2142+ <|> queryMoreLikeThisFieldQuery `taggedWith` "more_like_this_field"
2143+ <|> queryNestedQuery `taggedWith` "nested"
2144+ <|> queryPrefixQuery `taggedWith` "prefix"
2145+ <|> queryRangeQuery `taggedWith` "range"
2146+ <|> queryRegexpQuery `taggedWith` "regexp"
2147+ <|> querySimpleQueryStringQuery `taggedWith` "simple_query_string"
2148+ where taggedWith parser k = parser =<< o .: k
2149+ termQuery = fieldTagged $ \(FieldName fn) o ->
2150+ TermQuery <$> (Term fn <$> o .: "value") <*> o .:? "boost"
2151+ termsQuery o = case HM.toList o of
2152+ [(fn, vs)] -> do vals <- parseJSON vs
2154+ x:xs -> return (TermsQuery fn (x :| xs))
2155+ _ -> fail "Expected non empty list of values"
2156+ _ -> fail "Expected object with 1 field-named key"
2157+ idsQuery o = IdsQuery <$> o .: "type"
2159+ queryQueryStringQuery = pure . QueryQueryStringQuery
2160+ queryMatchQuery = pure . QueryMatchQuery
2161+ queryMultiMatchQuery = QueryMultiMatchQuery <$> parseJSON v
2162+ queryBoolQuery = pure . QueryBoolQuery
2163+ queryBoostingQuery = pure . QueryBoostingQuery
2164+ queryCommonTermsQuery = pure . QueryCommonTermsQuery
2165+ constantScoreFilter o = case HM.lookup "filter" o of
2166+ Just x -> ConstantScoreFilter <$> parseJSON x
2168+ _ -> fail "Does not appear to be a ConstantScoreFilter"
2169+ constantScoreQuery o = case HM.lookup "query" o of
2170+ Just x -> ConstantScoreQuery <$> parseJSON x
2172+ _ -> fail "Does not appear to be a ConstantScoreQuery"
2173+ queryDisMaxQuery = pure . QueryDisMaxQuery
2174+ queryFilteredQuery = pure . QueryFilteredQuery
2175+ queryFuzzyLikeThisQuery = pure . QueryFuzzyLikeThisQuery
2176+ queryFuzzyLikeFieldQuery = pure . QueryFuzzyLikeFieldQuery
2177+ queryFuzzyQuery = pure . QueryFuzzyQuery
2178+ queryHasChildQuery = pure . QueryHasChildQuery
2179+ queryHasParentQuery = pure . QueryHasParentQuery
2180+ queryIndicesQuery = pure . QueryIndicesQuery
2181+ matchAllQuery o = MatchAllQuery <$> o .:? "boost"
2182+ queryMoreLikeThisQuery = pure . QueryMoreLikeThisQuery
2183+ queryMoreLikeThisFieldQuery = pure . QueryMoreLikeThisFieldQuery
2184+ queryNestedQuery = pure . QueryNestedQuery
2185+ queryPrefixQuery = pure . QueryPrefixQuery
2186+ queryRangeQuery = pure . QueryRangeQuery
2187+ queryRegexpQuery = pure . QueryRegexpQuery
2188+ querySimpleQueryStringQuery = pure . QuerySimpleQueryStringQuery
2191+omitNulls :: [(Text, Value)] -> Value
2192+omitNulls = object . filter notNull where
2193+ notNull (_, Null) = False
2194+ notNull (_, Array a) = (not . V.null) a
2198+instance ToJSON SimpleQueryStringQuery where
2199+ toJSON SimpleQueryStringQuery {..} =
2200+ omitNulls (base ++ maybeAdd)
2201+ where base = [ "query" .= simpleQueryStringQuery ]
2202+ maybeAdd = [ "fields" .= simpleQueryStringField
2203+ , "default_operator" .= simpleQueryStringOperator
2204+ , "analyzer" .= simpleQueryStringAnalyzer
2205+ , "flags" .= simpleQueryStringFlags
2206+ , "lowercase_expanded_terms" .= simpleQueryStringLowercaseExpanded
2207+ , "locale" .= simpleQueryStringLocale ]
2209+instance FromJSON SimpleQueryStringQuery where
2210+ parseJSON = withObject "SimpleQueryStringQuery" parse
2211+ where parse o = SimpleQueryStringQuery <$> o .: "query"
2213+ <*> o .:? "default_operator"
2214+ <*> o .:? "analyzer"
2215+ <*> (parseFlags <$> o .:? "flags")
2216+ <*> o .:? "lowercase_expanded_terms"
2218+ parseFlags (Just (x:xs)) = Just (x :| xs)
2219+ parseFlags _ = Nothing
2221+instance ToJSON FieldOrFields where
2222+ toJSON (FofField fieldName) =
2224+ toJSON (FofFields fieldNames) =
2227+instance FromJSON FieldOrFields where
2228+ parseJSON v = FofField <$> parseJSON v
2229+ <|> FofFields <$> (parseNEJSON =<< parseJSON v)
2231+instance ToJSON SimpleQueryFlag where
2232+ toJSON SimpleQueryAll = "ALL"
2233+ toJSON SimpleQueryNone = "NONE"
2234+ toJSON SimpleQueryAnd = "AND"
2235+ toJSON SimpleQueryOr = "OR"
2236+ toJSON SimpleQueryPrefix = "PREFIX"
2237+ toJSON SimpleQueryPhrase = "PHRASE"
2238+ toJSON SimpleQueryPrecedence = "PRECEDENCE"
2239+ toJSON SimpleQueryEscape = "ESCAPE"
2240+ toJSON SimpleQueryWhitespace = "WHITESPACE"
2241+ toJSON SimpleQueryFuzzy = "FUZZY"
2242+ toJSON SimpleQueryNear = "NEAR"
2243+ toJSON SimpleQuerySlop = "SLOP"
2245+instance FromJSON SimpleQueryFlag where
2246+ parseJSON = withText "SimpleQueryFlag" parse
2247+ where parse "ALL" = pure SimpleQueryAll
2248+ parse "NONE" = pure SimpleQueryNone
2249+ parse "AND" = pure SimpleQueryAnd
2250+ parse "OR" = pure SimpleQueryOr
2251+ parse "PREFIX" = pure SimpleQueryPrefix
2252+ parse "PHRASE" = pure SimpleQueryPhrase
2253+ parse "PRECEDENCE" = pure SimpleQueryPrecedence
2254+ parse "ESCAPE" = pure SimpleQueryEscape
2255+ parse "WHITESPACE" = pure SimpleQueryWhitespace
2256+ parse "FUZZY" = pure SimpleQueryFuzzy
2257+ parse "NEAR" = pure SimpleQueryNear
2258+ parse "SLOP" = pure SimpleQuerySlop
2259+ parse f = fail ("Unexpected SimpleQueryFlag: " <> show f)
2261+instance ToJSON RegexpQuery where
2262+ toJSON (RegexpQuery (FieldName rqQueryField)
2263+ (Regexp regexpQueryQuery) rqQueryFlags
2265+ object [ rqQueryField .= omitNulls base ]
2266+ where base = [ "value" .= regexpQueryQuery
2267+ , "flags" .= rqQueryFlags
2268+ , "boost" .= rqQueryBoost ]
2270+instance FromJSON RegexpQuery where
2271+ parseJSON = withObject "RegexpQuery" parse
2272+ where parse = fieldTagged $ \fn o ->
2278+instance ToJSON QueryStringQuery where
2279+ toJSON (QueryStringQuery qsQueryString
2280+ qsDefaultField qsOperator
2281+ qsAnalyzer qsAllowWildcard
2282+ qsLowercaseExpanded qsEnablePositionIncrements
2283+ qsFuzzyMaxExpansions qsFuzziness
2284+ qsFuzzyPrefixLength qsPhraseSlop
2285+ qsBoost qsAnalyzeWildcard
2286+ qsGeneratePhraseQueries qsMinimumShouldMatch
2287+ qsLenient qsLocale) =
2290+ base = [ "query" .= qsQueryString
2291+ , "default_field" .= qsDefaultField
2292+ , "default_operator" .= qsOperator
2293+ , "analyzer" .= qsAnalyzer
2294+ , "allow_leading_wildcard" .= qsAllowWildcard
2295+ , "lowercase_expanded_terms" .= qsLowercaseExpanded
2296+ , "enable_position_increments" .= qsEnablePositionIncrements
2297+ , "fuzzy_max_expansions" .= qsFuzzyMaxExpansions
2298+ , "fuzziness" .= qsFuzziness
2299+ , "fuzzy_prefix_length" .= qsFuzzyPrefixLength
2300+ , "phrase_slop" .= qsPhraseSlop
2301+ , "boost" .= qsBoost
2302+ , "analyze_wildcard" .= qsAnalyzeWildcard
2303+ , "auto_generate_phrase_queries" .= qsGeneratePhraseQueries
2304+ , "minimum_should_match" .= qsMinimumShouldMatch
2305+ , "lenient" .= qsLenient
2306+ , "locale" .= qsLocale ]
2308+instance FromJSON QueryStringQuery where
2309+ parseJSON = withObject "QueryStringQuery" parse
2310+ where parse o = QueryStringQuery
2312+ <*> o .:? "default_field"
2313+ <*> o .:? "default_operator"
2314+ <*> o .:? "analyzer"
2315+ <*> o .:? "allow_leading_wildcard"
2316+ <*> o .:? "lowercase_expanded_terms"
2317+ <*> o .:? "enable_position_increments"
2318+ <*> o .:? "fuzzy_max_expansions"
2319+ <*> o .:? "fuzziness"
2320+ <*> o .:? "fuzzy_prefix_length"
2321+ <*> o .:? "phrase_slop"
2323+ <*> o .:? "analyze_wildcard"
2324+ <*> o .:? "auto_generate_phrase_queries"
2325+ <*> o .:? "minimum_should_match"
2326+ <*> o .:? "lenient"
2329+instance ToJSON RangeQuery where
2330+ toJSON (RangeQuery (FieldName fieldName) range boost) =
2331+ object [ fieldName .= object conjoined ]
2332+ where conjoined = [ "boost" .= boost ] ++ (rangeValueToPair range)
2334+instance FromJSON RangeQuery where
2335+ parseJSON = withObject "RangeQuery" parse
2336+ where parse = fieldTagged $ \fn o ->
2338+ <$> parseJSON (Object o)
2341+instance FromJSON RangeValue where
2342+ parseJSON = withObject "RangeValue" parse
2343+ where parse o = parseDate o
2345+ parseDate o = do lt <- o .:? "lt"
2349+ case (lt, lte, gt, gte) of
2350+ (Just a, _, Just b, _) -> return (RangeDateGtLt (GreaterThanD b) (LessThanD a))
2351+ (Just a, _, _, Just b)-> return (RangeDateGteLt (GreaterThanEqD b) (LessThanD a))
2352+ (_, Just a, Just b, _)-> return (RangeDateGtLte (GreaterThanD b) (LessThanEqD a))
2353+ (_, Just a, _, Just b)-> return (RangeDateGteLte (GreaterThanEqD b) (LessThanEqD a))
2354+ (_, _, Just a, _)-> return (RangeDateGt (GreaterThanD a))
2355+ (Just a, _, _, _)-> return (RangeDateLt (LessThanD a))
2356+ (_, _, _, Just a)-> return (RangeDateGte (GreaterThanEqD a))
2357+ (_, Just a, _, _)-> return (RangeDateLte (LessThanEqD a))
2358+ (Nothing, Nothing, Nothing, Nothing) -> mzero
2359+ parseDouble o = do lt <- o .:? "lt"
2363+ case (lt, lte, gt, gte) of
2364+ (Just a, _, Just b, _) -> return (RangeDoubleGtLt (GreaterThan b) (LessThan a))
2365+ (Just a, _, _, Just b)-> return (RangeDoubleGteLt (GreaterThanEq b) (LessThan a))
2366+ (_, Just a, Just b, _)-> return (RangeDoubleGtLte (GreaterThan b) (LessThanEq a))
2367+ (_, Just a, _, Just b)-> return (RangeDoubleGteLte (GreaterThanEq b) (LessThanEq a))
2368+ (_, _, Just a, _)-> return (RangeDoubleGt (GreaterThan a))
2369+ (Just a, _, _, _)-> return (RangeDoubleLt (LessThan a))
2370+ (_, _, _, Just a)-> return (RangeDoubleGte (GreaterThanEq a))
2371+ (_, Just a, _, _)-> return (RangeDoubleLte (LessThanEq a))
2372+ (Nothing, Nothing, Nothing, Nothing) -> mzero
2374+instance ToJSON PrefixQuery where
2375+ toJSON (PrefixQuery (FieldName fieldName) queryValue boost) =
2376+ object [ fieldName .= omitNulls base ]
2377+ where base = [ "value" .= queryValue
2378+ , "boost" .= boost ]
2380+instance FromJSON PrefixQuery where
2381+ parseJSON = withObject "PrefixQuery" parse
2382+ where parse = fieldTagged $ \fn o ->
2387+instance ToJSON NestedQuery where
2388+ toJSON (NestedQuery nqPath nqScoreType nqQuery) =
2389+ object [ "path" .= nqPath
2390+ , "score_mode" .= nqScoreType
2391+ , "query" .= nqQuery ]
2393+instance FromJSON NestedQuery where
2394+ parseJSON = withObject "NestedQuery" parse
2395+ where parse o = NestedQuery
2397+ <*> o .: "score_mode"
2400+instance ToJSON MoreLikeThisFieldQuery where
2401+ toJSON (MoreLikeThisFieldQuery text (FieldName fieldName)
2402+ percent mtf mqt stopwords mindf maxdf
2403+ minwl maxwl boostTerms boost analyzer) =
2404+ object [ fieldName .= omitNulls base ]
2405+ where base = [ "like_text" .= text
2406+ , "percent_terms_to_match" .= percent
2407+ , "min_term_freq" .= mtf
2408+ , "max_query_terms" .= mqt
2409+ , "stop_words" .= stopwords
2410+ , "min_doc_freq" .= mindf
2411+ , "max_doc_freq" .= maxdf
2412+ , "min_word_length" .= minwl
2413+ , "max_word_length" .= maxwl
2414+ , "boost_terms" .= boostTerms
2416+ , "analyzer" .= analyzer ]
2418+instance FromJSON MoreLikeThisFieldQuery where
2419+ parseJSON = withObject "MoreLikeThisFieldQuery" parse
2420+ where parse = fieldTagged $ \fn o ->
2421+ MoreLikeThisFieldQuery
2422+ <$> o .: "like_text"
2424+ <*> o .:? "percent_terms_to_match"
2425+ <*> o .:? "min_term_freq"
2426+ <*> o .:? "max_query_terms"
2427+ <*> (optionalNE =<< o .:? "stop_words")
2428+ <*> o .:? "min_doc_freq"
2429+ <*> o .:? "max_doc_freq"
2430+ <*> o .:? "min_word_length"
2431+ <*> o .:? "max_word_length"
2432+ <*> o .:? "boost_terms"
2434+ <*> o .:? "analyzer"
2435+ optionalNE = maybe (pure Nothing) (fmap Just . parseNEJSON)
2437+instance ToJSON MoreLikeThisQuery where
2438+ toJSON (MoreLikeThisQuery text fields percent
2439+ mtf mqt stopwords mindf maxdf
2440+ minwl maxwl boostTerms boost analyzer) =
2442+ where base = [ "like_text" .= text
2443+ , "fields" .= fields
2444+ , "percent_terms_to_match" .= percent
2445+ , "min_term_freq" .= mtf
2446+ , "max_query_terms" .= mqt
2447+ , "stop_words" .= stopwords
2448+ , "min_doc_freq" .= mindf
2449+ , "max_doc_freq" .= maxdf
2450+ , "min_word_length" .= minwl
2451+ , "max_word_length" .= maxwl
2452+ , "boost_terms" .= boostTerms
2454+ , "analyzer" .= analyzer ]
2456+instance FromJSON MoreLikeThisQuery where
2457+ parseJSON = withObject "MoreLikeThisQuery" parse
2458+ where parse o = MoreLikeThisQuery
2459+ <$> o .: "like_text"
2460+ <*> (optionalNE =<< o .:? "fields")
2461+ <*> o .:? "percent_terms_to_match"
2462+ <*> o .:? "min_term_freq"
2463+ <*> o .:? "max_query_terms"
2464+ <*> (optionalNE =<< o .:? "stop_words")
2465+ <*> o .:? "min_doc_freq"
2466+ <*> o .:? "max_doc_freq"
2467+ <*> o .:? "min_word_length"
2468+ <*> o .:? "max_word_length"
2469+ <*> o .:? "boost_terms"
2471+ <*> o .:? "analyzer"
2472+ optionalNE = maybe (pure Nothing) (fmap Just . parseNEJSON)
2474+instance ToJSON IndicesQuery where
2475+ toJSON (IndicesQuery indices query noMatch) =
2476+ omitNulls [ "indices" .= indices
2477+ , "no_match_query" .= noMatch
2478+ , "query" .= query ]
2480+instance FromJSON IndicesQuery where
2481+ parseJSON = withObject "IndicesQuery" parse
2482+ where parse o = IndicesQuery
2483+ <$> o .:? "indices" .!= []
2485+ <*> o .:? "no_match_query"
2487+instance ToJSON HasParentQuery where
2488+ toJSON (HasParentQuery queryType query scoreType) =
2489+ omitNulls [ "parent_type" .= queryType
2490+ , "score_type" .= scoreType
2491+ , "query" .= query ]
2493+instance FromJSON HasParentQuery where
2494+ parseJSON = withObject "HasParentQuery" parse
2495+ where parse o = HasParentQuery
2496+ <$> o .: "parent_type"
2498+ <*> o .:? "score_type"
2500+instance ToJSON HasChildQuery where
2501+ toJSON (HasChildQuery queryType query scoreType) =
2502+ omitNulls [ "query" .= query
2503+ , "score_type" .= scoreType
2504+ , "type" .= queryType ]
2506+instance FromJSON HasChildQuery where
2507+ parseJSON = withObject "HasChildQuery" parse
2508+ where parse o = HasChildQuery
2511+ <*> o .:? "score_type"
2513+instance ToJSON FuzzyQuery where
2514+ toJSON (FuzzyQuery (FieldName fieldName) queryText
2515+ prefixLength maxEx fuzziness boost) =
2516+ object [ fieldName .= omitNulls base ]
2517+ where base = [ "value" .= queryText
2518+ , "fuzziness" .= fuzziness
2519+ , "prefix_length" .= prefixLength
2521+ , "max_expansions" .= maxEx ]
2523+instance FromJSON FuzzyQuery where
2524+ parseJSON = withObject "FuzzyQuery" parse
2525+ where parse = fieldTagged $ \fn o ->
2528+ <*> o .: "prefix_length"
2529+ <*> o .: "max_expansions"
2530+ <*> o .: "fuzziness"
2533+instance ToJSON FuzzyLikeFieldQuery where
2534+ toJSON (FuzzyLikeFieldQuery (FieldName fieldName)
2535+ fieldText maxTerms ignoreFreq fuzziness prefixLength
2537+ object [ fieldName .=
2538+ omitNulls [ "like_text" .= fieldText
2539+ , "max_query_terms" .= maxTerms
2540+ , "ignore_tf" .= ignoreFreq
2541+ , "fuzziness" .= fuzziness
2542+ , "prefix_length" .= prefixLength
2543+ , "analyzer" .= analyzer
2544+ , "boost" .= boost ]]
2546+instance FromJSON FuzzyLikeFieldQuery where
2547+ parseJSON = withObject "FuzzyLikeFieldQuery" parse
2548+ where parse = fieldTagged $ \fn o ->
2549+ FuzzyLikeFieldQuery fn
2550+ <$> o .: "like_text"
2551+ <*> o .: "max_query_terms"
2552+ <*> o .: "ignore_tf"
2553+ <*> o .: "fuzziness"
2554+ <*> o .: "prefix_length"
2556+ <*> o .:? "analyzer"
2558+instance ToJSON FuzzyLikeThisQuery where
2559+ toJSON (FuzzyLikeThisQuery fields text maxTerms
2560+ ignoreFreq fuzziness prefixLength boost analyzer) =
2562+ where base = [ "fields" .= fields
2563+ , "like_text" .= text
2564+ , "max_query_terms" .= maxTerms
2565+ , "ignore_tf" .= ignoreFreq
2566+ , "fuzziness" .= fuzziness
2567+ , "prefix_length" .= prefixLength
2568+ , "analyzer" .= analyzer
2569+ , "boost" .= boost ]
2571+instance FromJSON FuzzyLikeThisQuery where
2572+ parseJSON = withObject "FuzzyLikeThisQuery" parse
2573+ where parse o = FuzzyLikeThisQuery
2574+ <$> o .:? "fields" .!= []
2575+ <*> o .: "like_text"
2576+ <*> o .: "max_query_terms"
2577+ <*> o .: "ignore_tf"
2578+ <*> o .: "fuzziness"
2579+ <*> o .: "prefix_length"
2581+ <*> o .:? "analyzer"
2583+instance ToJSON FilteredQuery where
2584+ toJSON (FilteredQuery query fFilter) =
2585+ object [ "query" .= query
2586+ , "filter" .= fFilter ]
2588+instance FromJSON FilteredQuery where
2589+ parseJSON = withObject "FilteredQuery" parse
2590+ where parse o = FilteredQuery
2594+instance ToJSON DisMaxQuery where
2595+ toJSON (DisMaxQuery queries tiebreaker boost) =
2597+ where base = [ "queries" .= queries
2599+ , "tie_breaker" .= tiebreaker ]
2601+instance FromJSON DisMaxQuery where
2602+ parseJSON = withObject "DisMaxQuery" parse
2603+ where parse o = DisMaxQuery
2604+ <$> o .:? "queries" .!= []
2605+ <*> o .: "tie_breaker"
2608+instance ToJSON CommonTermsQuery where
2609+ toJSON (CommonTermsQuery (FieldName fieldName)
2610+ (QueryString query) cf lfo hfo msm
2611+ boost analyzer disableCoord) =
2612+ object [fieldName .= omitNulls base ]
2613+ where base = [ "query" .= query
2614+ , "cutoff_frequency" .= cf
2615+ , "low_freq_operator" .= lfo
2616+ , "minimum_should_match" .= msm
2618+ , "analyzer" .= analyzer
2619+ , "disable_coord" .= disableCoord
2620+ , "high_freq_operator" .= hfo ]
2622+instance FromJSON CommonTermsQuery where
2623+ parseJSON = withObject "CommonTermsQuery" parse
2624+ where parse = fieldTagged $ \fn o ->
2625+ CommonTermsQuery fn
2627+ <*> o .: "cutoff_frequency"
2628+ <*> o .: "low_freq_operator"
2629+ <*> o .: "high_freq_operator"
2630+ <*> o .:? "minimum_should_match"
2632+ <*> o .:? "analyzer"
2633+ <*> o .:? "disable_coord"
2635+instance ToJSON CommonMinimumMatch where
2636+ toJSON (CommonMinimumMatch mm) = toJSON mm
2637+ toJSON (CommonMinimumMatchHighLow (MinimumMatchHighLow lowF highF)) =
2638+ object [ "low_freq" .= lowF
2639+ , "high_freq" .= highF ]
2641+instance FromJSON CommonMinimumMatch where
2642+ parseJSON v = parseMinimum v
2643+ <|> parseMinimumHighLow v
2644+ where parseMinimum = fmap CommonMinimumMatch . parseJSON
2645+ parseMinimumHighLow = fmap CommonMinimumMatchHighLow . withObject "CommonMinimumMatchHighLow" (\o ->
2646+ MinimumMatchHighLow
2647+ <$> o .: "low_freq"
2648+ <*> o .: "high_freq")
2651+instance ToJSON BoostingQuery where
2652+ toJSON (BoostingQuery bqPositiveQuery bqNegativeQuery bqNegativeBoost) =
2653+ object [ "positive" .= bqPositiveQuery
2654+ , "negative" .= bqNegativeQuery
2655+ , "negative_boost" .= bqNegativeBoost ]
2657+instance FromJSON BoostingQuery where
2658+ parseJSON = withObject "BoostingQuery" parse
2659+ where parse o = BoostingQuery
2660+ <$> o .: "positive"
2661+ <*> o .: "negative"
2662+ <*> o .: "negative_boost"
2664+instance ToJSON BoolQuery where
2665+ toJSON (BoolQuery mustM notM shouldM bqMin boost disableCoord) =
2667+ where base = [ "must" .= mustM
2668+ , "must_not" .= notM
2669+ , "should" .= shouldM
2670+ , "minimum_should_match" .= bqMin
2672+ , "disable_coord" .= disableCoord ]
2674+instance FromJSON BoolQuery where
2675+ parseJSON = withObject "BoolQuery" parse
2676+ where parse o = BoolQuery
2677+ <$> o .:? "must" .!= []
2678+ <*> o .:? "must_not" .!= []
2679+ <*> o .:? "should" .!= []
2680+ <*> o .:? "minimum_should_match"
2682+ <*> o .:? "disable_coord"
2684+instance ToJSON MatchQuery where
2685+ toJSON (MatchQuery (FieldName fieldName)
2686+ (QueryString mqQueryString) booleanOperator
2687+ zeroTermsQuery cutoffFrequency matchQueryType
2688+ analyzer maxExpansions lenient boost) =
2689+ object [ fieldName .= omitNulls base ]
2690+ where base = [ "query" .= mqQueryString
2691+ , "operator" .= booleanOperator
2692+ , "zero_terms_query" .= zeroTermsQuery
2693+ , "cutoff_frequency" .= cutoffFrequency
2694+ , "type" .= matchQueryType
2695+ , "analyzer" .= analyzer
2696+ , "max_expansions" .= maxExpansions
2697+ , "lenient" .= lenient
2698+ , "boost" .= boost ]
2700+instance FromJSON MatchQuery where
2701+ parseJSON = withObject "MatchQuery" parse
2702+ where parse = fieldTagged $ \fn o ->
2705+ <*> o .: "operator"
2706+ <*> o .: "zero_terms_query"
2707+ <*> o .:? "cutoff_frequency"
2709+ <*> o .:? "analyzer"
2710+ <*> o .:? "max_expansions"
2711+ <*> o .:? "lenient"
2714+instance ToJSON MultiMatchQuery where
2715+ toJSON (MultiMatchQuery fields (QueryString query) boolOp
2716+ ztQ tb mmqt cf analyzer maxEx lenient) =
2717+ object ["multi_match" .= omitNulls base]
2718+ where base = [ "fields" .= fmap toJSON fields
2720+ , "operator" .= boolOp
2721+ , "zero_terms_query" .= ztQ
2722+ , "tiebreaker" .= tb
2724+ , "cutoff_frequency" .= cf
2725+ , "analyzer" .= analyzer
2726+ , "max_expansions" .= maxEx
2727+ , "lenient" .= lenient ]
2729+instance FromJSON MultiMatchQuery where
2730+ parseJSON = withObject "MultiMatchQuery" parse
2731+ where parse raw = do o <- raw .: "multi_match"
2733+ <$> o .:? "fields" .!= []
2735+ <*> o .: "operator"
2736+ <*> o .: "zero_terms_query"
2737+ <*> o .:? "tiebreaker"
2739+ <*> o .:? "cutoff_frequency"
2740+ <*> o .:? "analyzer"
2741+ <*> o .:? "max_expansions"
2742+ <*> o .:? "lenient"
2744+instance ToJSON MultiMatchQueryType where
2745+ toJSON MultiMatchBestFields = "best_fields"
2746+ toJSON MultiMatchMostFields = "most_fields"
2747+ toJSON MultiMatchCrossFields = "cross_fields"
2748+ toJSON MultiMatchPhrase = "phrase"
2749+ toJSON MultiMatchPhrasePrefix = "phrase_prefix"
2751+instance FromJSON MultiMatchQueryType where
2752+ parseJSON = withText "MultiMatchPhrasePrefix" parse
2753+ where parse "best_fields" = pure MultiMatchBestFields
2754+ parse "most_fields" = pure MultiMatchMostFields
2755+ parse "cross_fields" = pure MultiMatchCrossFields
2756+ parse "phrase" = pure MultiMatchPhrase
2757+ parse "phrase_prefix" = pure MultiMatchPhrasePrefix
2758+ parse t = fail ("Unexpected MultiMatchPhrasePrefix: " <> show t)
2760+instance ToJSON BooleanOperator where
2761+ toJSON And = String "and"
2762+ toJSON Or = String "or"
2764+instance FromJSON BooleanOperator where
2765+ parseJSON = withText "BooleanOperator" parse
2766+ where parse "and" = pure And
2767+ parse "or" = pure Or
2768+ parse o = fail ("Unexpected BooleanOperator: " <> show o)
2770+instance ToJSON ZeroTermsQuery where
2771+ toJSON ZeroTermsNone = String "none"
2772+ toJSON ZeroTermsAll = String "all"
2774+instance FromJSON ZeroTermsQuery where
2775+ parseJSON = withText "ZeroTermsQuery" parse
2776+ where parse "none" = pure ZeroTermsNone
2777+ parse "all" = pure ZeroTermsAll
2778+ parse q = fail ("Unexpected ZeroTermsQuery: " <> show q)
2780+instance ToJSON MatchQueryType where
2781+ toJSON MatchPhrase = "phrase"
2782+ toJSON MatchPhrasePrefix = "phrase_prefix"
2784+instance FromJSON MatchQueryType where
2785+ parseJSON = withText "MatchQueryType" parse
2786+ where parse "phrase" = pure MatchPhrase
2787+ parse "phrase_prefix" = pure MatchPhrasePrefix
2788+ parse t = fail ("Unexpected MatchQueryType: " <> show t)
2790+instance FromJSON Status where
2791+ parseJSON (Object v) = Status <$>
2797+ parseJSON _ = empty
2800+instance ToJSON IndexSettings where
2801+ toJSON (IndexSettings s r) = object ["settings" .=
2803+ object ["number_of_shards" .= s, "number_of_replicas" .= r]
2807+instance FromJSON IndexSettings where
2808+ parseJSON = withObject "IndexSettings" parse
2809+ where parse o = do s <- o .: "settings"
2811+ IndexSettings <$> i .: "number_of_shards"
2812+ <*> i .: "number_of_replicas"
2814+instance ToJSON UpdatableIndexSetting where
2815+ toJSON (NumberOfReplicas x) = oPath ("index" :| ["number_of_replicas"]) x
2816+ toJSON (AutoExpandReplicas x) = oPath ("index" :| ["auto_expand_replicas"]) x
2817+ toJSON (RefreshInterval x) = oPath ("index" :| ["refresh_interval"]) (NominalDiffTimeJSON x)
2818+ toJSON (IndexConcurrency x) = oPath ("index" :| ["concurrency"]) x
2819+ toJSON (FailOnMergeFailure x) = oPath ("index" :| ["fail_on_merge_failure"]) x
2820+ toJSON (TranslogFlushThresholdOps x) = oPath ("index" :| ["translog", "flush_threshold_ops"]) x
2821+ toJSON (TranslogFlushThresholdSize x) = oPath ("index" :| ["translog", "flush_threshold_size"]) x
2822+ toJSON (TranslogFlushThresholdPeriod x) = oPath ("index" :| ["translog", "flush_threshold_period"]) (NominalDiffTimeJSON x)
2823+ toJSON (TranslogDisableFlush x) = oPath ("index" :| ["translog", "disable_flush"]) x
2824+ toJSON (CacheFilterMaxSize x) = oPath ("index" :| ["cache", "filter", "max_size"]) x
2825+ toJSON (CacheFilterExpire x) = oPath ("index" :| ["cache", "filter", "expire"]) (NominalDiffTimeJSON <$> x)
2826+ toJSON (GatewaySnapshotInterval x) = oPath ("index" :| ["gateway", "snapshot_interval"]) (NominalDiffTimeJSON x)
2827+ toJSON (RoutingAllocationInclude fs) = oPath ("index" :| ["routing", "allocation", "include"]) (attrFilterJSON fs)
2828+ toJSON (RoutingAllocationExclude fs) = oPath ("index" :| ["routing", "allocation", "exclude"]) (attrFilterJSON fs)
2829+ toJSON (RoutingAllocationRequire fs) = oPath ("index" :| ["routing", "allocation", "require"]) (attrFilterJSON fs)
2830+ toJSON (RoutingAllocationEnable x) = oPath ("index" :| ["routing", "allocation", "enable"]) x
2831+ toJSON (RoutingAllocationShardsPerNode x) = oPath ("index" :| ["routing", "allocation", "total_shards_per_node"]) x
2832+ toJSON (RecoveryInitialShards x) = oPath ("index" :| ["recovery", "initial_shards"]) x
2833+ toJSON (GCDeletes x) = oPath ("index" :| ["gc_deletes"]) (NominalDiffTimeJSON x)
2834+ toJSON (TTLDisablePurge x) = oPath ("index" :| ["ttl", "disable_purge"]) x
2835+ toJSON (TranslogFSType x) = oPath ("index" :| ["translog", "fs", "type"]) x
2836+ toJSON (IndexCompoundFormat x) = oPath ("index" :| ["compound_format"]) x
2837+ toJSON (IndexCompoundOnFlush x) = oPath ("index" :| ["compound_on_flush"]) x
2838+ toJSON (WarmerEnabled x) = oPath ("index" :| ["warmer", "enabled"]) x
2839+ toJSON (BlocksReadOnly x) = oPath ("blocks" :| ["read_only"]) x
2840+ toJSON (BlocksRead x) = oPath ("blocks" :| ["read"]) x
2841+ toJSON (BlocksWrite x) = oPath ("blocks" :| ["write"]) x
2842+ toJSON (BlocksMetaData x) = oPath ("blocks" :| ["metadata"]) x
2844+instance FromJSON UpdatableIndexSetting where
2845+ parseJSON = withObject "UpdatableIndexSetting" parse
2846+ where parse o = numberOfReplicas `taggedAt` ["index", "number_of_replicas"]
2847+ <|> autoExpandReplicas `taggedAt` ["index", "auto_expand_replicas"]
2848+ <|> refreshInterval `taggedAt` ["index", "refresh_interval"]
2849+ <|> indexConcurrency `taggedAt` ["index", "concurrency"]
2850+ <|> failOnMergeFailure `taggedAt` ["index", "fail_on_merge_failure"]
2851+ <|> translogFlushThresholdOps `taggedAt` ["index", "translog", "flush_threshold_ops"]
2852+ <|> translogFlushThresholdSize `taggedAt` ["index", "translog", "flush_threshold_size"]
2853+ <|> translogFlushThresholdPeriod `taggedAt` ["index", "translog", "flush_threshold_period"]
2854+ <|> translogDisableFlush `taggedAt` ["index", "translog", "disable_flush"]
2855+ <|> cacheFilterMaxSize `taggedAt` ["index", "cache", "filter", "max_size"]
2856+ <|> cacheFilterExpire `taggedAt` ["index", "cache", "filter", "expire"]
2857+ <|> gatewaySnapshotInterval `taggedAt` ["index", "gateway", "snapshot_interval"]
2858+ <|> routingAllocationInclude `taggedAt` ["index", "routing", "allocation", "include"]
2859+ <|> routingAllocationExclude `taggedAt` ["index", "routing", "allocation", "exclude"]
2860+ <|> routingAllocationRequire `taggedAt` ["index", "routing", "allocation", "require"]
2861+ <|> routingAllocationEnable `taggedAt` ["index", "routing", "allocation", "enable"]
2862+ <|> routingAllocationShardsPerNode `taggedAt` ["index", "routing", "allocation", "total_shards_per_node"]
2863+ <|> recoveryInitialShards `taggedAt` ["index", "recovery", "initial_shards"]
2864+ <|> gcDeletes `taggedAt` ["index", "gc_deletes"]
2865+ <|> ttlDisablePurge `taggedAt` ["index", "ttl", "disable_purge"]
2866+ <|> translogFSType `taggedAt` ["index", "translog", "fs", "type"]
2867+ <|> compoundFormat `taggedAt` ["index", "compound_format"]
2868+ <|> compoundOnFlush `taggedAt` ["index", "compound_on_flush"]
2869+ <|> warmerEnabled `taggedAt` ["index", "warmer", "enabled"]
2870+ <|> blocksReadOnly `taggedAt` ["blocks", "read_only"]
2871+ <|> blocksRead `taggedAt` ["blocks", "read"]
2872+ <|> blocksWrite `taggedAt` ["blocks", "write"]
2873+ <|> blocksMetaData `taggedAt` ["blocks", "metadata"]
2874+ where taggedAt f ks = taggedAt' f (Object o) ks
2875+ taggedAt' f v [] = f =<< (parseJSON v <|> (parseJSON =<< unStringlyTypeJSON v))
2876+ taggedAt' f v (k:ks) = withObject "Object" (\o -> do v' <- o .: k
2877+ taggedAt' f v' ks) v
2878+ numberOfReplicas = pure . NumberOfReplicas
2879+ autoExpandReplicas = pure . AutoExpandReplicas
2880+ refreshInterval = pure . RefreshInterval . ndtJSON
2881+ indexConcurrency = pure . IndexConcurrency
2882+ failOnMergeFailure = pure . FailOnMergeFailure
2883+ translogFlushThresholdOps = pure . TranslogFlushThresholdOps
2884+ translogFlushThresholdSize = pure . TranslogFlushThresholdSize
2885+ translogFlushThresholdPeriod = pure . TranslogFlushThresholdPeriod . ndtJSON
2886+ translogDisableFlush = pure . TranslogDisableFlush
2887+ cacheFilterMaxSize = pure . CacheFilterMaxSize
2888+ cacheFilterExpire = pure . CacheFilterExpire . fmap ndtJSON
2889+ gatewaySnapshotInterval = pure . GatewaySnapshotInterval . ndtJSON
2890+ routingAllocationInclude = fmap RoutingAllocationInclude . parseAttrFilter
2891+ routingAllocationExclude = fmap RoutingAllocationExclude . parseAttrFilter
2892+ routingAllocationRequire = fmap RoutingAllocationRequire . parseAttrFilter
2893+ routingAllocationEnable = pure . RoutingAllocationEnable
2894+ routingAllocationShardsPerNode = pure . RoutingAllocationShardsPerNode
2895+ recoveryInitialShards = pure . RecoveryInitialShards
2896+ gcDeletes = pure . GCDeletes . ndtJSON
2897+ ttlDisablePurge = pure . TTLDisablePurge
2898+ translogFSType = pure . TranslogFSType
2899+ compoundFormat = pure . IndexCompoundFormat
2900+ compoundOnFlush = pure . IndexCompoundOnFlush
2901+ warmerEnabled = pure . WarmerEnabled
2902+ blocksReadOnly = pure . BlocksReadOnly
2903+ blocksRead = pure . BlocksRead
2904+ blocksWrite = pure . BlocksWrite
2905+ blocksMetaData = pure . BlocksMetaData
2907+instance FromJSON IndexSettingsSummary where
2908+ parseJSON = withObject "IndexSettingsSummary" parse
2909+ where parse o = case HM.toList o of
2910+ [(ixn, v@(Object o'))] -> IndexSettingsSummary (IndexName ixn)
2912+ <*> (fmap (filter (not . redundant)) . parseSettings =<< o' .: "settings")
2913+ _ -> fail "Expected single-key object with index name"
2914+ redundant (NumberOfReplicas _) = True
2915+ redundant _ = False
2917+-- | For some reason in the settings API, all leaf values get returned
2918+-- as strings. This function attepmts to recover from this for all
2919+-- non-recursive JSON types. If nothing can be done or the same value
2920+-- would be return, it returns 'mzero'
2921+unStringlyTypeJSON :: MonadPlus m => Value -> m Value
2922+unStringlyTypeJSON (String "true") = return (Bool True)
2923+unStringlyTypeJSON (String "false") = return (Bool False)
2924+unStringlyTypeJSON (String "null") = return Null
2925+unStringlyTypeJSON (String t) = case readMay (T.unpack t) of
2926+ Just n -> return (Number n)
2928+unStringlyTypeJSON _ = mzero
2931+parseSettings :: Object -> Parser [UpdatableIndexSetting]
2932+parseSettings o = do
2934+ -- slice the index object into singleton hashmaps and try to parse each
2935+ parses <- forM (HM.toList o') $ \(k, v) -> do
2936+ -- blocks are now nested into the "index" key, which is not how they're serialized
2937+ let atRoot = Object (HM.singleton k v)
2938+ let atIndex = Object (HM.singleton "index" atRoot)
2939+ optional (parseJSON atRoot <|> parseJSON atIndex)
2940+ return (catMaybes parses)
2942+oPath :: ToJSON a => NonEmpty Text -> a -> Value
2943+oPath (k :| []) v = object [k .= v]
2944+oPath (k:| (h:t)) v = object [k .= oPath (h :| t) v]
2946+attrFilterJSON :: NonEmpty NodeAttrFilter -> Value
2947+attrFilterJSON fs = object [ n .= T.intercalate "," (toList vs)
2948+ | NodeAttrFilter (NodeAttrName n) vs <- toList fs]
2950+parseAttrFilter :: Value -> Parser (NonEmpty NodeAttrFilter)
2951+parseAttrFilter = withObject "NonEmpty NodeAttrFilter" parse
2952+ where parse o = case HM.toList o of
2953+ [] -> fail "Expected non-empty list of NodeAttrFilters"
2954+ x:xs -> DT.mapM (uncurry parse') (x :| xs)
2955+ parse' n = withText "Text" $ \t ->
2956+ case T.splitOn "," t of
2957+ fv:fvs -> return (NodeAttrFilter (NodeAttrName n) (fv :| fvs))
2958+ [] -> fail "Expected non-empty list of filter values"
2960+instance ToJSON ReplicaBounds where
2961+ toJSON (ReplicasBounded a b) = String (showText a <> "-" <> showText b)
2962+ toJSON (ReplicasLowerBounded a) = String (showText a <> "-all")
2963+ toJSON ReplicasUnbounded = Bool False
2965+instance FromJSON ReplicaBounds where
2966+ parseJSON v = withText "ReplicaBounds" parseText v
2967+ <|> withBool "ReplicaBounds" parseBool v
2968+ where parseText t = case T.splitOn "-" t of
2969+ [a, "all"] -> ReplicasLowerBounded <$> parseReadText a
2970+ [a, b] -> ReplicasBounded <$> parseReadText a
2971+ <*> parseReadText b
2972+ _ -> fail ("Could not parse ReplicaBounds: " <> show t)
2973+ parseBool False = pure ReplicasUnbounded
2974+ parseBool _ = fail "ReplicasUnbounded cannot be represented with True"
2976+instance ToJSON AllocationPolicy where
2977+ toJSON AllocAll = String "all"
2978+ toJSON AllocPrimaries = String "primaries"
2979+ toJSON AllocNewPrimaries = String "new_primaries"
2980+ toJSON AllocNone = String "none"
2982+instance FromJSON AllocationPolicy where
2983+ parseJSON = withText "AllocationPolicy" parse
2984+ where parse "all" = pure AllocAll
2985+ parse "primaries" = pure AllocPrimaries
2986+ parse "new_primaries" = pure AllocNewPrimaries
2987+ parse "none" = pure AllocNone
2988+ parse t = fail ("Invlaid AllocationPolicy: " <> show t)
2990+instance ToJSON InitialShardCount where
2991+ toJSON QuorumShards = String "quorum"
2992+ toJSON QuorumMinus1Shards = String "quorum-1"
2993+ toJSON FullShards = String "full"
2994+ toJSON FullMinus1Shards = String "full-1"
2995+ toJSON (ExplicitShards x) = toJSON x
2997+instance FromJSON InitialShardCount where
2998+ parseJSON v = withText "InitialShardCount" parseText v
2999+ <|> ExplicitShards <$> parseJSON v
3000+ where parseText "quorum" = pure QuorumShards
3001+ parseText "quorum-1" = pure QuorumMinus1Shards
3002+ parseText "full" = pure FullShards
3003+ parseText "full-1" = pure FullMinus1Shards
3004+ parseText _ = mzero
3006+instance ToJSON FSType where
3007+ toJSON FSSimple = "simple"
3008+ toJSON FSBuffered = "buffered"
3010+instance FromJSON FSType where
3011+ parseJSON = withText "FSType" parse
3012+ where parse "simple" = pure FSSimple
3013+ parse "buffered" = pure FSBuffered
3014+ parse t = fail ("Invalid FSType: " <> show t)
3016+instance ToJSON CompoundFormat where
3017+ toJSON (CompoundFileFormat x) = Bool x
3018+ toJSON (MergeSegmentVsTotalIndex x) = toJSON x
3020+instance FromJSON CompoundFormat where
3021+ parseJSON v = CompoundFileFormat <$> parseJSON v
3022+ <|> MergeSegmentVsTotalIndex <$> parseJSON v
3024+instance ToJSON NominalDiffTimeJSON where
3025+ toJSON (NominalDiffTimeJSON t) = String (showText (round t :: Integer) <> "s")
3027+instance FromJSON NominalDiffTimeJSON where
3028+ parseJSON = withText "NominalDiffTime" parse
3029+ where parse t = case T.takeEnd 1 t of
3030+ "s" -> NominalDiffTimeJSON . fromInteger <$> parseReadText (T.dropEnd 1 t)
3031+ _ -> fail "Invalid or missing NominalDiffTime unit (expected s)"
3033+instance ToJSON IndexTemplate where
3034+ toJSON (IndexTemplate p s m) = merge
3035+ (object [ "template" .= p
3036+ , "mappings" .= foldl' merge (object []) m
3040+ merge (Object o1) (Object o2) = toJSON $ HM.union o1 o2
3042+ merge _ _ = undefined
3044+instance (FromJSON a) => FromJSON (EsResult a) where
3045+ parseJSON jsonVal@(Object v) = do
3046+ found <- v .:? "found" .!= False
3048+ then parseJSON jsonVal
3049+ else return Nothing
3050+ EsResult <$> v .: "_index" <*>
3054+ parseJSON _ = empty
3056+instance (FromJSON a) => FromJSON (EsResultFound a) where
3057+ parseJSON (Object v) = EsResultFound <$>
3058+ v .: "_version" <*>
3060+ parseJSON _ = empty
3062+instance FromJSON EsError where
3063+ parseJSON (Object v) = EsError <$>
3066+ parseJSON _ = empty
3068+instance FromJSON IndexAliasesSummary where
3069+ parseJSON = withObject "IndexAliasesSummary" parse
3070+ where parse o = IndexAliasesSummary . mconcat <$> mapM (uncurry go) (HM.toList o)
3071+ go ixn = withObject "index aliases" $ \ia -> do
3072+ aliases <- ia .:? "aliases" .!= mempty
3073+ forM (HM.toList aliases) $ \(aName, v) -> do
3074+ let indexAlias = IndexAlias (IndexName ixn) (IndexAliasName (IndexName aName))
3075+ IndexAliasSummary indexAlias <$> parseJSON v
3078+instance ToJSON IndexAliasAction where
3079+ toJSON (AddAlias ia opts) = object ["add" .= (iaObj <> optsObj)]
3080+ where Object iaObj = toJSON ia
3081+ Object optsObj = toJSON opts
3082+ toJSON (RemoveAlias ia) = object ["remove" .= iaObj]
3083+ where Object iaObj = toJSON ia
3085+instance ToJSON IndexAlias where
3086+ toJSON IndexAlias {..} = object ["index" .= srcIndex
3087+ , "alias" .= indexAlias
3090+instance ToJSON IndexAliasCreate where
3091+ toJSON IndexAliasCreate {..} = Object (filterObj <> routingObj)
3092+ where filterObj = maybe mempty (HM.singleton "filter" . toJSON) aliasCreateFilter
3093+ Object routingObj = maybe (Object mempty) toJSON aliasCreateRouting
3095+instance ToJSON AliasRouting where
3096+ toJSON (AllAliasRouting v) = object ["routing" .= v]
3097+ toJSON (GranularAliasRouting srch idx) = object (catMaybes prs)
3098+ where prs = [("search_routing" .=) <$> srch
3099+ ,("index_routing" .=) <$> idx]
3101+instance FromJSON AliasRouting where
3102+ parseJSON = withObject "AliasRouting" parse
3103+ where parse o = parseAll o <|> parseGranular o
3104+ parseAll o = AllAliasRouting <$> o .: "routing"
3105+ parseGranular o = do
3106+ sr <- o .:? "search_routing"
3107+ ir <- o .:? "index_routing"
3108+ if isNothing sr && isNothing ir
3109+ then fail "Both search_routing and index_routing can't be blank"
3110+ else return (GranularAliasRouting sr ir)
3112+instance FromJSON IndexAliasCreate where
3113+ parseJSON v = withObject "IndexAliasCreate" parse v
3114+ where parse o = IndexAliasCreate <$> optional (parseJSON v)
3117+instance ToJSON SearchAliasRouting where
3118+ toJSON (SearchAliasRouting rvs) = toJSON (T.intercalate "," (routingValue <$> toList rvs))
3120+instance FromJSON SearchAliasRouting where
3121+ parseJSON = withText "SearchAliasRouting" parse
3122+ where parse t = SearchAliasRouting <$> parseNEJSON (String <$> T.splitOn "," t)
3124+instance ToJSON Search where
3125+ toJSON (Search query sFilter sort searchAggs highlight sTrackSortScores sFrom sSize _ sFields sSource) =
3126+ omitNulls [ "query" .= query
3127+ , "filter" .= sFilter
3129+ , "aggregations" .= searchAggs
3130+ , "highlight" .= highlight
3133+ , "track_scores" .= sTrackSortScores
3134+ , "fields" .= sFields
3135+ , "_source" .= sSource]
3138+instance ToJSON Source where
3139+ toJSON NoSource = toJSON False
3140+ toJSON (SourcePatterns patterns) = toJSON patterns
3141+ toJSON (SourceIncludeExclude incl excl) = object [ "include" .= incl, "exclude" .= excl ]
3143+instance ToJSON PatternOrPatterns where
3144+ toJSON (PopPattern pattern) = toJSON pattern
3145+ toJSON (PopPatterns patterns) = toJSON patterns
3147+instance ToJSON Include where
3148+ toJSON (Include patterns) = toJSON patterns
3150+instance ToJSON Exclude where
3151+ toJSON (Exclude patterns) = toJSON patterns
3153+instance ToJSON Pattern where
3154+ toJSON (Pattern pattern) = toJSON pattern
3157+instance ToJSON FieldHighlight where
3158+ toJSON (FieldHighlight (FieldName fName) (Just fSettings)) =
3159+ object [ fName .= fSettings ]
3160+ toJSON (FieldHighlight (FieldName fName) Nothing) =
3161+ object [ fName .= emptyObject ]
3163+instance ToJSON Highlights where
3164+ toJSON (Highlights global fields) =
3165+ omitNulls (("fields" .= fields)
3166+ : highlightSettingsPairs global)
3168+instance ToJSON HighlightSettings where
3169+ toJSON hs = omitNulls (highlightSettingsPairs (Just hs))
3171+highlightSettingsPairs :: Maybe HighlightSettings -> [Pair]
3172+highlightSettingsPairs Nothing = []
3173+highlightSettingsPairs (Just (Plain plh)) = plainHighPairs (Just plh)
3174+highlightSettingsPairs (Just (Postings ph)) = postHighPairs (Just ph)
3175+highlightSettingsPairs (Just (FastVector fvh)) = fastVectorHighPairs (Just fvh)
3178+plainHighPairs :: Maybe PlainHighlight -> [Pair]
3179+plainHighPairs Nothing = []
3180+plainHighPairs (Just (PlainHighlight plCom plNonPost)) =
3181+ [ "type" .= String "plain"]
3182+ ++ commonHighlightPairs plCom
3183+ ++ nonPostingsToPairs plNonPost
3185+postHighPairs :: Maybe PostingsHighlight -> [Pair]
3186+postHighPairs Nothing = []
3187+postHighPairs (Just (PostingsHighlight pCom)) =
3188+ ("type" .= String "postings")
3189+ : commonHighlightPairs pCom
3191+fastVectorHighPairs :: Maybe FastVectorHighlight -> [Pair]
3192+fastVectorHighPairs Nothing = []
3193+fastVectorHighPairs (Just
3194+ (FastVectorHighlight fvCom fvNonPostSettings fvBoundChars
3195+ fvBoundMaxScan fvFragOff fvMatchedFields
3197+ [ "type" .= String "fvh"
3198+ , "boundary_chars" .= fvBoundChars
3199+ , "boundary_max_scan" .= fvBoundMaxScan
3200+ , "fragment_offset" .= fvFragOff
3201+ , "matched_fields" .= fvMatchedFields
3202+ , "phraseLimit" .= fvPhraseLim]
3203+ ++ commonHighlightPairs fvCom
3204+ ++ nonPostingsToPairs fvNonPostSettings
3206+deleteSeveral :: (Eq k, Hashable k) => [k] -> HM.HashMap k v -> HM.HashMap k v
3207+deleteSeveral ks hm = foldr HM.delete hm ks
3209+commonHighlightPairs :: Maybe CommonHighlight -> [Pair]
3210+commonHighlightPairs Nothing = []
3211+commonHighlightPairs (Just (CommonHighlight chScore chForceSource chTag chEncoder
3212+ chNoMatchSize chHighlightQuery
3213+ chRequireFieldMatch)) =
3214+ [ "order" .= chScore
3215+ , "force_source" .= chForceSource
3216+ , "encoder" .= chEncoder
3217+ , "no_match_size" .= chNoMatchSize
3218+ , "highlight_query" .= chHighlightQuery
3219+ , "require_fieldMatch" .= chRequireFieldMatch]
3220+ ++ highlightTagToPairs chTag
3223+nonPostingsToPairs :: Maybe NonPostings -> [Pair]
3224+nonPostingsToPairs Nothing = []
3225+nonPostingsToPairs (Just (NonPostings npFragSize npNumOfFrags)) =
3226+ [ "fragment_size" .= npFragSize
3227+ , "number_of_fragments" .= npNumOfFrags]
3229+parseNEJSON :: (FromJSON a) => [Value] -> Parser (NonEmpty a)
3230+parseNEJSON [] = fail "Expected non-empty list"
3231+parseNEJSON (x:xs) = DT.mapM parseJSON (x :| xs)
3234+instance ToJSON HighlightEncoder where
3235+ toJSON DefaultEncoder = String "default"
3236+ toJSON HTMLEncoder = String "html"
3238+highlightTagToPairs :: Maybe HighlightTag -> [Pair]
3239+highlightTagToPairs (Just (TagSchema _)) = [ "scheme" .= String "default"]
3240+highlightTagToPairs (Just (CustomTags (pre, post))) = [ "pre_tags" .= pre
3241+ , "post_tags" .= post]
3242+highlightTagToPairs Nothing = []
3244+instance ToJSON SortSpec where
3245+ toJSON (DefaultSortSpec
3246+ (DefaultSort (FieldName dsSortFieldName) dsSortOrder dsIgnoreUnmapped
3247+ dsSortMode dsMissingSort dsNestedFilter)) =
3248+ object [dsSortFieldName .= omitNulls base] where
3249+ base = [ "order" .= dsSortOrder
3250+ , "ignore_unmapped" .= dsIgnoreUnmapped
3251+ , "mode" .= dsSortMode
3252+ , "missing" .= dsMissingSort
3253+ , "nested_filter" .= dsNestedFilter ]
3255+ toJSON (GeoDistanceSortSpec gdsSortOrder (GeoPoint (FieldName field) gdsLatLon) units) =
3256+ object [ "unit" .= units
3257+ , field .= gdsLatLon
3258+ , "order" .= gdsSortOrder ]
3261+instance ToJSON SortOrder where
3262+ toJSON Ascending = String "asc"
3263+ toJSON Descending = String "desc"
3266+instance ToJSON SortMode where
3267+ toJSON SortMin = String "min"
3268+ toJSON SortMax = String "max"
3269+ toJSON SortSum = String "sum"
3270+ toJSON SortAvg = String "avg"
3273+instance ToJSON Missing where
3274+ toJSON LastMissing = String "_last"
3275+ toJSON FirstMissing = String "_first"
3276+ toJSON (CustomMissing txt) = String txt
3279+instance ToJSON ScoreType where
3280+ toJSON ScoreTypeMax = "max"
3281+ toJSON ScoreTypeAvg = "avg"
3282+ toJSON ScoreTypeSum = "sum"
3283+ toJSON ScoreTypeNone = "none"
3285+instance FromJSON ScoreType where
3286+ parseJSON = withText "ScoreType" parse
3287+ where parse "max" = pure ScoreTypeMax
3288+ parse "avg" = pure ScoreTypeAvg
3289+ parse "sum" = pure ScoreTypeSum
3290+ parse "none" = pure ScoreTypeNone
3291+ parse t = fail ("Unexpected ScoreType: " <> show t)
3293+instance ToJSON Distance where
3294+ toJSON (Distance dCoefficient dUnit) =
3295+ String boltedTogether where
3296+ coefText = showText dCoefficient
3297+ (String unitText) = toJSON dUnit
3298+ boltedTogether = mappend coefText unitText
3300+instance FromJSON Distance where
3301+ parseJSON = withText "Distance" parse
3302+ where parse t = Distance <$> parseCoeff nT
3303+ <*> parseJSON (String unitT)
3304+ where (nT, unitT) = T.span validForNumber t
3305+ -- may be a better way to do this
3306+ validForNumber '-' = True
3307+ validForNumber '.' = True
3308+ validForNumber 'e' = True
3309+ validForNumber c = isNumber c
3310+ parseCoeff "" = fail "Empty string cannot be parsed as number"
3311+ parseCoeff s = return (read (T.unpack s))
3313+instance ToJSON DistanceUnit where
3314+ toJSON Miles = String "mi"
3315+ toJSON Yards = String "yd"
3316+ toJSON Feet = String "ft"
3317+ toJSON Inches = String "in"
3318+ toJSON Kilometers = String "km"
3319+ toJSON Meters = String "m"
3320+ toJSON Centimeters = String "cm"
3321+ toJSON Millimeters = String "mm"
3322+ toJSON NauticalMiles = String "nmi"
3325+instance FromJSON DistanceUnit where
3326+ parseJSON = withText "DistanceUnit" parse
3327+ where parse "mi" = pure Miles
3328+ parse "yd" = pure Yards
3329+ parse "ft" = pure Feet
3330+ parse "in" = pure Inches
3331+ parse "km" = pure Kilometers
3332+ parse "m" = pure Meters
3333+ parse "cm" = pure Centimeters
3334+ parse "mm" = pure Millimeters
3335+ parse "nmi" = pure NauticalMiles
3336+ parse u = fail ("Unrecognized DistanceUnit: " <> show u)
3338+instance ToJSON DistanceType where
3339+ toJSON Arc = String "arc"
3340+ toJSON SloppyArc = String "sloppy_arc"
3341+ toJSON Plane = String "plane"
3343+instance FromJSON DistanceType where
3344+ parseJSON = withText "DistanceType" parse
3345+ where parse "arc" = pure Arc
3346+ parse "sloppy_arc" = pure SloppyArc
3347+ parse "plane" = pure Plane
3348+ parse t = fail ("Unrecognized DistanceType: " <> show t)
3351+instance ToJSON OptimizeBbox where
3352+ toJSON NoOptimizeBbox = String "none"
3353+ toJSON (OptimizeGeoFilterType gft) = toJSON gft
3355+instance FromJSON OptimizeBbox where
3356+ parseJSON v = withText "NoOptimizeBbox" parseNoOptimize v
3357+ <|> parseOptimize v
3358+ where parseNoOptimize "none" = pure NoOptimizeBbox
3359+ parseNoOptimize _ = mzero
3360+ parseOptimize = fmap OptimizeGeoFilterType . parseJSON
3362+instance ToJSON GeoBoundingBoxConstraint where
3363+ toJSON (GeoBoundingBoxConstraint
3364+ (FieldName gbbcGeoBBField) gbbcConstraintBox cache type') =
3365+ object [gbbcGeoBBField .= gbbcConstraintBox
3366+ , "_cache" .= cache
3369+instance FromJSON GeoBoundingBoxConstraint where
3370+ parseJSON = withObject "GeoBoundingBoxConstraint" parse
3371+ where parse o = case HM.toList (deleteSeveral ["type", "_cache"] o) of
3372+ [(fn, v)] -> GeoBoundingBoxConstraint (FieldName fn)
3374+ <*> o .:? "_cache" .!= defaultCache
3376+ _ -> fail "Could not find field name for GeoBoundingBoxConstraint"
3378+instance ToJSON GeoFilterType where
3379+ toJSON GeoFilterMemory = String "memory"
3380+ toJSON GeoFilterIndexed = String "indexed"
3382+instance FromJSON GeoFilterType where
3383+ parseJSON = withText "GeoFilterType" parse
3384+ where parse "memory" = pure GeoFilterMemory
3385+ parse "indexed" = pure GeoFilterIndexed
3386+ parse t = fail ("Unrecognized GeoFilterType: " <> show t)
3388+instance ToJSON GeoBoundingBox where
3389+ toJSON (GeoBoundingBox gbbTopLeft gbbBottomRight) =
3390+ object ["top_left" .= gbbTopLeft
3391+ , "bottom_right" .= gbbBottomRight]
3393+instance FromJSON GeoBoundingBox where
3394+ parseJSON = withObject "GeoBoundingBox" parse
3395+ where parse o = GeoBoundingBox
3396+ <$> o .: "top_left"
3397+ <*> o .: "bottom_right"
3399+instance ToJSON LatLon where
3400+ toJSON (LatLon lLat lLon) =
3401+ object ["lat" .= lLat
3404+instance FromJSON LatLon where
3405+ parseJSON = withObject "LatLon" parse
3406+ where parse o = LatLon <$> o .: "lat"
3409+-- index for smaller ranges, fielddata for longer ranges
3410+instance ToJSON RangeExecution where
3411+ toJSON RangeExecutionIndex = "index"
3412+ toJSON RangeExecutionFielddata = "fielddata"
3415+instance FromJSON RangeExecution where
3416+ parseJSON = withText "RangeExecution" parse
3417+ where parse "index" = pure RangeExecutionIndex
3418+ parse "fielddata" = pure RangeExecutionFielddata
3419+ parse t = error ("Unrecognized RangeExecution " <> show t)
3421+instance ToJSON RegexpFlags where
3422+ toJSON AllRegexpFlags = String "ALL"
3423+ toJSON NoRegexpFlags = String "NONE"
3424+ toJSON (SomeRegexpFlags (h :| fs)) = String $ T.intercalate "|" flagStrs
3425+ where flagStrs = map flagStr . nub $ h:fs
3426+ flagStr AnyString = "ANYSTRING"
3427+ flagStr Automaton = "AUTOMATON"
3428+ flagStr Complement = "COMPLEMENT"
3429+ flagStr Empty = "EMPTY"
3430+ flagStr Intersection = "INTERSECTION"
3431+ flagStr Interval = "INTERVAL"
3433+instance FromJSON RegexpFlags where
3434+ parseJSON = withText "RegexpFlags" parse
3435+ where parse "ALL" = pure AllRegexpFlags
3436+ parse "NONE" = pure NoRegexpFlags
3437+ parse t = SomeRegexpFlags <$> parseNEJSON (String <$> T.splitOn "|" t)
3439+instance FromJSON RegexpFlag where
3440+ parseJSON = withText "RegexpFlag" parse
3441+ where parse "ANYSTRING" = pure AnyString
3442+ parse "AUTOMATON" = pure Automaton
3443+ parse "COMPLEMENT" = pure Complement
3444+ parse "EMPTY" = pure Empty
3445+ parse "INTERSECTION" = pure Intersection
3446+ parse "INTERVAL" = pure Interval
3447+ parse f = fail ("Unknown RegexpFlag: " <> show f)
3449+instance ToJSON Term where
3450+ toJSON (Term field value) = object ["term" .= object
3453+instance FromJSON Term where
3454+ parseJSON = withObject "Term" parse
3455+ where parse o = do termObj <- o .: "term"
3456+ case HM.toList termObj of
3457+ [(fn, v)] -> Term fn <$> parseJSON v
3458+ _ -> fail "Expected object with 1 field-named key"
3460+instance ToJSON BoolMatch where
3461+ toJSON (MustMatch term cache) = object ["must" .= term,
3463+ toJSON (MustNotMatch term cache) = object ["must_not" .= term,
3465+ toJSON (ShouldMatch terms cache) = object ["should" .= fmap toJSON terms,
3468+instance FromJSON BoolMatch where
3469+ parseJSON = withObject "BoolMatch" parse
3470+ where parse o = mustMatch `taggedWith` "must"
3471+ <|> mustNotMatch `taggedWith` "must_not"
3472+ <|> shouldMatch `taggedWith` "should"
3473+ where taggedWith parser k = parser =<< o .: k
3474+ mustMatch t = MustMatch t <$> o .:? "_cache" .!= defaultCache
3475+ mustNotMatch t = MustNotMatch t <$> o .:? "_cache" .!= defaultCache
3476+ shouldMatch t = ShouldMatch t <$> o .:? "_cache" .!= defaultCache
3478+instance (FromJSON a) => FromJSON (SearchResult a) where
3479+ parseJSON (Object v) = SearchResult <$>
3481+ v .: "timed_out" <*>
3484+ v .:? "aggregations" <*>
3486+ parseJSON _ = empty
3488+instance (FromJSON a) => FromJSON (SearchHits a) where
3489+ parseJSON (Object v) = SearchHits <$>
3491+ v .: "max_score" <*>
3493+ parseJSON _ = empty
3495+instance (FromJSON a) => FromJSON (Hit a) where
3496+ parseJSON (Object v) = Hit <$>
3501+ v .:? "_source" <*>
3503+ parseJSON _ = empty
3505+instance FromJSON ShardResult where
3506+ parseJSON (Object v) = ShardResult <$>
3508+ v .: "successful" <*>
3510+ parseJSON _ = empty
3513+instance FromJSON DocVersion where
3516+ maybe (fail "DocVersion out of range") return $ mkDocVersion i
3518+-- This insanity is because ES *sometimes* returns Replica/Shard counts as strings
3519+instance FromJSON ReplicaCount where
3520+ parseJSON v = parseAsInt v
3521+ <|> parseAsString v
3522+ where parseAsInt = fmap ReplicaCount . parseJSON
3523+ parseAsString = withText "ReplicaCount" (fmap ReplicaCount . parseReadText)
3525+instance FromJSON ShardCount where
3526+ parseJSON v = parseAsInt v
3527+ <|> parseAsString v
3528+ where parseAsInt = fmap ShardCount . parseJSON
3529+ parseAsString = withText "ShardCount" (fmap ShardCount . parseReadText)
3531+instance Bounded DocVersion where
3532+ minBound = DocVersion 1
3533+ maxBound = DocVersion 9200000000000000000 -- 9.2e+18
540−newtype ScrollId =
3535+instance Enum DocVersion where
541− ScrollId Text
3536+ succ x
542− deriving (Eq, Show, Ord, ToJSON, FromJSON)
3537+ | x /= maxBound = DocVersion (succ $ docVersionNumber x)
3538+ | otherwise = succError "DocVersion"
3540+ | x /= minBound = DocVersion (pred $ docVersionNumber x)
3541+ | otherwise = predError "DocVersion"
3543+ fromMaybe (error $ show i ++ " out of DocVersion range") $ mkDocVersion i
3544+ fromEnum = docVersionNumber
3545+ enumFrom = boundedEnumFrom
3546+ enumFromThen = boundedEnumFromThen