@@ -1,23 +1,20 @@
1−{-# LANGUAGE DeriveDataTypeable #-}
2−{-# LANGUAGE DeriveGeneric #-}
3{-# LANGUAGE FlexibleContexts #-}
1{-# LANGUAGE FlexibleContexts #-}
4{-# LANGUAGE FlexibleInstances #-}
2{-# LANGUAGE FlexibleInstances #-}
5{-# LANGUAGE GeneralizedNewtypeDeriving #-}
3{-# LANGUAGE GeneralizedNewtypeDeriving #-}
6{-# LANGUAGE MultiParamTypeClasses #-}
4{-# LANGUAGE MultiParamTypeClasses #-}
7−{-# LANGUAGE NoMonomorphismRestriction #-}
5+{-# LANGUAGE NamedFieldPuns #-}
8{-# LANGUAGE OverloadedStrings #-}
6{-# LANGUAGE OverloadedStrings #-}
9{-# LANGUAGE RecordWildCards #-}
7{-# LANGUAGE RecordWildCards #-}
10{-# LANGUAGE UndecidableInstances #-}
8{-# LANGUAGE UndecidableInstances #-}
11−{-# LANGUAGE NamedFieldPuns #-}
13-------------------------------------------------------------------------------
10-------------------------------------------------------------------------------
15-- Module : Database.Bloodhound.Types
12-- Module : Database.Bloodhound.Types
16−-- Copyright : (C) 2014 Chris Allen
13+-- Copyright : (C) 2014, 2018 Chris Allen
17-- License : BSD-style (see the file LICENSE)
14-- License : BSD-style (see the file LICENSE)
18-- Maintainer : Chris Allen <cma@bitemyapp.com
15-- Maintainer : Chris Allen <cma@bitemyapp.com
19-- Stability : provisional
16-- Stability : provisional
20−-- Portability : DeriveGeneric, RecordWildCards
17+-- Portability : RecordWildCards
22-- Data types for describing actions and data structures performed to interact
19-- Data types for describing actions and data structures performed to interact
23-- with Elasticsearch. The two main buckets your queries against Elasticsearch
20-- with Elasticsearch. The two main buckets your queries against Elasticsearch
@@ -45,10 +42,15 @@ module Database.Bloodhound.Types
45 , mkTermsAggregation
42 , mkTermsAggregation
46 , mkTermsScriptAggregation
43 , mkTermsScriptAggregation
47 , mkDateHistogram
44 , mkDateHistogram
45+ , mkCardinalityAggregation
48 , mkDocVersion
46 , mkDocVersion
48+ , mkExtendedStatsAggregation
49 , docVersionNumber
49 , docVersionNumber
51 , toDateHistogram
52 , toDateHistogram
52 , omitNulls
54 , omitNulls
@@ -59,6 +61,9 @@ module Database.Bloodhound.Types
60 , MonadBH(..)
62 , MonadBH(..)
61 , Version(..)
63 , Version(..)
62 , Status(..)
67 , Status(..)
63 , Existence(..)
68 , Existence(..)
64 , NullValue(..)
69 , NullValue(..)
@@ -66,8 +71,12 @@ module Database.Bloodhound.Types
66 , UpdatableIndexSetting(..)
71 , UpdatableIndexSetting(..)
67 , IndexSettingsSummary(..)
72 , IndexSettingsSummary(..)
68 , AllocationPolicy(..)
73 , AllocationPolicy(..)
69 , ReplicaBounds(..)
75 , ReplicaBounds(..)
70 , Bytes(..)
76 , Bytes(..)
71 , FSType(..)
80 , FSType(..)
72 , InitialShardCount(..)
81 , InitialShardCount(..)
73 , NodeAttrFilter(..)
82 , NodeAttrFilter(..)
@@ -111,8 +120,8 @@ module Database.Bloodhound.Types
111 , Pattern(..)
120 , Pattern(..)
112 , ShardResult(..)
121 , ShardResult(..)
113 , Hit(..)
122 , Hit(..)
114 , Filter(..)
124 , Filter(..)
116 , BoolMatch(..)
125 , BoolMatch(..)
117 , Term(..)
126 , Term(..)
118 , GeoPoint(..)
127 , GeoPoint(..)
@@ -139,8 +148,22 @@ module Database.Bloodhound.Types
139 , RegexpFlags(..)
148 , RegexpFlags(..)
140 , RegexpFlag(..)
149 , RegexpFlag(..)
141 , FieldName(..)
150 , FieldName(..)
142 , Script(..)
154 , Script(..)
155+ , ScriptLanguage(..)
143 , IndexName(..)
161 , IndexName(..)
162+ , IndexSelection(..)
165+ , ForceMergeIndexSettings(..)
166+ , defaultForceMergeIndexSettings
144 , TemplateName(..)
167 , TemplateName(..)
145 , TemplatePattern(..)
168 , TemplatePattern(..)
146 , MappingName(..)
169 , MappingName(..)
@@ -164,8 +187,19 @@ module Database.Bloodhound.Types
164 , BoolQuery(..)
187 , BoolQuery(..)
165 , BoostingQuery(..)
188 , BoostingQuery(..)
166 , CommonTermsQuery(..)
189 , CommonTermsQuery(..)
190+ , FunctionScoreQuery(..)
193+ , FunctionScoreFunctions(..)
194+ , ComponentFunctionScoreFunction(..)
195+ , FunctionScoreFunction(..)
198+ , FieldValueFactor(..)
200+ , FactorModifier(..)
201+ , FactorMissingFieldValue(..)
167 , DisMaxQuery(..)
202 , DisMaxQuery(..)
169 , FuzzyLikeThisQuery(..)
203 , FuzzyLikeThisQuery(..)
170 , FuzzyLikeFieldQuery(..)
204 , FuzzyLikeFieldQuery(..)
171 , FuzzyQuery(..)
205 , FuzzyQuery(..)
@@ -181,10 +215,16 @@ module Database.Bloodhound.Types
181 , RangeQuery(..)
215 , RangeQuery(..)
182 , RegexpQuery(..)
216 , RegexpQuery(..)
183 , QueryString(..)
217 , QueryString(..)
218+ , TemplateQueryInline(..)
219+ , TemplateQueryKeyValuePairs(..)
184 , BooleanOperator(..)
221 , BooleanOperator(..)
185 , ZeroTermsQuery(..)
222 , ZeroTermsQuery(..)
186 , CutoffFrequency(..)
223 , CutoffFrequency(..)
187 , Analyzer(..)
224 , Analyzer(..)
188 , MaxExpansions(..)
228 , MaxExpansions(..)
189 , Lenient(..)
229 , Lenient(..)
190 , MatchQueryType(..)
230 , MatchQueryType(..)
@@ -231,15 +271,108 @@ module Database.Bloodhound.Types
231 , CollectionMode(..)
271 , CollectionMode(..)
232 , TermOrder(..)
272 , TermOrder(..)
233 , TermInclusion(..)
273 , 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 (..)
235 , Aggregation(..)
364 , Aggregation(..)
236 , Aggregations
365 , Aggregations
237 , AggregationResults
366 , AggregationResults
238 , Bucket(..)
368 , Bucket(..)
239 , BucketAggregation(..)
369 , BucketAggregation(..)
240 , TermsAggregation(..)
370 , TermsAggregation(..)
371+ , MissingAggregation(..)
241 , ValueCountAggregation(..)
372 , ValueCountAggregation(..)
242 , FilterAggregation(..)
373 , FilterAggregation(..)
374+ , CardinalityAggregation(..)
375+ , MaxAggregation(..)
243 , DateHistogramAggregation(..)
376 , DateHistogramAggregation(..)
244 , DateRangeAggregation(..)
377 , DateRangeAggregation(..)
245 , DateRangeAggRange(..)
378 , DateRangeAggRange(..)
@@ -247,6 +380,9 @@ module Database.Bloodhound.Types
247 , DateMathAnchor(..)
380 , DateMathAnchor(..)
248 , DateMathModifier(..)
381 , DateMathModifier(..)
249 , DateMathUnit(..)
382 , DateMathUnit(..)
383+ , TopHitsAggregation(..)
384+ , StatisticsAggregation(..)
251 , Highlights(..)
387 , Highlights(..)
252 , FieldHighlight(..)
388 , FieldHighlight(..)
@@ -260,644 +396,36 @@ module Database.Bloodhound.Types
260 , HighlightTag(..)
396 , HighlightTag(..)
261 , HitHighlight
397 , HitHighlight
263 , TermsResult(..)
400 , TermsResult(..)
264 , DateHistogramResult(..)
401 , DateHistogramResult(..)
265 , DateRangeResult(..)
402 , DateRangeResult(..)
267 , EsUsername(..)
405 , EsUsername(..)
268 , EsPassword(..)
406 , EsPassword(..)
271−import Control.Applicative
272−import Control.Monad.Catch
273−import Control.Monad.Except
274−import Control.Monad.Reader
275−import Control.Monad.State
276−import Control.Monad.Writer
278−import Data.Aeson.Types (Pair, Parser, emptyObject,
280−import qualified Data.ByteString.Lazy.Char8 as L
282−import Data.Hashable (Hashable)
283−import qualified Data.HashMap.Strict as HM
284−import Data.List (foldl', nub)
285−import Data.List.NonEmpty (NonEmpty (..), toList)
286−import qualified Data.Map.Strict as M
288−import Data.Scientific (Scientific)
289−import Data.Text (Text)
290−import qualified Data.Text as T
291−import Data.Time.Calendar
292−import Data.Time.Clock (NominalDiffTime, UTCTime)
293−import Data.Time.Clock.POSIX
294−import qualified Data.Traversable as DT
295−import Data.Typeable (Typeable)
296−import qualified Data.Vector as V
298−import GHC.Generics (Generic)
299−import Network.HTTP.Client
300−import qualified Network.HTTP.Types.Method as NHTM
302−import Database.Bloodhound.Types.Class
303−import Database.Bloodhound.Types.Internal
306−-- >>> :set -XOverloadedStrings
307−-- >>> import Database.Bloodhound
308−-- >>> let testServer = (Server "http://localhost:9200")
309−-- >>> let testIndex = IndexName "twitter"
310−-- >>> let testMapping = MappingName "tweet"
311−-- >>> let defaultIndexSettings = IndexSettings (ShardCount 3) (ReplicaCount 2)
313−-- defaultIndexSettings is exported by Database.Bloodhound as well
314−-- no trailing slashes in servers, library handles building the path.
316−-- | Create a 'BHEnv' with all optional fields defaulted. HTTP hook
317−-- will be a noop. You can use the exported fields to customize it further, e.g.:
319−-- >> (mkBHEnv myServer myManager) { bhRequestHook = customHook }
320−mkBHEnv :: Server -> Manager -> BHEnv
321−mkBHEnv s m = BHEnv s m return
323−newtype BH m a = BH {
324− unBH :: ReaderT BHEnv m a
325− } deriving ( Functor
339−instance MonadTrans BH where
342−instance (MonadReader r m) => MonadReader r (BH m) where
344− local f (BH (ReaderT m)) = BH $ ReaderT $ \r ->
347−instance (Functor m, Applicative m, MonadIO m) => MonadBH (BH m) where
348− getBHEnv = BH getBHEnv
350−runBH :: BHEnv -> BH m a -> m a
351−runBH e f = runReaderT (unBH f) e
353−{-| 'Version' is embedded in 'Status' -}
354−data Version = Version { number :: Text
355− , build_hash :: Text
356− , build_timestamp :: UTCTime
357− , build_snapshot :: Bool
358− , lucene_version :: Text } deriving (Eq, Show, Generic, Typeable)
360−{-| 'Status' is a data type for describing the JSON body returned by
361− Elasticsearch when you query its status. This was deprecated in 1.2.0.
363− <http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-status.html#indices-status>
366−data Status = Status { ok :: Maybe Bool
369− , version :: Version
370− , tagline :: Text } deriving (Eq, Show, Generic)
372−{-| 'IndexSettings' is used to configure the shards and replicas when you create
373− an Elasticsearch Index.
375− <http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-create-index.html>
379− IndexSettings { indexShards :: ShardCount
380− , indexReplicas :: ReplicaCount } deriving (Eq, Show, Generic, Typeable)
382−{-| 'defaultIndexSettings' is an 'IndexSettings' with 3 shards and 2 replicas. -}
383−defaultIndexSettings :: IndexSettings
384−defaultIndexSettings = IndexSettings (ShardCount 3) (ReplicaCount 2)
386−{-| 'UpdatableIndexSetting' are settings which may be updated after an index is created.
388− <https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-update-settings.html>
390−data UpdatableIndexSetting = NumberOfReplicas ReplicaCount
391− -- ^ The number of replicas each shard has.
392− | AutoExpandReplicas ReplicaBounds
393− | BlocksReadOnly Bool
394− -- ^ Set to True to have the index read only. False to allow writes and metadata changes.
396− -- ^ Set to True to disable read operations against the index.
398− -- ^ Set to True to disable write operations against the index.
399− | BlocksMetaData Bool
400− -- ^ Set to True to disable metadata operations against the index.
401− | RefreshInterval NominalDiffTime
402− -- ^ The async refresh interval of a shard
403− | IndexConcurrency Int
404− | FailOnMergeFailure Bool
405− | TranslogFlushThresholdOps Int
406− -- ^ When to flush on operations.
407− | TranslogFlushThresholdSize Bytes
408− -- ^ When to flush based on translog (bytes) size.
409− | TranslogFlushThresholdPeriod NominalDiffTime
410− -- ^ When to flush based on a period of not flushing.
411− | TranslogDisableFlush Bool
412− -- ^ Disables flushing. Note, should be set for a short interval and then enabled.
413− | CacheFilterMaxSize (Maybe Bytes)
414− -- ^ The maximum size of filter cache (per segment in shard).
415− | CacheFilterExpire (Maybe NominalDiffTime)
416− -- ^ The expire after access time for filter cache.
417− | GatewaySnapshotInterval NominalDiffTime
418− -- ^ The gateway snapshot interval (only applies to shared gateways).
419− | RoutingAllocationInclude (NonEmpty NodeAttrFilter)
420− -- ^ A node matching any rule will be allowed to host shards from the index.
421− | RoutingAllocationExclude (NonEmpty NodeAttrFilter)
422− -- ^ A node matching any rule will NOT be allowed to host shards from the index.
423− | RoutingAllocationRequire (NonEmpty NodeAttrFilter)
424− -- ^ Only nodes matching all rules will be allowed to host shards from the index.
425− | RoutingAllocationEnable AllocationPolicy
426− -- ^ Enables shard allocation for a specific index.
427− | RoutingAllocationShardsPerNode ShardCount
428− -- ^ Controls the total number of shards (replicas and primaries) allowed to be allocated on a single node.
429− | RecoveryInitialShards InitialShardCount
430− -- ^ When using local gateway a particular shard is recovered only if there can be allocated quorum shards in the cluster.
431− | GCDeletes NominalDiffTime
432− | TTLDisablePurge Bool
433− -- ^ Disables temporarily the purge of expired docs.
434− | TranslogFSType FSType
435− | IndexCompoundFormat CompoundFormat
436− | IndexCompoundOnFlush Bool
437− | WarmerEnabled Bool
438− deriving (Eq, Show, Generic, Typeable)
440−data AllocationPolicy = AllocAll
441− -- ^ Allows shard allocation for all shards.
443− -- ^ Allows shard allocation only for primary shards.
445− -- ^ Allows shard allocation only for primary shards for new indices.
447− -- ^ No shard allocation is allowed
448− deriving (Eq, Show, Generic, Typeable)
450−data ReplicaBounds = ReplicasBounded Int Int
451− | ReplicasLowerBounded Int
453− deriving (Eq, Show, Generic, Typeable)
455−newtype Bytes = Bytes Int deriving (Eq, Show, Generic, Typeable, Ord, ToJSON, FromJSON)
457−data FSType = FSSimple
458− | FSBuffered deriving (Eq, Show, Generic, Typeable, Ord)
460−data InitialShardCount = QuorumShards
461− | QuorumMinus1Shards
464− | ExplicitShards Int
465− deriving (Eq, Show, Generic, Typeable)
467−data NodeAttrFilter = NodeAttrFilter { nodeAttrFilterName :: NodeAttrName
468− , nodeAttrFilterValues :: NonEmpty Text}
469− deriving (Eq, Show, Generic, Ord)
471−newtype NodeAttrName = NodeAttrName Text deriving (Eq, Show, Ord, Generic, Typeable)
473−data CompoundFormat = CompoundFileFormat Bool
474− | MergeSegmentVsTotalIndex Double
475− -- ^ percentage between 0 and 1 where 0 is false, 1 is true
476− deriving (Eq, Show, Generic, Typeable)
478−newtype NominalDiffTimeJSON = NominalDiffTimeJSON { ndtJSON :: NominalDiffTime }
480−data IndexSettingsSummary = IndexSettingsSummary { sSummaryIndexName :: IndexName
481− , sSummaryFixedSettings :: IndexSettings
482− , sSummaryUpdateable :: [UpdatableIndexSetting]}
483− deriving (Eq, Show, Generic, Typeable)
485−{-| 'Reply' and 'Method' are type synonyms from 'Network.HTTP.Types.Method.Method' -}
486−type Reply = Network.HTTP.Client.Response L.ByteString
487−type Method = NHTM.Method
489−{-| 'OpenCloseIndex' is a sum type for opening and closing indices.
491− <http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-open-close.html>
493−data OpenCloseIndex = OpenIndex | CloseIndex deriving (Eq, Show, Generic, Typeable)
495−data FieldType = GeoPointType
501− | ByteType deriving (Eq, Show, Generic, Typeable)
503−data FieldDefinition =
504− FieldDefinition { fieldType :: FieldType } deriving (Eq, Show, Generic, Typeable)
506−{-| An 'IndexTemplate' defines a template that will automatically be
507− applied to new indices created. The templates include both
508− 'IndexSettings' and mappings, and a simple 'TemplatePattern' that
509− controls if the template will be applied to the index created.
510− Specify mappings as follows: @[toJSON TweetMapping, ...]@
512− https://www.elastic.co/guide/en/elasticsearch/reference/1.7/indices-templates.html
515− IndexTemplate { templatePattern :: TemplatePattern
516− , templateSettings :: Maybe IndexSettings
517− , templateMappings :: [Value]
521− MappingField { mappingFieldName :: FieldName
522− , fieldDefinition :: FieldDefinition } deriving (Eq, Show, Generic, Typeable)
524−{-| Support for type reification of 'Mapping's is currently incomplete, for
525− now the mapping API verbiage expects a 'ToJSON'able blob.
527− Indexes have mappings, mappings are schemas for the documents contained in the
528− index. I'd recommend having only one mapping per index, always having a mapping,
529− and keeping different kinds of documents separated if possible.
531−data Mapping = Mapping { typeName :: TypeName
532− , mappingFields :: [MappingField] } deriving (Eq, Show, Generic, Typeable)
534−{-| 'BulkOperation' is a sum type for expressing the four kinds of bulk
535− operation index, create, delete, and update. 'BulkIndex' behaves like an
536− "upsert", 'BulkCreate' will fail if a document already exists at the DocId.
538− <http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-bulk.html#docs-bulk>
541− BulkIndex IndexName MappingName DocId Value
542− | BulkCreate IndexName MappingName DocId Value
543− | BulkDelete IndexName MappingName DocId
544− | BulkUpdate IndexName MappingName DocId Value deriving (Eq, Show, Generic, Typeable)
546−{-| 'EsResult' describes the standard wrapper JSON document that you see in
547− successful Elasticsearch lookups or lookups that couldn't find the document.
549−data EsResult a = EsResult { _index :: Text
552− , foundResult :: Maybe (EsResultFound a)} deriving (Eq, Show, Generic, Typeable)
554−{-| 'EsResultFound' contains the document and its metadata inside of an
555− 'EsResult' when the document was successfully found.
557−data EsResultFound a = EsResultFound { _version :: DocVersion
558− , _source :: a } deriving (Eq, Show, Generic, Typeable)
560−{-| 'EsError' is the generic type that will be returned when there was a
561− problem. If you can't parse the expected response, its a good idea to
564−data EsError = EsError { errorStatus :: Int
565− , errorMessage :: Text } deriving (Eq, Show, Generic, Typeable)
567−{-| 'EsProtocolException' ideally should never be thrown. It will only
568−be thrown when Bloodhound can't parse a response returned by the
569−ElasticSearch server. This should be reported as a bug to the bug
570−tracker. Be sure to include the body.
572−data EsProtocolException = EsProtocolException { esProtoExBody :: L.ByteString }
573− deriving (Eq, Show, Generic, Typeable)
575−instance Exception EsProtocolException
577−data IndexAlias = IndexAlias { srcIndex :: IndexName
578− , indexAlias :: IndexAliasName } deriving (Eq, Show, Generic, Typeable)
580−newtype IndexAliasName = IndexAliasName { indexAliasName :: IndexName } deriving (Eq, Show, Generic, ToJSON)
582−data IndexAliasAction = AddAlias IndexAlias IndexAliasCreate
583− | RemoveAlias IndexAlias deriving (Show, Eq, Generic, Typeable)
585−data IndexAliasCreate = IndexAliasCreate { aliasCreateRouting :: Maybe AliasRouting
586− , aliasCreateFilter :: Maybe Filter}
587− deriving (Show, Eq, Generic, Typeable)
589−data AliasRouting = AllAliasRouting RoutingValue
590− | GranularAliasRouting (Maybe SearchAliasRouting) (Maybe IndexAliasRouting)
591− deriving (Show, Eq, Generic, Typeable)
593−newtype SearchAliasRouting = SearchAliasRouting (NonEmpty RoutingValue) deriving (Show, Eq, Generic, Typeable)
595−newtype IndexAliasRouting = IndexAliasRouting RoutingValue deriving (Show, Eq, Generic, ToJSON, FromJSON, Typeable)
597−newtype RoutingValue = RoutingValue { routingValue :: Text } deriving (Show, Eq, Generic, ToJSON, FromJSON, Typeable)
599−newtype IndexAliasesSummary = IndexAliasesSummary { indexAliasesSummary :: [IndexAliasSummary] } deriving (Show, Eq, Generic, Typeable)
601−{-| 'IndexAliasSummary' is a summary of an index alias configured for a server. -}
602−data IndexAliasSummary = IndexAliasSummary { indexAliasSummaryAlias :: IndexAlias
603− , indexAliasSummaryCreate :: IndexAliasCreate} deriving (Show, Eq, Generic, Typeable)
605−{-| 'DocVersion' is an integer version number for a document between 1
606−and 9.2e+18 used for <<https://www.elastic.co/guide/en/elasticsearch/guide/current/optimistic-concurrency-control.html optimistic concurrency control>>.
608−newtype DocVersion = DocVersion {
609− docVersionNumber :: Int
610− } deriving (Eq, Show, Generic, Ord, ToJSON)
612−-- | Smart constructor for in-range doc version
613−mkDocVersion :: Int -> Maybe DocVersion
615− | i >= (docVersionNumber minBound) && i <= (docVersionNumber maxBound) =
617− | otherwise = Nothing
620−{-| 'ExternalDocVersion' is a convenience wrapper if your code uses its
621−own version numbers instead of ones from ES.
623−newtype ExternalDocVersion = ExternalDocVersion DocVersion
624− deriving (Eq, Show, Generic, Ord, Bounded, Enum, ToJSON)
626−{-| 'VersionControl' is specified when indexing documents as a
627−optimistic concurrency control.
629−data VersionControl = NoVersionControl
630− -- ^ Don't send a version. This is a pure overwrite.
631− | InternalVersion DocVersion
632− -- ^ Use the default ES versioning scheme. Only
633− -- index the document if the version is the same
634− -- as the one specified. Only applicable to
635− -- updates, as you should be getting Version from
637− | ExternalGT ExternalDocVersion
638− -- ^ Use your own version numbering. Only index
639− -- the document if the version is strictly higher
640− -- OR the document doesn't exist. The given
641− -- version will be used as the new version number
642− -- for the stored document. N.B. All updates must
643− -- increment this number, meaning there is some
644− -- global, external ordering of updates.
645− | ExternalGTE ExternalDocVersion
646− -- ^ Use your own version numbering. Only index
647− -- the document if the version is equal or higher
648− -- than the stored version. Will succeed if there
649− -- is no existing document. The given version will
650− -- be used as the new version number for the
651− -- stored document. Use with care, as this could
652− -- result in data loss.
653− | ForceVersion ExternalDocVersion
654− -- ^ The document will always be indexed and the
655− -- given version will be the new version. This is
656− -- typically used for correcting errors. Use with
657− -- care, as this could result in data loss.
658− deriving (Show, Eq, Generic, Ord)
660−{-| 'DocumentParent' is used to specify a parent document.
662−newtype DocumentParent = DocumentParent DocId
663− deriving (Eq, Show, Generic, Typeable)
665−{-| 'IndexDocumentSettings' are special settings supplied when indexing
666−a document. For the best backwards compatiblity when new fields are
667−added, you should probably prefer to start with 'defaultIndexDocumentSettings'
669−data IndexDocumentSettings =
670− IndexDocumentSettings { idsVersionControl :: VersionControl
671− , idsParent :: Maybe DocumentParent
672− } deriving (Eq, Show, Generic, Typeable)
674−{-| Reasonable default settings. Chooses no version control and no parent.
676−defaultIndexDocumentSettings :: IndexDocumentSettings
677−defaultIndexDocumentSettings = IndexDocumentSettings NoVersionControl Nothing
679−{-| 'Sort' is a synonym for a list of 'SortSpec's. Sort behavior is order
680− dependent with later sorts acting as tie-breakers for earlier sorts.
682−type Sort = [SortSpec]
684−{-| The two main kinds of 'SortSpec' are 'DefaultSortSpec' and
685− 'GeoDistanceSortSpec'. The latter takes a 'SortOrder', 'GeoPoint', and
686− 'DistanceUnit' to express "nearness" to a single geographical point as a
689−<http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#search-request-sort>
691−data SortSpec = DefaultSortSpec DefaultSort
692− | GeoDistanceSortSpec SortOrder GeoPoint DistanceUnit deriving (Eq, Show, Generic, Typeable)
694−{-| 'DefaultSort' is usually the kind of 'SortSpec' you'll want. There's a
695− 'mkSort' convenience function for when you want to specify only the most
698−<http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#search-request-sort>
701− DefaultSort { sortFieldName :: FieldName
702− , sortOrder :: SortOrder
704− , ignoreUnmapped :: Bool
705− , sortMode :: Maybe SortMode
706− , missingSort :: Maybe Missing
707− , nestedFilter :: Maybe Filter } deriving (Eq, Show, Generic, Typeable)
709−{-| 'SortOrder' is 'Ascending' or 'Descending', as you might expect. These get
710− encoded into "asc" or "desc" when turned into JSON.
712−<http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#search-request-sort>
714−data SortOrder = Ascending
715− | Descending deriving (Eq, Show, Generic, Typeable)
717−{-| 'Missing' prescribes how to handle missing fields. A missing field can be
718− sorted last, first, or using a custom value as a substitute.
720−<http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#_missing_values>
722−data Missing = LastMissing
724− | CustomMissing Text deriving (Eq, Show, Generic, Typeable)
726−{-| 'SortMode' prescribes how to handle sorting array/multi-valued fields.
728−http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#_sort_mode_option
730−data SortMode = SortMin
733− | SortAvg deriving (Eq, Show, Generic, Typeable)
735−{-| 'mkSort' defaults everything but the 'FieldName' and the 'SortOrder' so
736− that you can concisely describe the usual kind of 'SortSpec's you want.
738−mkSort :: FieldName -> SortOrder -> DefaultSort
739−mkSort fieldName sOrder = DefaultSort fieldName sOrder False Nothing Nothing Nothing
741−{-| 'Cache' is for telling ES whether it should cache a 'Filter' not.
742− 'Query's cannot be cached.
744−type Cache = Bool -- caching on/off
745−defaultCache :: Cache
748−{-| 'PrefixValue' is used in 'PrefixQuery' as the main query component.
750−type PrefixValue = Text
752−{-| 'BooleanOperator' is the usual And/Or operators with an ES compatible
753− JSON encoding baked in. Used all over the place.
755−data BooleanOperator = And | Or deriving (Eq, Show, Generic, Typeable)
757−{-| 'ShardCount' is part of 'IndexSettings'
759−newtype ShardCount = ShardCount Int deriving (Eq, Show, Generic, ToJSON, Typeable)
761−{-| 'ReplicaCount' is part of 'IndexSettings'
763−newtype ReplicaCount = ReplicaCount Int deriving (Eq, Show, Generic, ToJSON, Typeable)
765−{-| 'IndexName' is used to describe which index to query/create/delete
767−newtype IndexName = IndexName Text deriving (Eq, Generic, Show, ToJSON, FromJSON, Typeable)
769−{-| 'TemplateName' is used to describe which template to query/create/delete
771−newtype TemplateName = TemplateName Text deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
773−{-| 'TemplatePattern' represents a pattern which is matched against index names
775−newtype TemplatePattern = TemplatePattern Text deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
777−{-| 'MappingName' is part of mappings which are how ES describes and schematizes
778− the data in the indices.
780−newtype MappingName = MappingName Text deriving (Eq, Generic, Show, ToJSON, FromJSON, Typeable)
782−{-| 'DocId' is a generic wrapper value for expressing unique Document IDs.
783− Can be set by the user or created by ES itself. Often used in client
784− functions for poking at specific documents.
786−newtype DocId = DocId Text deriving (Eq, Generic, Show, ToJSON, FromJSON, Typeable)
788−{-| 'QueryString' is used to wrap query text bodies, be they human written or not.
790−newtype QueryString = QueryString Text deriving (Eq, Generic, Show, ToJSON, FromJSON, Typeable)
792−{-| 'FieldName' is used all over the place wherever a specific field within
793− a document needs to be specified, usually in 'Query's or 'Filter's.
795−newtype FieldName = FieldName Text deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
798−{-| 'Script' is often used in place of 'FieldName' to specify more
409+ , AnalyzerDefinition(..)
799−complex ways of extracting a value from a document.
410+ , TokenizerDefinition(..)
800−-}
411+ , TokenFilterDefinition(..)
801−newtype Script = Script { scriptText :: Text } deriving (Eq, Show, Generic, Typeable)
412+ , CharFilterDefinition(..)
803−{-| 'CacheName' is used in 'RegexpFilter' for describing the
414+ , TokenChar(..)
804− 'CacheKey' keyed caching behavior.
415+ , Shingle(..)
805−-}
416+ , Language(..)
806−newtype CacheName = CacheName Text deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
417+ ) where
808−{-| 'CacheKey' is used in 'RegexpFilter' to key regex caching.
419+import Bloodhound.Import
810−newtype CacheKey =
421+import Database.Bloodhound.Internal.Aggregation
811− CacheKey Text deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
422+import Database.Bloodhound.Internal.Analysis
812−newtype Existence =
423+import Database.Bloodhound.Internal.Client
813− Existence Bool deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
424+import Database.Bloodhound.Internal.Highlight
814−newtype NullValue =
425+import Database.Bloodhound.Internal.Newtypes
815− NullValue Bool deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
426+import Database.Bloodhound.Internal.Query
816−newtype CutoffFrequency =
427+import Database.Bloodhound.Internal.Sort
817− CutoffFrequency Double deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
428+import Database.Bloodhound.Internal.Suggest
819− Analyzer Text deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
820−newtype MaxExpansions =
821− MaxExpansions Int deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
823−{-| 'Lenient', if set to true, will cause format based failures to be
824− ignored. I don't know what the bloody default is, Elasticsearch
825− documentation didn't say what it was. Let me know if you figure it out.
828− Lenient Bool deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
830− Tiebreaker Double deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
832− Boost Double deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
834− BoostTerms Double deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
836−{-| 'MinimumMatch' controls how many should clauses in the bool query should
837− match. Can be an absolute value (2) or a percentage (30%) or a
838− combination of both.
840−newtype MinimumMatch =
841− MinimumMatch Int deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
842−newtype DisableCoord =
843− DisableCoord Bool deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
844−newtype IgnoreTermFrequency =
845− IgnoreTermFrequency Bool deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
846−newtype MinimumTermFrequency =
847− MinimumTermFrequency Int deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
848−newtype MaxQueryTerms =
849− MaxQueryTerms Int deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
851− Fuzziness Double deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
853−{-| 'PrefixLength' is the prefix length used in queries, defaults to 0. -}
854−newtype PrefixLength =
855− PrefixLength Int deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
857− TypeName Text deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
858−newtype PercentMatch =
859− PercentMatch Double deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
861− StopWord Text deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
863− QueryPath Text deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
865−{-| Allowing a wildcard at the beginning of a word (eg "*ing") is particularly
866− heavy, because all terms in the index need to be examined, just in case
867− they match. Leading wildcards can be disabled by setting
868− 'AllowLeadingWildcard' to false. -}
869−newtype AllowLeadingWildcard =
870− AllowLeadingWildcard Bool deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
871−newtype LowercaseExpanded =
872− LowercaseExpanded Bool deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
873−newtype EnablePositionIncrements =
874− EnablePositionIncrements Bool deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
876−{-| By default, wildcard terms in a query are not analyzed.
877− Setting 'AnalyzeWildcard' to true enables best-effort analysis.
879−newtype AnalyzeWildcard = AnalyzeWildcard Bool deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
881−{-| 'GeneratePhraseQueries' defaults to false.
883−newtype GeneratePhraseQueries =
884− GeneratePhraseQueries Bool deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
886−{-| 'Locale' is used for string conversions - defaults to ROOT.
888−newtype Locale = Locale Text deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
889−newtype MaxWordLength = MaxWordLength Int deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
890−newtype MinWordLength = MinWordLength Int deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
892−{-| 'PhraseSlop' sets the default slop for phrases, 0 means exact
893− phrase matches. Default is 0.
895−newtype PhraseSlop = PhraseSlop Int deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
896−newtype MinDocFrequency = MinDocFrequency Int deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
897−newtype MaxDocFrequency = MaxDocFrequency Int deriving (Eq, Show, Generic, ToJSON, FromJSON, Typeable)
899−-- | Newtype wrapper to parse ES's concerning tendency to in some APIs return a floating point number of milliseconds since epoch ಠ_ಠ
900−newtype POSIXMS = POSIXMS { posixMS :: UTCTime }
902{-| 'unpackId' is a silly convenience function that gets used once.
430{-| 'unpackId' is a silly convenience function that gets used once.
@@ -905,8 +433,6 @@ unpackId :: DocId -> Text
905unpackId (DocId docId) = docId
433unpackId (DocId docId) = docId
907type TrackSortScores = Bool
435type TrackSortScores = Bool
908−newtype From = From Int deriving (Eq, Show, Generic, ToJSON)
909−newtype Size = Size Int deriving (Eq, Show, Generic, ToJSON)
911data Search = Search { queryBody :: Maybe Query
437data Search = Search { queryBody :: Maybe Query
912 , filterBody :: Maybe Filter
438 , filterBody :: Maybe Filter
@@ -918,537 +444,74 @@ data Search = Search { queryBody :: Maybe Query
918 , from :: From
444 , from :: From
919 , size :: Size
445 , size :: Size
920 , searchType :: SearchType
446 , searchType :: SearchType
447+ , searchAfterKey :: Maybe SearchAfterKey
921 , fields :: Maybe [FieldName]
448 , fields :: Maybe [FieldName]
449+ , scriptFields :: Maybe ScriptFields
922− , source :: Maybe Source } deriving (Eq, Show, Generic, Typeable)
450+ , source :: Maybe Source
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)
924data SearchType = SearchTypeQueryThenFetch
480data SearchType = SearchTypeQueryThenFetch
925 | SearchTypeDfsQueryThenFetch
481 | SearchTypeDfsQueryThenFetch
928− | SearchTypeQueryAndFetch
929− | SearchTypeDfsQueryAndFetch
930− deriving (Eq, Show, Generic, Typeable)
482+ deriving (Eq, Show)
932data Source =
484data Source =
934 | SourcePatterns PatternOrPatterns
486 | SourcePatterns PatternOrPatterns
935 | SourceIncludeExclude Include Exclude
487 | SourceIncludeExclude Include Exclude
936− deriving (Show, Eq, Generic, Typeable)
488+ deriving (Eq, Show)
938−data PatternOrPatterns = PopPattern Pattern
939− | PopPatterns [Pattern] deriving (Eq, Show, Generic, Typeable)
941−data Include = Include [Pattern] deriving (Eq, Show, Generic, Typeable)
942−data Exclude = Exclude [Pattern] deriving (Eq, Show, Generic, Typeable)
944−newtype Pattern = Pattern Text deriving (Eq, Show, Generic, Typeable)
946−data Highlights = Highlights { globalsettings :: Maybe HighlightSettings
947− , highlightFields :: [FieldHighlight]
948− } deriving (Show, Eq, Generic, Typeable)
950−data FieldHighlight = FieldHighlight FieldName (Maybe HighlightSettings)
951− deriving (Show, Eq, Generic, Typeable)
954−data HighlightSettings = Plain PlainHighlight
955− | Postings PostingsHighlight
956− | FastVector FastVectorHighlight
957− deriving (Show, Eq, Generic, Typeable)
958−data PlainHighlight =
959− PlainHighlight { plainCommon :: Maybe CommonHighlight
960− , plainNonPost :: Maybe NonPostings } deriving (Show, Eq, Generic, Typeable)
962− -- This requires that index_options are set to 'offset' in the mapping.
963−data PostingsHighlight = PostingsHighlight (Maybe CommonHighlight) deriving (Show, Eq, Generic, Typeable)
965−-- This requires that term_vector is set to 'with_positions_offsets' in the mapping.
966−data FastVectorHighlight =
967− FastVectorHighlight { fvCommon :: Maybe CommonHighlight
968− , fvNonPostSettings :: Maybe NonPostings
969− , boundaryChars :: Maybe Text
970− , boundaryMaxScan :: Maybe Int
971− , fragmentOffset :: Maybe Int
972− , matchedFields :: [Text]
973− , phraseLimit :: Maybe Int
974− } deriving (Show, Eq, Generic, Typeable)
976−data CommonHighlight =
977− CommonHighlight { order :: Maybe Text
978− , forceSource :: Maybe Bool
979− , tag :: Maybe HighlightTag
980− , encoder :: Maybe HighlightEncoder
981− , noMatchSize :: Maybe Int
982− , highlightQuery :: Maybe Query
983− , requireFieldMatch :: Maybe Bool
984− } deriving (Show, Eq, Generic, Typeable)
986−-- Settings that are only applicable to FastVector and Plain highlighters.
988− NonPostings { fragmentSize :: Maybe Int
989− , numberOfFragments :: Maybe Int} deriving (Show, Eq, Generic, Typeable)
991−data HighlightEncoder = DefaultEncoder
993− deriving (Show, Eq, Generic, Typeable)
995−-- NOTE: Should the tags use some kind of HTML type, rather than Text?
996−data HighlightTag = TagSchema Text
997− | CustomTags ([Text], [Text]) -- Only uses more than the first value in the lists if fvh
998− deriving (Show, Eq, Generic, Typeable)
1002− TermQuery Term (Maybe Boost)
1003− | TermsQuery Text (NonEmpty Text)
1004− | QueryMatchQuery MatchQuery
1005− | QueryMultiMatchQuery MultiMatchQuery
1006− | QueryBoolQuery BoolQuery
1007− | QueryBoostingQuery BoostingQuery
1008− | QueryCommonTermsQuery CommonTermsQuery
1009− | ConstantScoreFilter Filter Boost
1010− | ConstantScoreQuery Query Boost
1011− | QueryDisMaxQuery DisMaxQuery
1012− | QueryFilteredQuery FilteredQuery
1013− | QueryFuzzyLikeThisQuery FuzzyLikeThisQuery
1014− | QueryFuzzyLikeFieldQuery FuzzyLikeFieldQuery
1015− | QueryFuzzyQuery FuzzyQuery
1016− | QueryHasChildQuery HasChildQuery
1017− | QueryHasParentQuery HasParentQuery
1018− | IdsQuery MappingName [DocId]
1019− | QueryIndicesQuery IndicesQuery
1020− | MatchAllQuery (Maybe Boost)
1021− | QueryMoreLikeThisQuery MoreLikeThisQuery
1022− | QueryMoreLikeThisFieldQuery MoreLikeThisFieldQuery
1023− | QueryNestedQuery NestedQuery
1024− | QueryPrefixQuery PrefixQuery
1025− | QueryQueryStringQuery QueryStringQuery
1026− | QuerySimpleQueryStringQuery SimpleQueryStringQuery
1027− | QueryRangeQuery RangeQuery
1028− | QueryRegexpQuery RegexpQuery
1029− deriving (Eq, Show, Generic, Typeable)
1032− RegexpQuery { regexpQueryField :: FieldName
1033− , regexpQuery :: Regexp
1034− , regexpQueryFlags :: RegexpFlags
1035− , regexpQueryBoost :: Maybe Boost
1036− } deriving (Eq, Show, Generic, Typeable)
1039− RangeQuery { rangeQueryField :: FieldName
1040− , rangeQueryRange :: RangeValue
1041− , rangeQueryBoost :: Boost } deriving (Eq, Show, Generic, Typeable)
1043−mkRangeQuery :: FieldName -> RangeValue -> RangeQuery
1044−mkRangeQuery f r = RangeQuery f r (Boost 1.0)
1046−data SimpleQueryStringQuery =
1047− SimpleQueryStringQuery
1048− { simpleQueryStringQuery :: QueryString
1049− , simpleQueryStringField :: Maybe FieldOrFields
1050− , simpleQueryStringOperator :: Maybe BooleanOperator
1051− , simpleQueryStringAnalyzer :: Maybe Analyzer
1052− , simpleQueryStringFlags :: Maybe (NonEmpty SimpleQueryFlag)
1053− , simpleQueryStringLowercaseExpanded :: Maybe LowercaseExpanded
1054− , simpleQueryStringLocale :: Maybe Locale
1055− } deriving (Eq, Show, Generic, Typeable)
1057−data SimpleQueryFlag =
1062− | SimpleQueryPrefix
1063− | SimpleQueryPhrase
1064− | SimpleQueryPrecedence
1065− | SimpleQueryEscape
1066− | SimpleQueryWhitespace
1069− | SimpleQuerySlop deriving (Eq, Show, Generic, Typeable)
1071−-- use_dis_max and tie_breaker when fields are plural?
1072−data QueryStringQuery =
1074− { queryStringQuery :: QueryString
1075− , queryStringDefaultField :: Maybe FieldName
1076− , queryStringOperator :: Maybe BooleanOperator
1077− , queryStringAnalyzer :: Maybe Analyzer
1078− , queryStringAllowLeadingWildcard :: Maybe AllowLeadingWildcard
1079− , queryStringLowercaseExpanded :: Maybe LowercaseExpanded
1080− , queryStringEnablePositionIncrements :: Maybe EnablePositionIncrements
1081− , queryStringFuzzyMaxExpansions :: Maybe MaxExpansions
1082− , queryStringFuzziness :: Maybe Fuzziness
1083− , queryStringFuzzyPrefixLength :: Maybe PrefixLength
1084− , queryStringPhraseSlop :: Maybe PhraseSlop
1085− , queryStringBoost :: Maybe Boost
1086− , queryStringAnalyzeWildcard :: Maybe AnalyzeWildcard
1087− , queryStringGeneratePhraseQueries :: Maybe GeneratePhraseQueries
1088− , queryStringMinimumShouldMatch :: Maybe MinimumMatch
1089− , queryStringLenient :: Maybe Lenient
1090− , queryStringLocale :: Maybe Locale
1091− } deriving (Eq, Show, Generic, Typeable)
1093−mkQueryStringQuery :: QueryString -> QueryStringQuery
1094−mkQueryStringQuery qs =
1095− QueryStringQuery qs Nothing Nothing
1096− Nothing Nothing Nothing Nothing
1097− Nothing Nothing Nothing Nothing
1098− Nothing Nothing Nothing Nothing
1101−data FieldOrFields = FofField FieldName
1102− | FofFields (NonEmpty FieldName) deriving (Eq, Show, Generic, Typeable)
1106− { prefixQueryField :: FieldName
1107− , prefixQueryPrefixValue :: Text
1108− , prefixQueryBoost :: Maybe Boost } deriving (Eq, Show, Generic, Typeable)
1112− { nestedQueryPath :: QueryPath
1113− , nestedQueryScoreType :: ScoreType
1114− , nestedQuery :: Query } deriving (Eq, Show, Generic, Typeable)
1116−data MoreLikeThisFieldQuery =
1117− MoreLikeThisFieldQuery
1118− { moreLikeThisFieldText :: Text
1119− , moreLikeThisFieldFields :: FieldName
1120− -- default 0.3 (30%)
1121− , moreLikeThisFieldPercentMatch :: Maybe PercentMatch
1122− , moreLikeThisFieldMinimumTermFreq :: Maybe MinimumTermFrequency
1123− , moreLikeThisFieldMaxQueryTerms :: Maybe MaxQueryTerms
1124− , moreLikeThisFieldStopWords :: Maybe (NonEmpty StopWord)
1125− , moreLikeThisFieldMinDocFrequency :: Maybe MinDocFrequency
1126− , moreLikeThisFieldMaxDocFrequency :: Maybe MaxDocFrequency
1127− , moreLikeThisFieldMinWordLength :: Maybe MinWordLength
1128− , moreLikeThisFieldMaxWordLength :: Maybe MaxWordLength
1129− , moreLikeThisFieldBoostTerms :: Maybe BoostTerms
1130− , moreLikeThisFieldBoost :: Maybe Boost
1131− , moreLikeThisFieldAnalyzer :: Maybe Analyzer
1132− } deriving (Eq, Show, Generic, Typeable)
1134−data MoreLikeThisQuery =
1136− { moreLikeThisText :: Text
1137− , moreLikeThisFields :: Maybe (NonEmpty FieldName)
1138− -- default 0.3 (30%)
1139− , moreLikeThisPercentMatch :: Maybe PercentMatch
1140− , moreLikeThisMinimumTermFreq :: Maybe MinimumTermFrequency
1141− , moreLikeThisMaxQueryTerms :: Maybe MaxQueryTerms
1142− , moreLikeThisStopWords :: Maybe (NonEmpty StopWord)
1143− , moreLikeThisMinDocFrequency :: Maybe MinDocFrequency
1144− , moreLikeThisMaxDocFrequency :: Maybe MaxDocFrequency
1145− , moreLikeThisMinWordLength :: Maybe MinWordLength
1146− , moreLikeThisMaxWordLength :: Maybe MaxWordLength
1147− , moreLikeThisBoostTerms :: Maybe BoostTerms
1148− , moreLikeThisBoost :: Maybe Boost
1149− , moreLikeThisAnalyzer :: Maybe Analyzer
1150− } deriving (Eq, Show, Generic, Typeable)
1154− { indicesQueryIndices :: [IndexName]
1155− , indicesQuery :: Query
1157− , indicesQueryNoMatch :: Maybe Query } deriving (Eq, Show, Generic, Typeable)
1159−data HasParentQuery =
1161− { hasParentQueryType :: TypeName
1162− , hasParentQuery :: Query
1163− , hasParentQueryScoreType :: Maybe ScoreType } deriving (Eq, Show, Generic, Typeable)
1165−data HasChildQuery =
1167− { hasChildQueryType :: TypeName
1168− , hasChildQuery :: Query
1169− , hasChildQueryScoreType :: Maybe ScoreType } deriving (Eq, Show, Generic, Typeable)
1175− | ScoreTypeNone deriving (Eq, Show, Generic, Typeable)
1178− FuzzyQuery { fuzzyQueryField :: FieldName
1179− , fuzzyQueryValue :: Text
1180− , fuzzyQueryPrefixLength :: PrefixLength
1181− , fuzzyQueryMaxExpansions :: MaxExpansions
1182− , fuzzyQueryFuzziness :: Fuzziness
1183− , fuzzyQueryBoost :: Maybe Boost
1184− } deriving (Eq, Show, Generic, Typeable)
1186−data FuzzyLikeFieldQuery =
1187− FuzzyLikeFieldQuery
1188− { fuzzyLikeField :: FieldName
1189− -- anaphora is good for the soul.
1190− , fuzzyLikeFieldText :: Text
1191− , fuzzyLikeFieldMaxQueryTerms :: MaxQueryTerms
1192− , fuzzyLikeFieldIgnoreTermFrequency :: IgnoreTermFrequency
1193− , fuzzyLikeFieldFuzziness :: Fuzziness
1194− , fuzzyLikeFieldPrefixLength :: PrefixLength
1195− , fuzzyLikeFieldBoost :: Boost
1196− , fuzzyLikeFieldAnalyzer :: Maybe Analyzer
1197− } deriving (Eq, Show, Generic, Typeable)
1199−data FuzzyLikeThisQuery =
1201− { fuzzyLikeFields :: [FieldName]
1202− , fuzzyLikeText :: Text
1203− , fuzzyLikeMaxQueryTerms :: MaxQueryTerms
1204− , fuzzyLikeIgnoreTermFrequency :: IgnoreTermFrequency
1205− , fuzzyLikeFuzziness :: Fuzziness
1206− , fuzzyLikePrefixLength :: PrefixLength
1207− , fuzzyLikeBoost :: Boost
1208− , fuzzyLikeAnalyzer :: Maybe Analyzer
1209− } deriving (Eq, Show, Generic, Typeable)
1211−data FilteredQuery =
1213− { filteredQuery :: Query
1214− , filteredFilter :: Filter } deriving (Eq, Show, Generic, Typeable)
1217− DisMaxQuery { disMaxQueries :: [Query]
1219− , disMaxTiebreaker :: Tiebreaker
1220− , disMaxBoost :: Maybe Boost
1221− } deriving (Eq, Show, Generic, Typeable)
1224− MatchQuery { matchQueryField :: FieldName
1225− , matchQueryQueryString :: QueryString
1226− , matchQueryOperator :: BooleanOperator
1227− , matchQueryZeroTerms :: ZeroTermsQuery
1228− , matchQueryCutoffFrequency :: Maybe CutoffFrequency
1229− , matchQueryMatchType :: Maybe MatchQueryType
1230− , matchQueryAnalyzer :: Maybe Analyzer
1231− , matchQueryMaxExpansions :: Maybe MaxExpansions
1232− , matchQueryLenient :: Maybe Lenient
1233− , matchQueryBoost :: Maybe Boost } deriving (Eq, Show, Generic, Typeable)
1235−{-| 'mkMatchQuery' is a convenience function that defaults the less common parameters,
1236− enabling you to provide only the 'FieldName' and 'QueryString' to make a 'MatchQuery'
1238−mkMatchQuery :: FieldName -> QueryString -> MatchQuery
1239−mkMatchQuery field query = MatchQuery field query Or ZeroTermsNone Nothing Nothing Nothing Nothing Nothing Nothing
1241−data MatchQueryType =
490+instance ToJSON Source where
1242− MatchPhrase
491+ toJSON NoSource = toJSON False
1243− | MatchPhrasePrefix deriving (Eq, Show, Generic, Typeable)
492+ toJSON (SourcePatterns patterns) = toJSON patterns
1244−
493+ toJSON (SourceIncludeExclude incl excl) = object [ "includes" .= incl, "excludes" .= excl ]
1245−data MultiMatchQuery =
1246− MultiMatchQuery { multiMatchQueryFields :: [FieldName]
1247− , multiMatchQueryString :: QueryString
1248− , multiMatchQueryOperator :: BooleanOperator
1249− , multiMatchQueryZeroTerms :: ZeroTermsQuery
1250− , multiMatchQueryTiebreaker :: Maybe Tiebreaker
1251− , multiMatchQueryType :: Maybe MultiMatchQueryType
1252− , multiMatchQueryCutoffFrequency :: Maybe CutoffFrequency
1253− , multiMatchQueryAnalyzer :: Maybe Analyzer
1254− , multiMatchQueryMaxExpansions :: Maybe MaxExpansions
1255− , multiMatchQueryLenient :: Maybe Lenient } deriving (Eq, Show, Generic, Typeable)
1257−{-| 'mkMultiMatchQuery' is a convenience function that defaults the less common parameters,
1258− enabling you to provide only the list of 'FieldName's and 'QueryString' to
1259− make a 'MultiMatchQuery'.
1262−mkMultiMatchQuery :: [FieldName] -> QueryString -> MultiMatchQuery
1263−mkMultiMatchQuery matchFields query =
1264− MultiMatchQuery matchFields query
1265− Or ZeroTermsNone Nothing Nothing Nothing Nothing Nothing Nothing
1267−data MultiMatchQueryType =
1268− MultiMatchBestFields
1269− | MultiMatchMostFields
1270− | MultiMatchCrossFields
1272− | MultiMatchPhrasePrefix deriving (Eq, Show, Generic, Typeable)
1275− BoolQuery { boolQueryMustMatch :: [Query]
1276− , boolQueryMustNotMatch :: [Query]
1277− , boolQueryShouldMatch :: [Query]
1278− , boolQueryMinimumShouldMatch :: Maybe MinimumMatch
1279− , boolQueryBoost :: Maybe Boost
1280− , boolQueryDisableCoord :: Maybe DisableCoord
1281− } deriving (Eq, Show, Generic, Typeable)
1283−mkBoolQuery :: [Query] -> [Query] -> [Query] -> BoolQuery
1284−mkBoolQuery must mustNot should =
1285− BoolQuery must mustNot should Nothing Nothing Nothing
1287−data BoostingQuery =
1288− BoostingQuery { positiveQuery :: Query
1289− , negativeQuery :: Query
1290− , negativeBoost :: Boost } deriving (Eq, Show, Generic, Typeable)
1292−data CommonTermsQuery =
1293− CommonTermsQuery { commonField :: FieldName
1294− , commonQuery :: QueryString
1295− , commonCutoffFrequency :: CutoffFrequency
1296− , commonLowFreqOperator :: BooleanOperator
1297− , commonHighFreqOperator :: BooleanOperator
1298− , commonMinimumShouldMatch :: Maybe CommonMinimumMatch
1299− , commonBoost :: Maybe Boost
1300− , commonAnalyzer :: Maybe Analyzer
1301− , commonDisableCoord :: Maybe DisableCoord
1302− } deriving (Eq, Show, Generic, Typeable)
1304−data CommonMinimumMatch =
1305− CommonMinimumMatchHighLow MinimumMatchHighLow
1306− | CommonMinimumMatch MinimumMatch
1307− deriving (Eq, Show, Generic, Typeable)
1309−data MinimumMatchHighLow =
1310− MinimumMatchHighLow { lowFreq :: MinimumMatch
1311− , highFreq :: MinimumMatch } deriving (Eq, Show, Generic, Typeable)
1313−data Filter = AndFilter [Filter] Cache
1314− | OrFilter [Filter] Cache
1315− | NotFilter Filter Cache
1317− | BoolFilter BoolMatch
1318− | ExistsFilter FieldName -- always cached
1319− | GeoBoundingBoxFilter GeoBoundingBoxConstraint
1320− | GeoDistanceFilter GeoPoint Distance DistanceType OptimizeBbox Cache
1321− | GeoDistanceRangeFilter GeoPoint DistanceRange
1322− | GeoPolygonFilter FieldName [LatLon]
1323− | IdsFilter MappingName [DocId]
1325− | MissingFilter FieldName Existence NullValue
1326− | PrefixFilter FieldName PrefixValue Cache
1327− | QueryFilter Query Cache
1328− | RangeFilter FieldName RangeValue RangeExecution Cache
1329− | RegexpFilter FieldName Regexp RegexpFlags CacheName Cache CacheKey
1330− | TermFilter Term Cache
1331− deriving (Eq, Show, Generic, Typeable)
1333−data ZeroTermsQuery = ZeroTermsNone
1334− | ZeroTermsAll deriving (Eq, Show, Generic, Typeable)
1336−data RangeExecution = RangeExecutionIndex
1337− | RangeExecutionFielddata deriving (Eq, Show, Generic, Typeable)
1339−newtype Regexp = Regexp Text deriving (Eq, Show, Generic, Typeable, FromJSON)
1341−data RegexpFlags = AllRegexpFlags
1343− | SomeRegexpFlags (NonEmpty RegexpFlag) deriving (Eq, Show, Generic, Typeable)
1345−data RegexpFlag = AnyString
1350− | Interval deriving (Eq, Show, Generic, Typeable)
1352−newtype LessThan = LessThan Double deriving (Eq, Show, Generic, Typeable)
1353−newtype LessThanEq = LessThanEq Double deriving (Eq, Show, Generic, Typeable)
1354−newtype GreaterThan = GreaterThan Double deriving (Eq, Show, Generic, Typeable)
1355−newtype GreaterThanEq = GreaterThanEq Double deriving (Eq, Show, Generic, Typeable)
1357−newtype LessThanD = LessThanD UTCTime deriving (Eq, Show, Generic, Typeable)
1358−newtype LessThanEqD = LessThanEqD UTCTime deriving (Eq, Show, Generic, Typeable)
1359−newtype GreaterThanD = GreaterThanD UTCTime deriving (Eq, Show, Generic, Typeable)
1360−newtype GreaterThanEqD = GreaterThanEqD UTCTime deriving (Eq, Show, Generic, Typeable)
1362−data RangeValue = RangeDateLte LessThanEqD
1363− | RangeDateLt LessThanD
1364− | RangeDateGte GreaterThanEqD
1365− | RangeDateGt GreaterThanD
1366− | RangeDateGtLt GreaterThanD LessThanD
1367− | RangeDateGteLte GreaterThanEqD LessThanEqD
1368− | RangeDateGteLt GreaterThanEqD LessThanD
1369− | RangeDateGtLte GreaterThanD LessThanEqD
1370− | RangeDoubleLte LessThanEq
1371− | RangeDoubleLt LessThan
1372− | RangeDoubleGte GreaterThanEq
1373− | RangeDoubleGt GreaterThan
1374− | RangeDoubleGtLt GreaterThan LessThan
1375− | RangeDoubleGteLte GreaterThanEq LessThanEq
1376− | RangeDoubleGteLt GreaterThanEq LessThan
1377− | RangeDoubleGtLte GreaterThan LessThanEq
1378− deriving (Eq, Show, Generic, Typeable)
1380−rangeValueToPair :: RangeValue -> [Pair]
1381−rangeValueToPair rv = case rv of
1382− RangeDateLte (LessThanEqD t) -> ["lte" .= t]
1383− RangeDateGte (GreaterThanEqD t) -> ["gte" .= t]
1384− RangeDateLt (LessThanD t) -> ["lt" .= t]
1385− RangeDateGt (GreaterThanD t) -> ["gt" .= t]
1386− RangeDateGteLte (GreaterThanEqD l) (LessThanEqD g) -> ["gte" .= l, "lte" .= g]
1387− RangeDateGtLte (GreaterThanD l) (LessThanEqD g) -> ["gt" .= l, "lte" .= g]
1388− RangeDateGteLt (GreaterThanEqD l) (LessThanD g) -> ["gte" .= l, "lt" .= g]
1389− RangeDateGtLt (GreaterThanD l) (LessThanD g) -> ["gt" .= l, "lt" .= g]
1390− RangeDoubleLte (LessThanEq t) -> ["lte" .= t]
1391− RangeDoubleGte (GreaterThanEq t) -> ["gte" .= t]
1392− RangeDoubleLt (LessThan t) -> ["lt" .= t]
1393− RangeDoubleGt (GreaterThan t) -> ["gt" .= t]
1394− RangeDoubleGteLte (GreaterThanEq l) (LessThanEq g) -> ["gte" .= l, "lte" .= g]
1395− RangeDoubleGtLte (GreaterThan l) (LessThanEq g) -> ["gt" .= l, "lte" .= g]
1396− RangeDoubleGteLt (GreaterThanEq l) (LessThan g) -> ["gte" .= l, "lt" .= g]
1397− RangeDoubleGtLt (GreaterThan l) (LessThan g) -> ["gt" .= l, "lt" .= g]
1399−data Term = Term { termField :: Text
1400− , termValue :: Text } deriving (Eq, Show, Generic, Typeable)
1402−data BoolMatch = MustMatch Term Cache
1403− | MustNotMatch Term Cache
1404− | ShouldMatch [Term] Cache deriving (Eq, Show, Generic, Typeable)
1406−-- "memory" or "indexed"
1407−data GeoFilterType = GeoFilterMemory
1408− | GeoFilterIndexed deriving (Eq, Show, Generic, Typeable)
1410−data LatLon = LatLon { lat :: Double
1411− , lon :: Double } deriving (Eq, Show, Generic, Typeable)
1413−data GeoBoundingBox =
1414− GeoBoundingBox { topLeft :: LatLon
1415− , bottomRight :: LatLon } deriving (Eq, Show, Generic, Typeable)
1417−data GeoBoundingBoxConstraint =
495+data PatternOrPatterns = PopPattern Pattern
1418− GeoBoundingBoxConstraint { geoBBField :: FieldName
1419− , constraintBox :: GeoBoundingBox
1420− , bbConstraintcache :: Cache
1421− , geoType :: GeoFilterType
1422− } deriving (Eq, Show, Generic, Typeable)
496+ | PopPatterns [Pattern] deriving (Eq, Read, Show)
1424−data GeoPoint =
498+instance ToJSON PatternOrPatterns where
1425− GeoPoint { geoField :: FieldName
499+ toJSON (PopPattern pattern) = toJSON pattern
1426− , latLon :: LatLon} deriving (Eq, Show, Generic, Typeable)
500+ toJSON (PopPatterns patterns) = toJSON patterns
1428−data DistanceUnit = Miles
502+data Include = Include [Pattern] deriving (Eq, Read, Show)
1429− | Yards
503+data Exclude = Exclude [Pattern] deriving (Eq, Read, Show)
1436− | NauticalMiles deriving (Eq, Show, Generic, Typeable)
1438−data DistanceType = Arc
505+instance ToJSON Include where
1439− | SloppyArc -- doesn't exist <1.0
506+ toJSON (Include patterns) = toJSON patterns
1440− | Plane deriving (Eq, Show, Generic, Typeable)
1442−data OptimizeBbox = OptimizeGeoFilterType GeoFilterType
508+instance ToJSON Exclude where
1443− | NoOptimizeBbox deriving (Eq, Show, Generic, Typeable)
509+ toJSON (Exclude patterns) = toJSON patterns
1445−data Distance =
511+newtype Pattern = Pattern Text deriving (Eq, Read, Show)
1446− Distance { coefficient :: Double
1447− , unit :: DistanceUnit } deriving (Eq, Show, Generic, Typeable)
1449−data DistanceRange =
513+instance ToJSON Pattern where
1450− DistanceRange { distanceFrom :: Distance
514+ toJSON (Pattern pattern) = toJSON pattern
1451− , distanceTo :: Distance } deriving (Eq, Show, Generic, Typeable)
1453data SearchResult a =
516data SearchResult a =
1454 SearchResult { took :: Int
517 SearchResult { took :: Int
@@ -1456,2030 +519,12 @@ data SearchResult a =
1456 , shards :: ShardResult
519 , shards :: ShardResult
1457 , searchHits :: SearchHits a
520 , searchHits :: SearchHits a
1458 , aggregations :: Maybe AggregationResults
521 , aggregations :: Maybe AggregationResults
1459− , scrollId :: Maybe ScrollId } deriving (Eq, Show, Generic, Typeable)
522+ , scrollId :: Maybe ScrollId
1460−
523+ -- ^ Only one Suggestion request / response per
1461−newtype ScrollId = ScrollId Text deriving (Eq, Show, Generic, Ord, ToJSON, FromJSON)
524+ -- Search is supported.
1462−
525+ , suggest :: Maybe NamedSuggestionResponse
1463−type Score = Maybe Double
526+ }
1464−
527+ deriving (Eq, Show)
1466− SearchHits { hitsTotal :: Int
1467− , maxScore :: Score
1468− , hits :: [Hit a] } deriving (Eq, Show, Generic, Typeable)
1471−instance Monoid (SearchHits a) where
1472− mempty = SearchHits 0 Nothing mempty
1473− mappend (SearchHits ta ma ha) (SearchHits tb mb hb) =
1474− SearchHits (ta + tb) (max ma mb) (ha <> hb)
1478− Hit { hitIndex :: IndexName
1479− , hitType :: MappingName
1480− , hitDocId :: DocId
1481− , hitScore :: Score
1482− , hitSource :: Maybe a
1483− , hitHighlight :: Maybe HitHighlight } deriving (Eq, Show, Generic, Typeable)
1486− ShardResult { shardTotal :: Int
1487− , shardsSuccessful :: Int
1488− , shardsFailed :: Int } deriving (Eq, Show, Generic, Typeable)
1490−type HitHighlight = M.Map Text [Text]
1492−showText :: Show a => a -> Text
1493−showText = T.pack . show
1495−readMay :: Read a => String -> Maybe a
1496−readMay s = case reads s of
1497− (a, ""):_ -> Just a
1500−parseReadText :: Read a => Text -> Parser a
1501−parseReadText = maybe mzero return . readMay . T.unpack
1503−type Aggregations = M.Map Text Aggregation
1505−emptyAggregations :: Aggregations
1506−emptyAggregations = M.empty
1508−mkAggregations :: Text -> Aggregation -> Aggregations
1509−mkAggregations name aggregation = M.insert name aggregation emptyAggregations
1511−data TermOrder = TermOrder{ termSortField :: Text
1512− , termSortOrder :: SortOrder } deriving (Eq, Show, Generic, Typeable)
1514−data TermInclusion = TermInclusion Text
1515− | TermPattern Text Text deriving (Eq, Show, Generic, Typeable)
1517−data CollectionMode = BreadthFirst
1518− | DepthFirst deriving (Eq, Show, Generic, Typeable)
1520−data ExecutionHint = Ordinals
1522− | GlobalOrdinalsHash
1523− | GlobalOrdinalsLowCardinality
1524− | Map deriving (Eq, Show, Generic, Typeable)
1526−data TimeInterval = Weeks
1530− | Seconds deriving (Eq)
1532−data Interval = Year
1540− | FractionalInterval Float TimeInterval deriving (Eq, Show, Generic, Typeable)
1542−data Aggregation = TermsAgg TermsAggregation
1543− | DateHistogramAgg DateHistogramAggregation
1544− | ValueCountAgg ValueCountAggregation
1545− | FilterAgg FilterAggregation
1546− | DateRangeAgg DateRangeAggregation deriving (Eq, Show, Generic, Typeable)
1549−data TermsAggregation = TermsAggregation { term :: Either Text Text
1550− , termInclude :: Maybe TermInclusion
1551− , termExclude :: Maybe TermInclusion
1552− , termOrder :: Maybe TermOrder
1553− , termMinDocCount :: Maybe Int
1554− , termSize :: Maybe Int
1555− , termShardSize :: Maybe Int
1556− , termCollectMode :: Maybe CollectionMode
1557− , termExecutionHint :: Maybe ExecutionHint
1558− , termAggs :: Maybe Aggregations
1559− } deriving (Eq, Show, Generic, Typeable)
1561−data DateHistogramAggregation = DateHistogramAggregation { dateField :: FieldName
1562− , dateInterval :: Interval
1563− , dateFormat :: Maybe Text
1564− -- pre and post deprecated in 1.5
1565− , datePreZone :: Maybe Text
1566− , datePostZone :: Maybe Text
1567− , datePreOffset :: Maybe Text
1568− , datePostOffset :: Maybe Text
1569− , dateAggs :: Maybe Aggregations
1570− } deriving (Eq, Show, Generic, Typeable)
1573−data DateRangeAggregation = DateRangeAggregation { draField :: FieldName
1574− , draFormat :: Maybe Text
1575− , draRanges :: NonEmpty DateRangeAggRange
1576− } deriving (Eq, Show, Generic, Typeable)
1578−data DateRangeAggRange = DateRangeFrom DateMathExpr
1579− | DateRangeTo DateMathExpr
1580− | DateRangeFromAndTo DateMathExpr DateMathExpr deriving (Eq, Show, Generic, Typeable)
1582−-- | See <https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#date-math> for more information.
1583−data DateMathExpr = DateMathExpr DateMathAnchor [DateMathModifier] deriving (Eq, Show, Generic, Typeable)
1586−-- | Starting point for a date range. This along with the 'DateMathModifiers' gets you the date ES will start from.
1587−data DateMathAnchor = DMNow
1588− | DMDate Day deriving (Eq, Show, Generic, Typeable)
1590−data DateMathModifier = AddTime Int DateMathUnit
1591− | SubtractTime Int DateMathUnit
1592− | RoundDownTo DateMathUnit deriving (Eq, Show, Generic, Typeable)
1594−data DateMathUnit = DMYear
1600− | DMSecond deriving (Eq, Show, Generic, Typeable)
1602−-- | See <https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-valuecount-aggregation.html> for more information.
1603−data ValueCountAggregation = FieldValueCount FieldName
1604− | ScriptValueCount Script deriving (Eq, Show, Generic, Typeable)
1606−-- | 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.
1607−data FilterAggregation = FilterAggregation { faFilter :: Filter
1608− , faAggs :: Maybe Aggregations} deriving (Eq, Show, Generic, Typeable)
1610−mkTermsAggregation :: Text -> TermsAggregation
1611−mkTermsAggregation t = TermsAggregation (Left t) Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing
1613−mkTermsScriptAggregation :: Text -> TermsAggregation
1614−mkTermsScriptAggregation t = TermsAggregation (Right t) Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing
1616−mkDateHistogram :: FieldName -> Interval -> DateHistogramAggregation
1617−mkDateHistogram t i = DateHistogramAggregation t i Nothing Nothing Nothing Nothing Nothing Nothing
1619−instance ToJSON Version where
1620− toJSON Version {..} = object ["number" .= number
1621− ,"build_hash" .= build_hash
1622− ,"build_timestamp" .= build_timestamp
1623− ,"build_snapshot" .= build_snapshot
1624− ,"lucene_version" .= lucene_version]
1626−instance FromJSON Version where
1627− parseJSON = withObject "Version" parse
1628− where parse o = Version
1630− <*> o .: "build_hash"
1631− <*> o .: "build_timestamp"
1632− <*> o .: "build_snapshot"
1633− <*> o .: "lucene_version"
1635−instance ToJSON TermOrder where
1636− toJSON (TermOrder termSortField termSortOrder) = object [termSortField .= termSortOrder]
1638−instance ToJSON TermInclusion where
1639− toJSON (TermInclusion x) = toJSON x
1640− toJSON (TermPattern pattern flags) = omitNulls [ "pattern" .= pattern,
1643−instance ToJSON CollectionMode where
1644− toJSON BreadthFirst = "breadth_first"
1645− toJSON DepthFirst = "depth_first"
1647−instance ToJSON ExecutionHint where
1648− toJSON Ordinals = "ordinals"
1649− toJSON GlobalOrdinals = "global_ordinals"
1650− toJSON GlobalOrdinalsHash = "global_ordinals_hash"
1651− toJSON GlobalOrdinalsLowCardinality = "global_ordinals_low_cardinality"
1654−instance ToJSON Interval where
1655− toJSON Year = "year"
1656− toJSON Quarter = "quarter"
1657− toJSON Month = "month"
1658− toJSON Week = "week"
1660− toJSON Hour = "hour"
1661− toJSON Minute = "minute"
1662− toJSON Second = "second"
1663− toJSON (FractionalInterval fraction interval) = toJSON $ show fraction ++ show interval
1665−instance Show TimeInterval where
1672−instance ToJSON Aggregation where
1673− toJSON (TermsAgg (TermsAggregation term include exclude order minDocCount size shardSize collectMode executionHint termAggs)) =
1674− omitNulls ["terms" .= omitNulls [ toJSON' term,
1675− "include" .= include,
1676− "exclude" .= exclude,
1678− "min_doc_count" .= minDocCount,
1680− "shard_size" .= shardSize,
1681− "collect_mode" .= collectMode,
1682− "execution_hint" .= executionHint
1684− "aggs" .= termAggs ]
1686− toJSON' x = case x of { Left y -> "field" .= y; Right y -> "script" .= y }
1688− toJSON (DateHistogramAgg (DateHistogramAggregation field interval format preZone postZone preOffset postOffset dateHistoAggs)) =
1689− omitNulls ["date_histogram" .= omitNulls [ "field" .= field,
1690− "interval" .= interval,
1691− "format" .= format,
1692− "pre_zone" .= preZone,
1693− "post_zone" .= postZone,
1694− "pre_offset" .= preOffset,
1695− "post_offset" .= postOffset
1697− "aggs" .= dateHistoAggs ]
1698− toJSON (ValueCountAgg a) = object ["value_count" .= v]
1699− where v = case a of
1700− (FieldValueCount (FieldName n)) -> object ["field" .= n]
1701− (ScriptValueCount (Script s)) -> object ["script" .= s]
1702− toJSON (FilterAgg (FilterAggregation filt ags)) =
1703− omitNulls [ "filter" .= filt
1705− toJSON (DateRangeAgg a) = object [ "date_range" .= a
1708−instance ToJSON DateRangeAggregation where
1709− toJSON DateRangeAggregation {..} =
1710− omitNulls [ "field" .= draField
1711− , "format" .= draFormat
1712− , "ranges" .= toList draRanges
1715−instance ToJSON DateRangeAggRange where
1716− toJSON (DateRangeFrom e) = object [ "from" .= e ]
1717− toJSON (DateRangeTo e) = object [ "to" .= e ]
1718− toJSON (DateRangeFromAndTo f t) = object [ "from" .= f, "to" .= t ]
1720−instance ToJSON DateMathExpr where
1721− toJSON (DateMathExpr a mods) = String (fmtA a <> mconcat (fmtMod <$> mods))
1722− where fmtA DMNow = "now"
1723− fmtA (DMDate date) = case toGregorian date of
1724− (y,m,d) -> showText y <> "-" <>
1725− showText m <> "-" <>
1727− fmtMod (AddTime n u) = "+" <> showText n <> fmtU u
1728− fmtMod (SubtractTime n u) = "-" <> showText n <> fmtU u
1729− fmtMod (RoundDownTo u) = "/" <> fmtU u
1735− fmtU DMMinute = "m"
1736− fmtU DMSecond = "s"
1739−type AggregationResults = M.Map Text Value
1741−class BucketAggregation a where
1743− docCount :: a -> Int
1744− aggs :: a -> Maybe AggregationResults
1747−data Bucket a = Bucket { buckets :: [a]} deriving (Show)
1749−data BucketValue = TextValue Text
1750− | ScientificValue Scientific
1751− | BoolValue Bool deriving (Show)
1753−data TermsResult = TermsResult { termKey :: BucketValue
1754− , termsDocCount :: Int
1755− , termsAggs :: Maybe AggregationResults } deriving (Show)
1757−data DateHistogramResult = DateHistogramResult { dateKey :: Int
1758− , dateKeyStr :: Maybe Text
1759− , dateDocCount :: Int
1760− , dateHistogramAggs :: Maybe AggregationResults } deriving (Show)
1762−data DateRangeResult = DateRangeResult { dateRangeKey :: Text
1763− , dateRangeFrom :: Maybe UTCTime
1764− , dateRangeFromAsString :: Maybe Text
1765− , dateRangeTo :: Maybe UTCTime
1766− , dateRangeToAsString :: Maybe Text
1767− , dateRangeDocCount :: Int
1768− , dateRangeAggs :: Maybe AggregationResults } deriving (Show, Eq, Generic, Typeable)
1770−toTerms :: Text -> AggregationResults -> Maybe (Bucket TermsResult)
1771−toTerms t a = M.lookup t a >>= deserialize
1772− where deserialize = parseMaybe parseJSON
1774−toDateHistogram :: Text -> AggregationResults -> Maybe (Bucket DateHistogramResult)
1775−toDateHistogram t a = M.lookup t a >>= deserialize
1776− where deserialize = parseMaybe parseJSON
1778−instance BucketAggregation TermsResult where
1779− key (TermsResult (TextValue t) _ _) = t
1780− key (TermsResult (ScientificValue s) _ _) = T.pack $ show s
1781− key (TermsResult (BoolValue b) _ _) = T.pack $ show b
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 BucketValue where
1801− parseJSON (String t) = return $ TextValue t
1802− parseJSON (Number s) = return $ ScientificValue s
1803− parseJSON (Bool b) = return $ BoolValue b
1804− parseJSON _ = mempty
1806−instance FromJSON TermsResult where
1807− parseJSON = withObject "TermsResult" $ \o -> do
1808− termKey <- o .: "key"
1809− termsDocCount <- o .: "doc_count"
1810− termsAggs <- o .:? "aggregations"
1811− return TermsResult{..}
1813−instance FromJSON DateHistogramResult where
1814− parseJSON (Object v) = DateHistogramResult <$>
1816− v .:? "key_as_string" <*>
1817− v .: "doc_count" <*>
1818− v .:? "aggregations"
1819− parseJSON _ = mempty
1821−instance FromJSON DateRangeResult where
1822− parseJSON = withObject "DateRangeResult" parse
1823− where parse v = DateRangeResult <$>
1825− (fmap posixMS <$> v .:? "from") <*>
1826− v .:? "from_as_string" <*>
1827− (fmap posixMS <$> v .:? "to") <*>
1828− v .:? "to_as_string" <*>
1829− v .: "doc_count" <*>
1830− v .:? "aggregations"
1832−instance FromJSON POSIXMS where
1833− parseJSON = withScientific "POSIXMS" (return . parse)
1834− where parse n = let n' = truncate n :: Integer
1835− in POSIXMS (posixSecondsToUTCTime (fromInteger (n' `div` 1000)))
1837−instance Monoid Filter where
1838− mempty = IdentityFilter
1839− mappend a b = AndFilter [a, b] defaultCache
1841−instance Seminearring Filter where
1842− a <||> b = OrFilter [a, b] defaultCache
1844−instance ToJSON Filter where
1845− toJSON (AndFilter filters cache) =
1847− object [ "filters" .= fmap toJSON filters
1848− , "_cache" .= cache]]
1850− toJSON (OrFilter filters cache) =
1852− object [ "filters" .= fmap toJSON filters
1853− , "_cache" .= cache]]
1855− toJSON (NotFilter notFilter cache) =
1857− object ["filter" .= notFilter
1858− , "_cache" .= cache]]
1860− toJSON (IdentityFilter) =
1861− object ["match_all" .= object []]
1863− toJSON (TermFilter (Term termFilterField termFilterValue) cache) =
1864− object ["term" .= object base]
1865− where base = [termFilterField .= termFilterValue,
1868− toJSON (ExistsFilter (FieldName fieldName)) =
1869− object ["exists" .= object
1870− ["field" .= fieldName]]
1872− toJSON (BoolFilter boolMatch) =
1873− object ["bool" .= boolMatch]
1875− toJSON (GeoBoundingBoxFilter bbConstraint) =
1876− object ["geo_bounding_box" .= bbConstraint]
1878− toJSON (GeoDistanceFilter (GeoPoint (FieldName distanceGeoField) geoDistLatLon)
1879− distance distanceType optimizeBbox cache) =
1880− object ["geo_distance" .=
1881− object ["distance" .= distance
1882− , "distance_type" .= distanceType
1883− , "optimize_bbox" .= optimizeBbox
1884− , distanceGeoField .= geoDistLatLon
1885− , "_cache" .= cache]]
1887− toJSON (GeoDistanceRangeFilter (GeoPoint (FieldName gddrField) drLatLon)
1888− (DistanceRange geoDistRangeDistFrom drDistanceTo)) =
1889− object ["geo_distance_range" .=
1890− object ["from" .= geoDistRangeDistFrom
1891− , "to" .= drDistanceTo
1892− , gddrField .= drLatLon]]
1894− toJSON (GeoPolygonFilter (FieldName geoPolygonFilterField) latLons) =
1895− object ["geo_polygon" .=
1896− object [geoPolygonFilterField .=
1897− object ["points" .= fmap toJSON latLons]]]
1899− toJSON (IdsFilter (MappingName mappingName) values) =
1901− object ["type" .= mappingName
1902− , "values" .= fmap unpackId values]]
1904− toJSON (LimitFilter limit) =
1905− object ["limit" .= object ["value" .= limit]]
1907− toJSON (MissingFilter (FieldName fieldName) (Existence existence) (NullValue nullValue)) =
1908− object ["missing" .=
1909− object ["field" .= fieldName
1910− , "existence" .= existence
1911− , "null_value" .= nullValue]]
1913− toJSON (PrefixFilter (FieldName fieldName) fieldValue cache) =
1914− object ["prefix" .=
1915− object [fieldName .= fieldValue
1916− , "_cache" .= cache]]
1918− toJSON (QueryFilter query False) =
1919− object ["query" .= toJSON query ]
1920− toJSON (QueryFilter query True) =
1921− object ["fquery" .=
1922− object [ "query" .= toJSON query
1923− , "_cache" .= True ]]
1925− toJSON (RangeFilter (FieldName fieldName) rangeValue rangeExecution cache) =
1927− object [ fieldName .= object (rangeValueToPair rangeValue)
1928− , "execution" .= rangeExecution
1929− , "_cache" .= cache]]
1931− toJSON (RegexpFilter (FieldName fieldName)
1932− (Regexp regexText) flags (CacheName cacheName) cache (CacheKey cacheKey)) =
1933− object ["regexp" .=
1934− object [fieldName .=
1935− object ["value" .= regexText
1936− , "flags" .= flags]
1937− , "_name" .= cacheName
1938− , "_cache" .= cache
1939− , "_cache_key" .= cacheKey]]
1941−instance FromJSON Filter where
1942− parseJSON = withObject "Filter" parse
1943− where parse o = andFilter `taggedWith` "and"
1944− <|> orFilter `taggedWith` "or"
1945− <|> notFilter `taggedWith` "not"
1946− <|> identityFilter `taggedWith` "match_all"
1947− <|> boolFilter `taggedWith` "bool"
1948− <|> existsFilter `taggedWith` "exists"
1949− <|> geoBoundingBoxFilter `taggedWith` "geo_bounding_box"
1950− <|> geoDistanceFilter `taggedWith` "geo_distance"
1951− <|> geoDistanceRangeFilter `taggedWith` "geo_distance_range"
1952− <|> geoPolygonFilter `taggedWith` "geo_polygon"
1953− <|> idsFilter `taggedWith` "ids"
1954− <|> limitFilter `taggedWith` "limit"
1955− <|> missingFilter `taggedWith` "missing"
1956− <|> prefixFilter `taggedWith` "prefix"
1957− <|> queryFilter `taggedWith` "query"
1958− <|> fqueryFilter `taggedWith` "fquery"
1959− <|> rangeFilter `taggedWith` "range"
1960− <|> regexpFilter `taggedWith` "regexp"
1961− <|> termFilter `taggedWith` "term"
1962− where taggedWith parser k = parser =<< o .: k
1963− andFilter o = AndFilter <$> o .: "filters"
1964− <*> o .:? "_cache" .!= defaultCache
1965− orFilter o = OrFilter <$> o .: "filters"
1966− <*> o .:? "_cache" .!= defaultCache
1967− notFilter o = NotFilter <$> o .: "filter"
1968− <*> o .: "_cache" .!= defaultCache
1969− identityFilter :: Object -> Parser Filter
1971− | HM.null m = pure IdentityFilter
1972− | otherwise = fail ("Identityfilter expected empty object but got " <> show m)
1973− boolFilter = pure . BoolFilter
1974− existsFilter o = ExistsFilter <$> o .: "field"
1975− geoBoundingBoxFilter = pure . GeoBoundingBoxFilter
1976− geoDistanceFilter o = do
1977− case HM.toList (deleteSeveral ["distance", "distance_type", "optimize_bbox", "_cache"] o) of
1979− gp <- GeoPoint (FieldName fn) <$> parseJSON v
1980− GeoDistanceFilter gp <$> o .: "distance"
1981− <*> o .: "distance_type"
1982− <*> o .: "optimize_bbox"
1983− <*> o .:? "_cache" .!= defaultCache
1984− _ -> fail "Could not find GeoDistanceFilter field name"
1985− geoDistanceRangeFilter o = do
1986− case HM.toList (deleteSeveral ["from", "to"] o) of
1988− gp <- GeoPoint (FieldName fn) <$> parseJSON v
1989− rng <- DistanceRange <$> o .: "from" <*> o .: "to"
1990− return (GeoDistanceRangeFilter gp rng)
1991− _ -> fail "Could not find GeoDistanceRangeFilter field name"
1992− geoPolygonFilter = fieldTagged $ \fn o -> GeoPolygonFilter fn <$> o .: "points"
1993− idsFilter o = IdsFilter <$> o .: "type"
1995− limitFilter o = LimitFilter <$> o .: "value"
1996− missingFilter o = MissingFilter <$> o .: "field"
1997− <*> o .: "existence"
1998− <*> o .: "null_value"
1999− prefixFilter o = case HM.toList (HM.delete "_cache" o) of
2000− [(fn, String v)] -> PrefixFilter (FieldName fn) v <$> o .:? "_cache" .!= defaultCache
2001− _ -> fail "Could not parse PrefixFilter"
2003− queryFilter q = pure (QueryFilter q False)
2004− fqueryFilter o = QueryFilter <$> o .: "query" <*> pure True
2005− rangeFilter o = case HM.toList (deleteSeveral ["execution", "_cache"] o) of
2006− [(fn, v)] -> RangeFilter (FieldName fn)
2008− <*> o .: "execution"
2009− <*> o .:? "_cache" .!= defaultCache
2010− _ -> fail "Could not find field name for RangeFilter"
2011− regexpFilter o = case HM.toList (deleteSeveral ["_name", "_cache", "_cache_key"] o) of
2012− [(fn, Object o')] -> RegexpFilter (FieldName fn)
2016− <*> o .:? "_cache" .!= defaultCache
2017− <*> o .: "_cache_key"
2018− _ -> fail "Could not find field name for RegexpFilter"
2019− termFilter o = case HM.toList (HM.delete "_cache" o) of
2020− [(termField, String termVal)] -> TermFilter (Term termField termVal)
2021− <$> o .:? "_cache" .!= defaultCache
2022− _ -> fail "Could not find term field for TermFilter"
2024−fieldTagged :: Monad m => (FieldName -> Object -> m a) -> Object -> m a
2025−fieldTagged f o = case HM.toList o of
2026− [(k, Object o')] -> f (FieldName k) o'
2027− _ -> fail "Expected object with 1 field-named key"
2029−instance ToJSON GeoPoint where
2030− toJSON (GeoPoint (FieldName geoPointField) geoPointLatLon) =
2031− object [ geoPointField .= geoPointLatLon ]
2034−instance ToJSON Query where
2035− toJSON (TermQuery (Term termQueryField termQueryValue) boost) =
2037− object [termQueryField .= object merged]]
2039− base = [ "value" .= termQueryValue ]
2040− boosted = maybe [] (return . ("boost" .=)) boost
2041− merged = mappend base boosted
2043− toJSON (TermsQuery fieldName terms) =
2044− object [ "terms" .= object conjoined ]
2045− where conjoined = [fieldName .= terms]
2046− toJSON (IdsQuery idsQueryMappingName docIds) =
2047− object [ "ids" .= object conjoined ]
2048− where conjoined = [ "type" .= idsQueryMappingName
2049− , "values" .= fmap toJSON docIds ]
2051− toJSON (QueryQueryStringQuery qQueryStringQuery) =
2052− object [ "query_string" .= qQueryStringQuery ]
2054− toJSON (QueryMatchQuery matchQuery) =
2055− object [ "match" .= matchQuery ]
2057− toJSON (QueryMultiMatchQuery multiMatchQuery) =
2058− toJSON multiMatchQuery
2060− toJSON (QueryBoolQuery boolQuery) =
2061− object [ "bool" .= boolQuery ]
2063− toJSON (QueryBoostingQuery boostingQuery) =
2064− object [ "boosting" .= boostingQuery ]
2066− toJSON (QueryCommonTermsQuery commonTermsQuery) =
2067− object [ "common" .= commonTermsQuery ]
2069− toJSON (ConstantScoreFilter csFilter boost) =
2070− object ["constant_score" .= object ["filter" .= csFilter
2071− , "boost" .= boost]]
2073− toJSON (ConstantScoreQuery query boost) =
2074− object ["constant_score" .= object ["query" .= query
2075− , "boost" .= boost]]
2077− toJSON (QueryDisMaxQuery disMaxQuery) =
2078− object [ "dis_max" .= disMaxQuery ]
2080− toJSON (QueryFilteredQuery qFilteredQuery) =
2081− object [ "filtered" .= qFilteredQuery ]
2083− toJSON (QueryFuzzyLikeThisQuery fuzzyQuery) =
2084− object [ "fuzzy_like_this" .= fuzzyQuery ]
2086− toJSON (QueryFuzzyLikeFieldQuery fuzzyFieldQuery) =
2087− object [ "fuzzy_like_this_field" .= fuzzyFieldQuery ]
2089− toJSON (QueryFuzzyQuery fuzzyQuery) =
2090− object [ "fuzzy" .= fuzzyQuery ]
2092− toJSON (QueryHasChildQuery childQuery) =
2093− object [ "has_child" .= childQuery ]
2095− toJSON (QueryHasParentQuery parentQuery) =
2096− object [ "has_parent" .= parentQuery ]
2098− toJSON (QueryIndicesQuery qIndicesQuery) =
2099− object [ "indices" .= qIndicesQuery ]
2101− toJSON (MatchAllQuery boost) =
2102− object [ "match_all" .= omitNulls [ "boost" .= boost ] ]
2104− toJSON (QueryMoreLikeThisQuery query) =
2105− object [ "more_like_this" .= query ]
2107− toJSON (QueryMoreLikeThisFieldQuery query) =
2108− object [ "more_like_this_field" .= query ]
2110− toJSON (QueryNestedQuery query) =
2111− object [ "nested" .= query ]
2113− toJSON (QueryPrefixQuery query) =
2114− object [ "prefix" .= query ]
2116− toJSON (QueryRangeQuery query) =
2117− object [ "range" .= query ]
2119− toJSON (QueryRegexpQuery query) =
2120− object [ "regexp" .= query ]
2122− toJSON (QuerySimpleQueryStringQuery query) =
2123− object [ "simple_query_string" .= query ]
2125−instance FromJSON Query where
2126− parseJSON v = withObject "Query" parse v
2127− where parse o = termQuery `taggedWith` "term"
2128− <|> termsQuery `taggedWith` "terms"
2129− <|> idsQuery `taggedWith` "ids"
2130− <|> queryQueryStringQuery `taggedWith` "query_string"
2131− <|> queryMatchQuery `taggedWith` "match"
2132− <|> queryMultiMatchQuery
2133− <|> queryBoolQuery `taggedWith` "bool"
2134− <|> queryBoostingQuery `taggedWith` "boosting"
2135− <|> queryCommonTermsQuery `taggedWith` "common"
2136− <|> constantScoreFilter `taggedWith` "constant_score"
2137− <|> constantScoreQuery `taggedWith` "constant_score"
2138− <|> queryDisMaxQuery `taggedWith` "dis_max"
2139− <|> queryFilteredQuery `taggedWith` "filtered"
2140− <|> queryFuzzyLikeThisQuery `taggedWith` "fuzzy_like_this"
2141− <|> queryFuzzyLikeFieldQuery `taggedWith` "fuzzy_like_this_field"
2142− <|> queryFuzzyQuery `taggedWith` "fuzzy"
2143− <|> queryHasChildQuery `taggedWith` "has_child"
2144− <|> queryHasParentQuery `taggedWith` "has_parent"
2145− <|> queryIndicesQuery `taggedWith` "indices"
2146− <|> matchAllQuery `taggedWith` "match_all"
2147− <|> queryMoreLikeThisQuery `taggedWith` "more_like_this"
2148− <|> queryMoreLikeThisFieldQuery `taggedWith` "more_like_this_field"
2149− <|> queryNestedQuery `taggedWith` "nested"
2150− <|> queryPrefixQuery `taggedWith` "prefix"
2151− <|> queryRangeQuery `taggedWith` "range"
2152− <|> queryRegexpQuery `taggedWith` "regexp"
2153− <|> querySimpleQueryStringQuery `taggedWith` "simple_query_string"
2154− where taggedWith parser k = parser =<< o .: k
2155− termQuery = fieldTagged $ \(FieldName fn) o ->
2156− TermQuery <$> (Term fn <$> o .: "value") <*> o .:? "boost"
2157− termsQuery o = case HM.toList o of
2158− [(fn, vs)] -> do vals <- parseJSON vs
2160− x:xs -> return (TermsQuery fn (x :| xs))
2161− _ -> fail "Expected non empty list of values"
2162− _ -> fail "Expected object with 1 field-named key"
2163− idsQuery o = IdsQuery <$> o .: "type"
2165− queryQueryStringQuery = pure . QueryQueryStringQuery
2166− queryMatchQuery = pure . QueryMatchQuery
2167− queryMultiMatchQuery = QueryMultiMatchQuery <$> parseJSON v
2168− queryBoolQuery = pure . QueryBoolQuery
2169− queryBoostingQuery = pure . QueryBoostingQuery
2170− queryCommonTermsQuery = pure . QueryCommonTermsQuery
2171− constantScoreFilter o = case HM.lookup "filter" o of
2172− Just x -> ConstantScoreFilter <$> parseJSON x
2174− _ -> fail "Does not appear to be a ConstantScoreFilter"
2175− constantScoreQuery o = case HM.lookup "query" o of
2176− Just x -> ConstantScoreQuery <$> parseJSON x
2178− _ -> fail "Does not appear to be a ConstantScoreQuery"
2179− queryDisMaxQuery = pure . QueryDisMaxQuery
2180− queryFilteredQuery = pure . QueryFilteredQuery
2181− queryFuzzyLikeThisQuery = pure . QueryFuzzyLikeThisQuery
2182− queryFuzzyLikeFieldQuery = pure . QueryFuzzyLikeFieldQuery
2183− queryFuzzyQuery = pure . QueryFuzzyQuery
2184− queryHasChildQuery = pure . QueryHasChildQuery
2185− queryHasParentQuery = pure . QueryHasParentQuery
2186− queryIndicesQuery = pure . QueryIndicesQuery
2187− matchAllQuery o = MatchAllQuery <$> o .:? "boost"
2188− queryMoreLikeThisQuery = pure . QueryMoreLikeThisQuery
2189− queryMoreLikeThisFieldQuery = pure . QueryMoreLikeThisFieldQuery
2190− queryNestedQuery = pure . QueryNestedQuery
2191− queryPrefixQuery = pure . QueryPrefixQuery
2192− queryRangeQuery = pure . QueryRangeQuery
2193− queryRegexpQuery = pure . QueryRegexpQuery
2194− querySimpleQueryStringQuery = pure . QuerySimpleQueryStringQuery
2197−omitNulls :: [(Text, Value)] -> Value
2198−omitNulls = object . filter notNull where
2199− notNull (_, Null) = False
2200− notNull (_, Array a) = (not . V.null) a
2204−instance ToJSON SimpleQueryStringQuery where
2205− toJSON SimpleQueryStringQuery {..} =
2206− omitNulls (base ++ maybeAdd)
2207− where base = [ "query" .= simpleQueryStringQuery ]
2208− maybeAdd = [ "fields" .= simpleQueryStringField
2209− , "default_operator" .= simpleQueryStringOperator
2210− , "analyzer" .= simpleQueryStringAnalyzer
2211− , "flags" .= simpleQueryStringFlags
2212− , "lowercase_expanded_terms" .= simpleQueryStringLowercaseExpanded
2213− , "locale" .= simpleQueryStringLocale ]
2215−instance FromJSON SimpleQueryStringQuery where
2216− parseJSON = withObject "SimpleQueryStringQuery" parse
2217− where parse o = SimpleQueryStringQuery <$> o .: "query"
2219− <*> o .:? "default_operator"
2220− <*> o .:? "analyzer"
2221− <*> (parseFlags <$> o .:? "flags")
2222− <*> o .:? "lowercase_expanded_terms"
2224− parseFlags (Just (x:xs)) = Just (x :| xs)
2225− parseFlags _ = Nothing
2227−instance ToJSON FieldOrFields where
2228− toJSON (FofField fieldName) =
2230− toJSON (FofFields fieldNames) =
2233−instance FromJSON FieldOrFields where
2234− parseJSON v = FofField <$> parseJSON v
2235− <|> FofFields <$> (parseNEJSON =<< parseJSON v)
2237−instance ToJSON SimpleQueryFlag where
2238− toJSON SimpleQueryAll = "ALL"
2239− toJSON SimpleQueryNone = "NONE"
2240− toJSON SimpleQueryAnd = "AND"
2241− toJSON SimpleQueryOr = "OR"
2242− toJSON SimpleQueryPrefix = "PREFIX"
2243− toJSON SimpleQueryPhrase = "PHRASE"
2244− toJSON SimpleQueryPrecedence = "PRECEDENCE"
2245− toJSON SimpleQueryEscape = "ESCAPE"
2246− toJSON SimpleQueryWhitespace = "WHITESPACE"
2247− toJSON SimpleQueryFuzzy = "FUZZY"
2248− toJSON SimpleQueryNear = "NEAR"
2249− toJSON SimpleQuerySlop = "SLOP"
2251−instance FromJSON SimpleQueryFlag where
2252− parseJSON = withText "SimpleQueryFlag" parse
2253− where parse "ALL" = pure SimpleQueryAll
2254− parse "NONE" = pure SimpleQueryNone
2255− parse "AND" = pure SimpleQueryAnd
2256− parse "OR" = pure SimpleQueryOr
2257− parse "PREFIX" = pure SimpleQueryPrefix
2258− parse "PHRASE" = pure SimpleQueryPhrase
2259− parse "PRECEDENCE" = pure SimpleQueryPrecedence
2260− parse "ESCAPE" = pure SimpleQueryEscape
2261− parse "WHITESPACE" = pure SimpleQueryWhitespace
2262− parse "FUZZY" = pure SimpleQueryFuzzy
2263− parse "NEAR" = pure SimpleQueryNear
2264− parse "SLOP" = pure SimpleQuerySlop
2265− parse f = fail ("Unexpected SimpleQueryFlag: " <> show f)
2267−instance ToJSON RegexpQuery where
2268− toJSON (RegexpQuery (FieldName rqQueryField)
2269− (Regexp regexpQueryQuery) rqQueryFlags
2271− object [ rqQueryField .= omitNulls base ]
2272− where base = [ "value" .= regexpQueryQuery
2273− , "flags" .= rqQueryFlags
2274− , "boost" .= rqQueryBoost ]
2276−instance FromJSON RegexpQuery where
2277− parseJSON = withObject "RegexpQuery" parse
2278− where parse = fieldTagged $ \fn o ->
2284−instance ToJSON QueryStringQuery where
2285− toJSON (QueryStringQuery qsQueryString
2286− qsDefaultField qsOperator
2287− qsAnalyzer qsAllowWildcard
2288− qsLowercaseExpanded qsEnablePositionIncrements
2289− qsFuzzyMaxExpansions qsFuzziness
2290− qsFuzzyPrefixLength qsPhraseSlop
2291− qsBoost qsAnalyzeWildcard
2292− qsGeneratePhraseQueries qsMinimumShouldMatch
2293− qsLenient qsLocale) =
2296− base = [ "query" .= qsQueryString
2297− , "default_field" .= qsDefaultField
2298− , "default_operator" .= qsOperator
2299− , "analyzer" .= qsAnalyzer
2300− , "allow_leading_wildcard" .= qsAllowWildcard
2301− , "lowercase_expanded_terms" .= qsLowercaseExpanded
2302− , "enable_position_increments" .= qsEnablePositionIncrements
2303− , "fuzzy_max_expansions" .= qsFuzzyMaxExpansions
2304− , "fuzziness" .= qsFuzziness
2305− , "fuzzy_prefix_length" .= qsFuzzyPrefixLength
2306− , "phrase_slop" .= qsPhraseSlop
2307− , "boost" .= qsBoost
2308− , "analyze_wildcard" .= qsAnalyzeWildcard
2309− , "auto_generate_phrase_queries" .= qsGeneratePhraseQueries
2310− , "minimum_should_match" .= qsMinimumShouldMatch
2311− , "lenient" .= qsLenient
2312− , "locale" .= qsLocale ]
2314−instance FromJSON QueryStringQuery where
2315− parseJSON = withObject "QueryStringQuery" parse
2316− where parse o = QueryStringQuery
2318− <*> o .:? "default_field"
2319− <*> o .:? "default_operator"
2320− <*> o .:? "analyzer"
2321− <*> o .:? "allow_leading_wildcard"
2322− <*> o .:? "lowercase_expanded_terms"
2323− <*> o .:? "enable_position_increments"
2324− <*> o .:? "fuzzy_max_expansions"
2325− <*> o .:? "fuzziness"
2326− <*> o .:? "fuzzy_prefix_length"
2327− <*> o .:? "phrase_slop"
2329− <*> o .:? "analyze_wildcard"
2330− <*> o .:? "auto_generate_phrase_queries"
2331− <*> o .:? "minimum_should_match"
2332− <*> o .:? "lenient"
2335−instance ToJSON RangeQuery where
2336− toJSON (RangeQuery (FieldName fieldName) range boost) =
2337− object [ fieldName .= object conjoined ]
2338− where conjoined = [ "boost" .= boost ] ++ (rangeValueToPair range)
2340−instance FromJSON RangeQuery where
2341− parseJSON = withObject "RangeQuery" parse
2342− where parse = fieldTagged $ \fn o ->
2344− <$> parseJSON (Object o)
2347−instance FromJSON RangeValue where
2348− parseJSON = withObject "RangeValue" parse
2349− where parse o = parseDate o
2351− parseDate o = do lt <- o .:? "lt"
2355− case (lt, lte, gt, gte) of
2356− (Just a, _, Just b, _) -> return (RangeDateGtLt (GreaterThanD b) (LessThanD a))
2357− (Just a, _, _, Just b)-> return (RangeDateGteLt (GreaterThanEqD b) (LessThanD a))
2358− (_, Just a, Just b, _)-> return (RangeDateGtLte (GreaterThanD b) (LessThanEqD a))
2359− (_, Just a, _, Just b)-> return (RangeDateGteLte (GreaterThanEqD b) (LessThanEqD a))
2360− (_, _, Just a, _)-> return (RangeDateGt (GreaterThanD a))
2361− (Just a, _, _, _)-> return (RangeDateLt (LessThanD a))
2362− (_, _, _, Just a)-> return (RangeDateGte (GreaterThanEqD a))
2363− (_, Just a, _, _)-> return (RangeDateLte (LessThanEqD a))
2364− (Nothing, Nothing, Nothing, Nothing) -> mzero
2365− parseDouble o = do lt <- o .:? "lt"
2369− case (lt, lte, gt, gte) of
2370− (Just a, _, Just b, _) -> return (RangeDoubleGtLt (GreaterThan b) (LessThan a))
2371− (Just a, _, _, Just b)-> return (RangeDoubleGteLt (GreaterThanEq b) (LessThan a))
2372− (_, Just a, Just b, _)-> return (RangeDoubleGtLte (GreaterThan b) (LessThanEq a))
2373− (_, Just a, _, Just b)-> return (RangeDoubleGteLte (GreaterThanEq b) (LessThanEq a))
2374− (_, _, Just a, _)-> return (RangeDoubleGt (GreaterThan a))
2375− (Just a, _, _, _)-> return (RangeDoubleLt (LessThan a))
2376− (_, _, _, Just a)-> return (RangeDoubleGte (GreaterThanEq a))
2377− (_, Just a, _, _)-> return (RangeDoubleLte (LessThanEq a))
2378− (Nothing, Nothing, Nothing, Nothing) -> mzero
2380−instance ToJSON PrefixQuery where
2381− toJSON (PrefixQuery (FieldName fieldName) queryValue boost) =
2382− object [ fieldName .= omitNulls base ]
2383− where base = [ "value" .= queryValue
2384− , "boost" .= boost ]
2386−instance FromJSON PrefixQuery where
2387− parseJSON = withObject "PrefixQuery" parse
2388− where parse = fieldTagged $ \fn o ->
2393−instance ToJSON NestedQuery where
2394− toJSON (NestedQuery nqPath nqScoreType nqQuery) =
2395− object [ "path" .= nqPath
2396− , "score_mode" .= nqScoreType
2397− , "query" .= nqQuery ]
2399−instance FromJSON NestedQuery where
2400− parseJSON = withObject "NestedQuery" parse
2401− where parse o = NestedQuery
2403− <*> o .: "score_mode"
2406−instance ToJSON MoreLikeThisFieldQuery where
2407− toJSON (MoreLikeThisFieldQuery text (FieldName fieldName)
2408− percent mtf mqt stopwords mindf maxdf
2409− minwl maxwl boostTerms boost analyzer) =
2410− object [ fieldName .= omitNulls base ]
2411− where base = [ "like_text" .= text
2412− , "percent_terms_to_match" .= percent
2413− , "min_term_freq" .= mtf
2414− , "max_query_terms" .= mqt
2415− , "stop_words" .= stopwords
2416− , "min_doc_freq" .= mindf
2417− , "max_doc_freq" .= maxdf
2418− , "min_word_length" .= minwl
2419− , "max_word_length" .= maxwl
2420− , "boost_terms" .= boostTerms
2422− , "analyzer" .= analyzer ]
2424−instance FromJSON MoreLikeThisFieldQuery where
2425− parseJSON = withObject "MoreLikeThisFieldQuery" parse
2426− where parse = fieldTagged $ \fn o ->
2427− MoreLikeThisFieldQuery
2428− <$> o .: "like_text"
2430− <*> o .:? "percent_terms_to_match"
2431− <*> o .:? "min_term_freq"
2432− <*> o .:? "max_query_terms"
2433− <*> (optionalNE =<< o .:? "stop_words")
2434− <*> o .:? "min_doc_freq"
2435− <*> o .:? "max_doc_freq"
2436− <*> o .:? "min_word_length"
2437− <*> o .:? "max_word_length"
2438− <*> o .:? "boost_terms"
2440− <*> o .:? "analyzer"
2441− optionalNE = maybe (pure Nothing) (fmap Just . parseNEJSON)
2443−instance ToJSON MoreLikeThisQuery where
2444− toJSON (MoreLikeThisQuery text fields percent
2445− mtf mqt stopwords mindf maxdf
2446− minwl maxwl boostTerms boost analyzer) =
2448− where base = [ "like_text" .= text
2449− , "fields" .= fields
2450− , "percent_terms_to_match" .= percent
2451− , "min_term_freq" .= mtf
2452− , "max_query_terms" .= mqt
2453− , "stop_words" .= stopwords
2454− , "min_doc_freq" .= mindf
2455− , "max_doc_freq" .= maxdf
2456− , "min_word_length" .= minwl
2457− , "max_word_length" .= maxwl
2458− , "boost_terms" .= boostTerms
2460− , "analyzer" .= analyzer ]
2462−instance FromJSON MoreLikeThisQuery where
2463− parseJSON = withObject "MoreLikeThisQuery" parse
2464− where parse o = MoreLikeThisQuery
2465− <$> o .: "like_text"
2466− <*> (optionalNE =<< o .:? "fields")
2467− <*> o .:? "percent_terms_to_match"
2468− <*> o .:? "min_term_freq"
2469− <*> o .:? "max_query_terms"
2470− <*> (optionalNE =<< o .:? "stop_words")
2471− <*> o .:? "min_doc_freq"
2472− <*> o .:? "max_doc_freq"
2473− <*> o .:? "min_word_length"
2474− <*> o .:? "max_word_length"
2475− <*> o .:? "boost_terms"
2477− <*> o .:? "analyzer"
2478− optionalNE = maybe (pure Nothing) (fmap Just . parseNEJSON)
2480−instance ToJSON IndicesQuery where
2481− toJSON (IndicesQuery indices query noMatch) =
2482− omitNulls [ "indices" .= indices
2483− , "no_match_query" .= noMatch
2484− , "query" .= query ]
2486−instance FromJSON IndicesQuery where
2487− parseJSON = withObject "IndicesQuery" parse
2488− where parse o = IndicesQuery
2489− <$> o .:? "indices" .!= []
2491− <*> o .:? "no_match_query"
2493−instance ToJSON HasParentQuery where
2494− toJSON (HasParentQuery queryType query scoreType) =
2495− omitNulls [ "parent_type" .= queryType
2496− , "score_type" .= scoreType
2497− , "query" .= query ]
2499−instance FromJSON HasParentQuery where
2500− parseJSON = withObject "HasParentQuery" parse
2501− where parse o = HasParentQuery
2502− <$> o .: "parent_type"
2504− <*> o .:? "score_type"
2506−instance ToJSON HasChildQuery where
2507− toJSON (HasChildQuery queryType query scoreType) =
2508− omitNulls [ "query" .= query
2509− , "score_type" .= scoreType
2510− , "type" .= queryType ]
2512−instance FromJSON HasChildQuery where
2513− parseJSON = withObject "HasChildQuery" parse
2514− where parse o = HasChildQuery
2517− <*> o .:? "score_type"
2519−instance ToJSON FuzzyQuery where
2520− toJSON (FuzzyQuery (FieldName fieldName) queryText
2521− prefixLength maxEx fuzziness boost) =
2522− object [ fieldName .= omitNulls base ]
2523− where base = [ "value" .= queryText
2524− , "fuzziness" .= fuzziness
2525− , "prefix_length" .= prefixLength
2527− , "max_expansions" .= maxEx ]
2529−instance FromJSON FuzzyQuery where
2530− parseJSON = withObject "FuzzyQuery" parse
2531− where parse = fieldTagged $ \fn o ->
2534− <*> o .: "prefix_length"
2535− <*> o .: "max_expansions"
2536− <*> o .: "fuzziness"
2539−instance ToJSON FuzzyLikeFieldQuery where
2540− toJSON (FuzzyLikeFieldQuery (FieldName fieldName)
2541− fieldText maxTerms ignoreFreq fuzziness prefixLength
2543− object [ fieldName .=
2544− omitNulls [ "like_text" .= fieldText
2545− , "max_query_terms" .= maxTerms
2546− , "ignore_tf" .= ignoreFreq
2547− , "fuzziness" .= fuzziness
2548− , "prefix_length" .= prefixLength
2549− , "analyzer" .= analyzer
2550− , "boost" .= boost ]]
2552−instance FromJSON FuzzyLikeFieldQuery where
2553− parseJSON = withObject "FuzzyLikeFieldQuery" parse
2554− where parse = fieldTagged $ \fn o ->
2555− FuzzyLikeFieldQuery fn
2556− <$> o .: "like_text"
2557− <*> o .: "max_query_terms"
2558− <*> o .: "ignore_tf"
2559− <*> o .: "fuzziness"
2560− <*> o .: "prefix_length"
2562− <*> o .:? "analyzer"
2564−instance ToJSON FuzzyLikeThisQuery where
2565− toJSON (FuzzyLikeThisQuery fields text maxTerms
2566− ignoreFreq fuzziness prefixLength boost analyzer) =
2568− where base = [ "fields" .= fields
2569− , "like_text" .= text
2570− , "max_query_terms" .= maxTerms
2571− , "ignore_tf" .= ignoreFreq
2572− , "fuzziness" .= fuzziness
2573− , "prefix_length" .= prefixLength
2574− , "analyzer" .= analyzer
2575− , "boost" .= boost ]
2577−instance FromJSON FuzzyLikeThisQuery where
2578− parseJSON = withObject "FuzzyLikeThisQuery" parse
2579− where parse o = FuzzyLikeThisQuery
2580− <$> o .:? "fields" .!= []
2581− <*> o .: "like_text"
2582− <*> o .: "max_query_terms"
2583− <*> o .: "ignore_tf"
2584− <*> o .: "fuzziness"
2585− <*> o .: "prefix_length"
2587− <*> o .:? "analyzer"
2589−instance ToJSON FilteredQuery where
2590− toJSON (FilteredQuery query fFilter) =
2591− object [ "query" .= query
2592− , "filter" .= fFilter ]
2594−instance FromJSON FilteredQuery where
2595− parseJSON = withObject "FilteredQuery" parse
2596− where parse o = FilteredQuery
2600−instance ToJSON DisMaxQuery where
2601− toJSON (DisMaxQuery queries tiebreaker boost) =
2603− where base = [ "queries" .= queries
2605− , "tie_breaker" .= tiebreaker ]
2607−instance FromJSON DisMaxQuery where
2608− parseJSON = withObject "DisMaxQuery" parse
2609− where parse o = DisMaxQuery
2610− <$> o .:? "queries" .!= []
2611− <*> o .: "tie_breaker"
2614−instance ToJSON CommonTermsQuery where
2615− toJSON (CommonTermsQuery (FieldName fieldName)
2616− (QueryString query) cf lfo hfo msm
2617− boost analyzer disableCoord) =
2618− object [fieldName .= omitNulls base ]
2619− where base = [ "query" .= query
2620− , "cutoff_frequency" .= cf
2621− , "low_freq_operator" .= lfo
2622− , "minimum_should_match" .= msm
2624− , "analyzer" .= analyzer
2625− , "disable_coord" .= disableCoord
2626− , "high_freq_operator" .= hfo ]
2628−instance FromJSON CommonTermsQuery where
2629− parseJSON = withObject "CommonTermsQuery" parse
2630− where parse = fieldTagged $ \fn o ->
2631− CommonTermsQuery fn
2633− <*> o .: "cutoff_frequency"
2634− <*> o .: "low_freq_operator"
2635− <*> o .: "high_freq_operator"
2636− <*> o .:? "minimum_should_match"
2638− <*> o .:? "analyzer"
2639− <*> o .:? "disable_coord"
2641−instance ToJSON CommonMinimumMatch where
2642− toJSON (CommonMinimumMatch mm) = toJSON mm
2643− toJSON (CommonMinimumMatchHighLow (MinimumMatchHighLow lowF highF)) =
2644− object [ "low_freq" .= lowF
2645− , "high_freq" .= highF ]
2647−instance FromJSON CommonMinimumMatch where
2648− parseJSON v = parseMinimum v
2649− <|> parseMinimumHighLow v
2650− where parseMinimum = fmap CommonMinimumMatch . parseJSON
2651− parseMinimumHighLow = fmap CommonMinimumMatchHighLow . withObject "CommonMinimumMatchHighLow" (\o ->
2652− MinimumMatchHighLow
2653− <$> o .: "low_freq"
2654− <*> o .: "high_freq")
2657−instance ToJSON BoostingQuery where
2658− toJSON (BoostingQuery bqPositiveQuery bqNegativeQuery bqNegativeBoost) =
2659− object [ "positive" .= bqPositiveQuery
2660− , "negative" .= bqNegativeQuery
2661− , "negative_boost" .= bqNegativeBoost ]
2663−instance FromJSON BoostingQuery where
2664− parseJSON = withObject "BoostingQuery" parse
2665− where parse o = BoostingQuery
2666− <$> o .: "positive"
2667− <*> o .: "negative"
2668− <*> o .: "negative_boost"
2670−instance ToJSON BoolQuery where
2671− toJSON (BoolQuery mustM notM shouldM bqMin boost disableCoord) =
2673− where base = [ "must" .= mustM
2674− , "must_not" .= notM
2675− , "should" .= shouldM
2676− , "minimum_should_match" .= bqMin
2678− , "disable_coord" .= disableCoord ]
2680−instance FromJSON BoolQuery where
2681− parseJSON = withObject "BoolQuery" parse
2682− where parse o = BoolQuery
2683− <$> o .:? "must" .!= []
2684− <*> o .:? "must_not" .!= []
2685− <*> o .:? "should" .!= []
2686− <*> o .:? "minimum_should_match"
2688− <*> o .:? "disable_coord"
2690−instance ToJSON MatchQuery where
2691− toJSON (MatchQuery (FieldName fieldName)
2692− (QueryString mqQueryString) booleanOperator
2693− zeroTermsQuery cutoffFrequency matchQueryType
2694− analyzer maxExpansions lenient boost) =
2695− object [ fieldName .= omitNulls base ]
2696− where base = [ "query" .= mqQueryString
2697− , "operator" .= booleanOperator
2698− , "zero_terms_query" .= zeroTermsQuery
2699− , "cutoff_frequency" .= cutoffFrequency
2700− , "type" .= matchQueryType
2701− , "analyzer" .= analyzer
2702− , "max_expansions" .= maxExpansions
2703− , "lenient" .= lenient
2704− , "boost" .= boost ]
2706−instance FromJSON MatchQuery where
2707− parseJSON = withObject "MatchQuery" parse
2708− where parse = fieldTagged $ \fn o ->
2711− <*> o .: "operator"
2712− <*> o .: "zero_terms_query"
2713− <*> o .:? "cutoff_frequency"
2715− <*> o .:? "analyzer"
2716− <*> o .:? "max_expansions"
2717− <*> o .:? "lenient"
2720−instance ToJSON MultiMatchQuery where
2721− toJSON (MultiMatchQuery fields (QueryString query) boolOp
2722− ztQ tb mmqt cf analyzer maxEx lenient) =
2723− object ["multi_match" .= omitNulls base]
2724− where base = [ "fields" .= fmap toJSON fields
2726− , "operator" .= boolOp
2727− , "zero_terms_query" .= ztQ
2728− , "tiebreaker" .= tb
2730− , "cutoff_frequency" .= cf
2731− , "analyzer" .= analyzer
2732− , "max_expansions" .= maxEx
2733− , "lenient" .= lenient ]
2735−instance FromJSON MultiMatchQuery where
2736− parseJSON = withObject "MultiMatchQuery" parse
2737− where parse raw = do o <- raw .: "multi_match"
2739− <$> o .:? "fields" .!= []
2741− <*> o .: "operator"
2742− <*> o .: "zero_terms_query"
2743− <*> o .:? "tiebreaker"
2745− <*> o .:? "cutoff_frequency"
2746− <*> o .:? "analyzer"
2747− <*> o .:? "max_expansions"
2748− <*> o .:? "lenient"
2750−instance ToJSON MultiMatchQueryType where
2751− toJSON MultiMatchBestFields = "best_fields"
2752− toJSON MultiMatchMostFields = "most_fields"
2753− toJSON MultiMatchCrossFields = "cross_fields"
2754− toJSON MultiMatchPhrase = "phrase"
2755− toJSON MultiMatchPhrasePrefix = "phrase_prefix"
2757−instance FromJSON MultiMatchQueryType where
2758− parseJSON = withText "MultiMatchPhrasePrefix" parse
2759− where parse "best_fields" = pure MultiMatchBestFields
2760− parse "most_fields" = pure MultiMatchMostFields
2761− parse "cross_fields" = pure MultiMatchCrossFields
2762− parse "phrase" = pure MultiMatchPhrase
2763− parse "phrase_prefix" = pure MultiMatchPhrasePrefix
2764− parse t = fail ("Unexpected MultiMatchPhrasePrefix: " <> show t)
2766−instance ToJSON BooleanOperator where
2767− toJSON And = String "and"
2768− toJSON Or = String "or"
2770−instance FromJSON BooleanOperator where
2771− parseJSON = withText "BooleanOperator" parse
2772− where parse "and" = pure And
2773− parse "or" = pure Or
2774− parse o = fail ("Unexpected BooleanOperator: " <> show o)
2776−instance ToJSON ZeroTermsQuery where
2777− toJSON ZeroTermsNone = String "none"
2778− toJSON ZeroTermsAll = String "all"
2780−instance FromJSON ZeroTermsQuery where
2781− parseJSON = withText "ZeroTermsQuery" parse
2782− where parse "none" = pure ZeroTermsNone
2783− parse "all" = pure ZeroTermsAll
2784− parse q = fail ("Unexpected ZeroTermsQuery: " <> show q)
2786−instance ToJSON MatchQueryType where
2787− toJSON MatchPhrase = "phrase"
2788− toJSON MatchPhrasePrefix = "phrase_prefix"
2790−instance FromJSON MatchQueryType where
2791− parseJSON = withText "MatchQueryType" parse
2792− where parse "phrase" = pure MatchPhrase
2793− parse "phrase_prefix" = pure MatchPhrasePrefix
2794− parse t = fail ("Unexpected MatchQueryType: " <> show t)
2796−instance FromJSON Status where
2797− parseJSON (Object v) = Status <$>
2803− parseJSON _ = empty
2806−instance ToJSON IndexSettings where
2807− toJSON (IndexSettings s r) = object ["settings" .=
2809− object ["number_of_shards" .= s, "number_of_replicas" .= r]
2813−instance FromJSON IndexSettings where
2814− parseJSON = withObject "IndexSettings" parse
2815− where parse o = do s <- o .: "settings"
2817− IndexSettings <$> i .: "number_of_shards"
2818− <*> i .: "number_of_replicas"
2820−instance ToJSON UpdatableIndexSetting where
2821− toJSON (NumberOfReplicas x) = oPath ("index" :| ["number_of_replicas"]) x
2822− toJSON (AutoExpandReplicas x) = oPath ("index" :| ["auto_expand_replicas"]) x
2823− toJSON (RefreshInterval x) = oPath ("index" :| ["refresh_interval"]) (NominalDiffTimeJSON x)
2824− toJSON (IndexConcurrency x) = oPath ("index" :| ["concurrency"]) x
2825− toJSON (FailOnMergeFailure x) = oPath ("index" :| ["fail_on_merge_failure"]) x
2826− toJSON (TranslogFlushThresholdOps x) = oPath ("index" :| ["translog", "flush_threshold_ops"]) x
2827− toJSON (TranslogFlushThresholdSize x) = oPath ("index" :| ["translog", "flush_threshold_size"]) x
2828− toJSON (TranslogFlushThresholdPeriod x) = oPath ("index" :| ["translog", "flush_threshold_period"]) (NominalDiffTimeJSON x)
2829− toJSON (TranslogDisableFlush x) = oPath ("index" :| ["translog", "disable_flush"]) x
2830− toJSON (CacheFilterMaxSize x) = oPath ("index" :| ["cache", "filter", "max_size"]) x
2831− toJSON (CacheFilterExpire x) = oPath ("index" :| ["cache", "filter", "expire"]) (NominalDiffTimeJSON <$> x)
2832− toJSON (GatewaySnapshotInterval x) = oPath ("index" :| ["gateway", "snapshot_interval"]) (NominalDiffTimeJSON x)
2833− toJSON (RoutingAllocationInclude fs) = oPath ("index" :| ["routing", "allocation", "include"]) (attrFilterJSON fs)
2834− toJSON (RoutingAllocationExclude fs) = oPath ("index" :| ["routing", "allocation", "exclude"]) (attrFilterJSON fs)
2835− toJSON (RoutingAllocationRequire fs) = oPath ("index" :| ["routing", "allocation", "require"]) (attrFilterJSON fs)
2836− toJSON (RoutingAllocationEnable x) = oPath ("index" :| ["routing", "allocation", "enable"]) x
2837− toJSON (RoutingAllocationShardsPerNode x) = oPath ("index" :| ["routing", "allocation", "total_shards_per_node"]) x
2838− toJSON (RecoveryInitialShards x) = oPath ("index" :| ["recovery", "initial_shards"]) x
2839− toJSON (GCDeletes x) = oPath ("index" :| ["gc_deletes"]) (NominalDiffTimeJSON x)
2840− toJSON (TTLDisablePurge x) = oPath ("index" :| ["ttl", "disable_purge"]) x
2841− toJSON (TranslogFSType x) = oPath ("index" :| ["translog", "fs", "type"]) x
2842− toJSON (IndexCompoundFormat x) = oPath ("index" :| ["compound_format"]) x
2843− toJSON (IndexCompoundOnFlush x) = oPath ("index" :| ["compound_on_flush"]) x
2844− toJSON (WarmerEnabled x) = oPath ("index" :| ["warmer", "enabled"]) x
2845− toJSON (BlocksReadOnly x) = oPath ("blocks" :| ["read_only"]) x
2846− toJSON (BlocksRead x) = oPath ("blocks" :| ["read"]) x
2847− toJSON (BlocksWrite x) = oPath ("blocks" :| ["write"]) x
2848− toJSON (BlocksMetaData x) = oPath ("blocks" :| ["metadata"]) x
2850−instance FromJSON UpdatableIndexSetting where
2851− parseJSON = withObject "UpdatableIndexSetting" parse
2852− where parse o = numberOfReplicas `taggedAt` ["index", "number_of_replicas"]
2853− <|> autoExpandReplicas `taggedAt` ["index", "auto_expand_replicas"]
2854− <|> refreshInterval `taggedAt` ["index", "refresh_interval"]
2855− <|> indexConcurrency `taggedAt` ["index", "concurrency"]
2856− <|> failOnMergeFailure `taggedAt` ["index", "fail_on_merge_failure"]
2857− <|> translogFlushThresholdOps `taggedAt` ["index", "translog", "flush_threshold_ops"]
2858− <|> translogFlushThresholdSize `taggedAt` ["index", "translog", "flush_threshold_size"]
2859− <|> translogFlushThresholdPeriod `taggedAt` ["index", "translog", "flush_threshold_period"]
2860− <|> translogDisableFlush `taggedAt` ["index", "translog", "disable_flush"]
2861− <|> cacheFilterMaxSize `taggedAt` ["index", "cache", "filter", "max_size"]
2862− <|> cacheFilterExpire `taggedAt` ["index", "cache", "filter", "expire"]
2863− <|> gatewaySnapshotInterval `taggedAt` ["index", "gateway", "snapshot_interval"]
2864− <|> routingAllocationInclude `taggedAt` ["index", "routing", "allocation", "include"]
2865− <|> routingAllocationExclude `taggedAt` ["index", "routing", "allocation", "exclude"]
2866− <|> routingAllocationRequire `taggedAt` ["index", "routing", "allocation", "require"]
2867− <|> routingAllocationEnable `taggedAt` ["index", "routing", "allocation", "enable"]
2868− <|> routingAllocationShardsPerNode `taggedAt` ["index", "routing", "allocation", "total_shards_per_node"]
2869− <|> recoveryInitialShards `taggedAt` ["index", "recovery", "initial_shards"]
2870− <|> gcDeletes `taggedAt` ["index", "gc_deletes"]
2871− <|> ttlDisablePurge `taggedAt` ["index", "ttl", "disable_purge"]
2872− <|> translogFSType `taggedAt` ["index", "translog", "fs", "type"]
2873− <|> compoundFormat `taggedAt` ["index", "compound_format"]
2874− <|> compoundOnFlush `taggedAt` ["index", "compound_on_flush"]
2875− <|> warmerEnabled `taggedAt` ["index", "warmer", "enabled"]
2876− <|> blocksReadOnly `taggedAt` ["blocks", "read_only"]
2877− <|> blocksRead `taggedAt` ["blocks", "read"]
2878− <|> blocksWrite `taggedAt` ["blocks", "write"]
2879− <|> blocksMetaData `taggedAt` ["blocks", "metadata"]
2880− where taggedAt f ks = taggedAt' f (Object o) ks
2881− taggedAt' f v [] = f =<< (parseJSON v <|> (parseJSON =<< unStringlyTypeJSON v))
2882− taggedAt' f v (k:ks) = withObject "Object" (\o -> do v' <- o .: k
2883− taggedAt' f v' ks) v
2884− numberOfReplicas = pure . NumberOfReplicas
2885− autoExpandReplicas = pure . AutoExpandReplicas
2886− refreshInterval = pure . RefreshInterval . ndtJSON
2887− indexConcurrency = pure . IndexConcurrency
2888− failOnMergeFailure = pure . FailOnMergeFailure
2889− translogFlushThresholdOps = pure . TranslogFlushThresholdOps
2890− translogFlushThresholdSize = pure . TranslogFlushThresholdSize
2891− translogFlushThresholdPeriod = pure . TranslogFlushThresholdPeriod . ndtJSON
2892− translogDisableFlush = pure . TranslogDisableFlush
2893− cacheFilterMaxSize = pure . CacheFilterMaxSize
2894− cacheFilterExpire = pure . CacheFilterExpire . fmap ndtJSON
2895− gatewaySnapshotInterval = pure . GatewaySnapshotInterval . ndtJSON
2896− routingAllocationInclude = fmap RoutingAllocationInclude . parseAttrFilter
2897− routingAllocationExclude = fmap RoutingAllocationExclude . parseAttrFilter
2898− routingAllocationRequire = fmap RoutingAllocationRequire . parseAttrFilter
2899− routingAllocationEnable = pure . RoutingAllocationEnable
2900− routingAllocationShardsPerNode = pure . RoutingAllocationShardsPerNode
2901− recoveryInitialShards = pure . RecoveryInitialShards
2902− gcDeletes = pure . GCDeletes . ndtJSON
2903− ttlDisablePurge = pure . TTLDisablePurge
2904− translogFSType = pure . TranslogFSType
2905− compoundFormat = pure . IndexCompoundFormat
2906− compoundOnFlush = pure . IndexCompoundOnFlush
2907− warmerEnabled = pure . WarmerEnabled
2908− blocksReadOnly = pure . BlocksReadOnly
2909− blocksRead = pure . BlocksRead
2910− blocksWrite = pure . BlocksWrite
2911− blocksMetaData = pure . BlocksMetaData
2913−instance FromJSON IndexSettingsSummary where
2914− parseJSON = withObject "IndexSettingsSummary" parse
2915− where parse o = case HM.toList o of
2916− [(ixn, v@(Object o'))] -> IndexSettingsSummary (IndexName ixn)
2918− <*> (fmap (filter (not . redundant)) . parseSettings =<< o' .: "settings")
2919− _ -> fail "Expected single-key object with index name"
2920− redundant (NumberOfReplicas _) = True
2921− redundant _ = False
2923−-- | For some reason in the settings API, all leaf values get returned
2924−-- as strings. This function attepmts to recover from this for all
2925−-- non-recursive JSON types. If nothing can be done or the same value
2926−-- would be return, it returns 'mzero'
2927−unStringlyTypeJSON :: MonadPlus m => Value -> m Value
2928−unStringlyTypeJSON (String "true") = return (Bool True)
2929−unStringlyTypeJSON (String "false") = return (Bool False)
2930−unStringlyTypeJSON (String "null") = return Null
2931−unStringlyTypeJSON (String t) = case readMay (T.unpack t) of
2932− Just n -> return (Number n)
2934−unStringlyTypeJSON _ = mzero
2937−parseSettings :: Object -> Parser [UpdatableIndexSetting]
2938−parseSettings o = do
2940− -- slice the index object into singleton hashmaps and try to parse each
2941− parses <- forM (HM.toList o') $ \(k, v) -> do
2942− -- blocks are now nested into the "index" key, which is not how they're serialized
2943− let atRoot = Object (HM.singleton k v)
2944− let atIndex = Object (HM.singleton "index" atRoot)
2945− optional (parseJSON atRoot <|> parseJSON atIndex)
2946− return (catMaybes parses)
2948−oPath :: ToJSON a => NonEmpty Text -> a -> Value
2949−oPath (k :| []) v = object [k .= v]
2950−oPath (k:| (h:t)) v = object [k .= oPath (h :| t) v]
2952−attrFilterJSON :: NonEmpty NodeAttrFilter -> Value
2953−attrFilterJSON fs = object [ n .= T.intercalate "," (toList vs)
2954− | NodeAttrFilter (NodeAttrName n) vs <- toList fs]
2956−parseAttrFilter :: Value -> Parser (NonEmpty NodeAttrFilter)
2957−parseAttrFilter = withObject "NonEmpty NodeAttrFilter" parse
2958− where parse o = case HM.toList o of
2959− [] -> fail "Expected non-empty list of NodeAttrFilters"
2960− x:xs -> DT.mapM (uncurry parse') (x :| xs)
2961− parse' n = withText "Text" $ \t ->
2962− case T.splitOn "," t of
2963− fv:fvs -> return (NodeAttrFilter (NodeAttrName n) (fv :| fvs))
2964− [] -> fail "Expected non-empty list of filter values"
2966−instance ToJSON ReplicaBounds where
2967− toJSON (ReplicasBounded a b) = String (showText a <> "-" <> showText b)
2968− toJSON (ReplicasLowerBounded a) = String (showText a <> "-all")
2969− toJSON ReplicasUnbounded = Bool False
2971−instance FromJSON ReplicaBounds where
2972− parseJSON v = withText "ReplicaBounds" parseText v
2973− <|> withBool "ReplicaBounds" parseBool v
2974− where parseText t = case T.splitOn "-" t of
2975− [a, "all"] -> ReplicasLowerBounded <$> parseReadText a
2976− [a, b] -> ReplicasBounded <$> parseReadText a
2977− <*> parseReadText b
2978− _ -> fail ("Could not parse ReplicaBounds: " <> show t)
2979− parseBool False = pure ReplicasUnbounded
2980− parseBool _ = fail "ReplicasUnbounded cannot be represented with True"
2982−instance ToJSON AllocationPolicy where
2983− toJSON AllocAll = String "all"
2984− toJSON AllocPrimaries = String "primaries"
2985− toJSON AllocNewPrimaries = String "new_primaries"
2986− toJSON AllocNone = String "none"
2988−instance FromJSON AllocationPolicy where
2989− parseJSON = withText "AllocationPolicy" parse
2990− where parse "all" = pure AllocAll
2991− parse "primaries" = pure AllocPrimaries
2992− parse "new_primaries" = pure AllocNewPrimaries
2993− parse "none" = pure AllocNone
2994− parse t = fail ("Invlaid AllocationPolicy: " <> show t)
2996−instance ToJSON InitialShardCount where
2997− toJSON QuorumShards = String "quorum"
2998− toJSON QuorumMinus1Shards = String "quorum-1"
2999− toJSON FullShards = String "full"
3000− toJSON FullMinus1Shards = String "full-1"
3001− toJSON (ExplicitShards x) = toJSON x
3003−instance FromJSON InitialShardCount where
3004− parseJSON v = withText "InitialShardCount" parseText v
3005− <|> ExplicitShards <$> parseJSON v
3006− where parseText "quorum" = pure QuorumShards
3007− parseText "quorum-1" = pure QuorumMinus1Shards
3008− parseText "full" = pure FullShards
3009− parseText "full-1" = pure FullMinus1Shards
3010− parseText _ = mzero
3012−instance ToJSON FSType where
3013− toJSON FSSimple = "simple"
3014− toJSON FSBuffered = "buffered"
3016−instance FromJSON FSType where
3017− parseJSON = withText "FSType" parse
3018− where parse "simple" = pure FSSimple
3019− parse "buffered" = pure FSBuffered
3020− parse t = fail ("Invalid FSType: " <> show t)
3022−instance ToJSON CompoundFormat where
3023− toJSON (CompoundFileFormat x) = Bool x
3024− toJSON (MergeSegmentVsTotalIndex x) = toJSON x
3026−instance FromJSON CompoundFormat where
3027− parseJSON v = CompoundFileFormat <$> parseJSON v
3028− <|> MergeSegmentVsTotalIndex <$> parseJSON v
3030−instance ToJSON NominalDiffTimeJSON where
3031− toJSON (NominalDiffTimeJSON t) = String (showText (round t :: Integer) <> "s")
3033−instance FromJSON NominalDiffTimeJSON where
3034− parseJSON = withText "NominalDiffTime" parse
3035− where parse t = case T.takeEnd 1 t of
3036− "s" -> NominalDiffTimeJSON . fromInteger <$> parseReadText (T.dropEnd 1 t)
3037− _ -> fail "Invalid or missing NominalDiffTime unit (expected s)"
3039−instance ToJSON IndexTemplate where
3040− toJSON (IndexTemplate p s m) = merge
3041− (object [ "template" .= p
3042− , "mappings" .= foldl' merge (object []) m
3046− merge (Object o1) (Object o2) = toJSON $ HM.union o1 o2
3048− merge _ _ = undefined
3050−instance (FromJSON a) => FromJSON (EsResult a) where
3051− parseJSON jsonVal@(Object v) = do
3052− found <- v .:? "found" .!= False
3054− then parseJSON jsonVal
3055− else return Nothing
3056− EsResult <$> v .: "_index" <*>
3060− parseJSON _ = empty
3062−instance (FromJSON a) => FromJSON (EsResultFound a) where
3063− parseJSON (Object v) = EsResultFound <$>
3064− v .: "_version" <*>
3066− parseJSON _ = empty
3068−instance FromJSON EsError where
3069− parseJSON (Object v) = EsError <$>
3072− parseJSON _ = empty
3074−instance FromJSON IndexAliasesSummary where
3075− parseJSON = withObject "IndexAliasesSummary" parse
3076− where parse o = IndexAliasesSummary . mconcat <$> mapM (uncurry go) (HM.toList o)
3077− go ixn = withObject "index aliases" $ \ia -> do
3078− aliases <- ia .:? "aliases" .!= mempty
3079− forM (HM.toList aliases) $ \(aName, v) -> do
3080− let indexAlias = IndexAlias (IndexName ixn) (IndexAliasName (IndexName aName))
3081− IndexAliasSummary indexAlias <$> parseJSON v
3084−instance ToJSON IndexAliasAction where
3085− toJSON (AddAlias ia opts) = object ["add" .= (iaObj <> optsObj)]
3086− where Object iaObj = toJSON ia
3087− Object optsObj = toJSON opts
3088− toJSON (RemoveAlias ia) = object ["remove" .= iaObj]
3089− where Object iaObj = toJSON ia
3091−instance ToJSON IndexAlias where
3092− toJSON IndexAlias {..} = object ["index" .= srcIndex
3093− , "alias" .= indexAlias
3096−instance ToJSON IndexAliasCreate where
3097− toJSON IndexAliasCreate {..} = Object (filterObj <> routingObj)
3098− where filterObj = maybe mempty (HM.singleton "filter" . toJSON) aliasCreateFilter
3099− Object routingObj = maybe (Object mempty) toJSON aliasCreateRouting
3101−instance ToJSON AliasRouting where
3102− toJSON (AllAliasRouting v) = object ["routing" .= v]
3103− toJSON (GranularAliasRouting srch idx) = object (catMaybes prs)
3104− where prs = [("search_routing" .=) <$> srch
3105− ,("index_routing" .=) <$> idx]
3107−instance FromJSON AliasRouting where
3108− parseJSON = withObject "AliasRouting" parse
3109− where parse o = parseAll o <|> parseGranular o
3110− parseAll o = AllAliasRouting <$> o .: "routing"
3111− parseGranular o = do
3112− sr <- o .:? "search_routing"
3113− ir <- o .:? "index_routing"
3114− if isNothing sr && isNothing ir
3115− then fail "Both search_routing and index_routing can't be blank"
3116− else return (GranularAliasRouting sr ir)
3118−instance FromJSON IndexAliasCreate where
3119− parseJSON v = withObject "IndexAliasCreate" parse v
3120− where parse o = IndexAliasCreate <$> optional (parseJSON v)
3123−instance ToJSON SearchAliasRouting where
3124− toJSON (SearchAliasRouting rvs) = toJSON (T.intercalate "," (routingValue <$> toList rvs))
3126−instance FromJSON SearchAliasRouting where
3127− parseJSON = withText "SearchAliasRouting" parse
3128− where parse t = SearchAliasRouting <$> parseNEJSON (String <$> T.splitOn "," t)
3130−instance ToJSON Search where
3131− toJSON (Search query sFilter sort searchAggs highlight sTrackSortScores sFrom sSize _ sFields sSource) =
3132− omitNulls [ "query" .= query
3133− , "filter" .= sFilter
3135− , "aggregations" .= searchAggs
3136− , "highlight" .= highlight
3139− , "track_scores" .= sTrackSortScores
3140− , "fields" .= sFields
3141− , "_source" .= sSource]
3144−instance ToJSON Source where
3145− toJSON NoSource = toJSON False
3146− toJSON (SourcePatterns patterns) = toJSON patterns
3147− toJSON (SourceIncludeExclude incl excl) = object [ "include" .= incl, "exclude" .= excl ]
3149−instance ToJSON PatternOrPatterns where
3150− toJSON (PopPattern pattern) = toJSON pattern
3151− toJSON (PopPatterns patterns) = toJSON patterns
3153−instance ToJSON Include where
3154− toJSON (Include patterns) = toJSON patterns
3156−instance ToJSON Exclude where
3157− toJSON (Exclude patterns) = toJSON patterns
3159−instance ToJSON Pattern where
3160− toJSON (Pattern pattern) = toJSON pattern
3163−instance ToJSON FieldHighlight where
3164− toJSON (FieldHighlight (FieldName fName) (Just fSettings)) =
3165− object [ fName .= fSettings ]
3166− toJSON (FieldHighlight (FieldName fName) Nothing) =
3167− object [ fName .= emptyObject ]
3169−instance ToJSON Highlights where
3170− toJSON (Highlights global fields) =
3171− omitNulls (("fields" .= fields)
3172− : highlightSettingsPairs global)
3174−instance ToJSON HighlightSettings where
3175− toJSON hs = omitNulls (highlightSettingsPairs (Just hs))
3177−highlightSettingsPairs :: Maybe HighlightSettings -> [Pair]
3178−highlightSettingsPairs Nothing = []
3179−highlightSettingsPairs (Just (Plain plh)) = plainHighPairs (Just plh)
3180−highlightSettingsPairs (Just (Postings ph)) = postHighPairs (Just ph)
3181−highlightSettingsPairs (Just (FastVector fvh)) = fastVectorHighPairs (Just fvh)
3184−plainHighPairs :: Maybe PlainHighlight -> [Pair]
3185−plainHighPairs Nothing = []
3186−plainHighPairs (Just (PlainHighlight plCom plNonPost)) =
3187− [ "type" .= String "plain"]
3188− ++ commonHighlightPairs plCom
3189− ++ nonPostingsToPairs plNonPost
3191−postHighPairs :: Maybe PostingsHighlight -> [Pair]
3192−postHighPairs Nothing = []
3193−postHighPairs (Just (PostingsHighlight pCom)) =
3194− ("type" .= String "postings")
3195− : commonHighlightPairs pCom
3197−fastVectorHighPairs :: Maybe FastVectorHighlight -> [Pair]
3198−fastVectorHighPairs Nothing = []
3199−fastVectorHighPairs (Just
3200− (FastVectorHighlight fvCom fvNonPostSettings fvBoundChars
3201− fvBoundMaxScan fvFragOff fvMatchedFields
3203− [ "type" .= String "fvh"
3204− , "boundary_chars" .= fvBoundChars
3205− , "boundary_max_scan" .= fvBoundMaxScan
3206− , "fragment_offset" .= fvFragOff
3207− , "matched_fields" .= fvMatchedFields
3208− , "phraseLimit" .= fvPhraseLim]
3209− ++ commonHighlightPairs fvCom
3210− ++ nonPostingsToPairs fvNonPostSettings
3212−deleteSeveral :: (Eq k, Hashable k) => [k] -> HM.HashMap k v -> HM.HashMap k v
3213−deleteSeveral ks hm = foldr HM.delete hm ks
3215−commonHighlightPairs :: Maybe CommonHighlight -> [Pair]
3216−commonHighlightPairs Nothing = []
3217−commonHighlightPairs (Just (CommonHighlight chScore chForceSource chTag chEncoder
3218− chNoMatchSize chHighlightQuery
3219− chRequireFieldMatch)) =
3220− [ "order" .= chScore
3221− , "force_source" .= chForceSource
3222− , "encoder" .= chEncoder
3223− , "no_match_size" .= chNoMatchSize
3224− , "highlight_query" .= chHighlightQuery
3225− , "require_fieldMatch" .= chRequireFieldMatch]
3226− ++ highlightTagToPairs chTag
3229−nonPostingsToPairs :: Maybe NonPostings -> [Pair]
3230−nonPostingsToPairs Nothing = []
3231−nonPostingsToPairs (Just (NonPostings npFragSize npNumOfFrags)) =
3232− [ "fragment_size" .= npFragSize
3233− , "number_of_fragments" .= npNumOfFrags]
3235−parseNEJSON :: (FromJSON a) => [Value] -> Parser (NonEmpty a)
3236−parseNEJSON [] = fail "Expected non-empty list"
3237−parseNEJSON (x:xs) = DT.mapM parseJSON (x :| xs)
3240−instance ToJSON HighlightEncoder where
3241− toJSON DefaultEncoder = String "default"
3242− toJSON HTMLEncoder = String "html"
3244−highlightTagToPairs :: Maybe HighlightTag -> [Pair]
3245−highlightTagToPairs (Just (TagSchema _)) = [ "scheme" .= String "default"]
3246−highlightTagToPairs (Just (CustomTags (pre, post))) = [ "pre_tags" .= pre
3247− , "post_tags" .= post]
3248−highlightTagToPairs Nothing = []
3250−instance ToJSON SortSpec where
3251− toJSON (DefaultSortSpec
3252− (DefaultSort (FieldName dsSortFieldName) dsSortOrder dsIgnoreUnmapped
3253− dsSortMode dsMissingSort dsNestedFilter)) =
3254− object [dsSortFieldName .= omitNulls base] where
3255− base = [ "order" .= dsSortOrder
3256− , "ignore_unmapped" .= dsIgnoreUnmapped
3257− , "mode" .= dsSortMode
3258− , "missing" .= dsMissingSort
3259− , "nested_filter" .= dsNestedFilter ]
3261− toJSON (GeoDistanceSortSpec gdsSortOrder (GeoPoint (FieldName field) gdsLatLon) units) =
3262− object [ "unit" .= units
3263− , field .= gdsLatLon
3264− , "order" .= gdsSortOrder ]
3267−instance ToJSON SortOrder where
3268− toJSON Ascending = String "asc"
3269− toJSON Descending = String "desc"
3272−instance ToJSON SortMode where
3273− toJSON SortMin = String "min"
3274− toJSON SortMax = String "max"
3275− toJSON SortSum = String "sum"
3276− toJSON SortAvg = String "avg"
3279−instance ToJSON Missing where
3280− toJSON LastMissing = String "_last"
3281− toJSON FirstMissing = String "_first"
3282− toJSON (CustomMissing txt) = String txt
3285−instance ToJSON ScoreType where
3286− toJSON ScoreTypeMax = "max"
3287− toJSON ScoreTypeAvg = "avg"
3288− toJSON ScoreTypeSum = "sum"
3289− toJSON ScoreTypeNone = "none"
3291−instance FromJSON ScoreType where
3292− parseJSON = withText "ScoreType" parse
3293− where parse "max" = pure ScoreTypeMax
3294− parse "avg" = pure ScoreTypeAvg
3295− parse "sum" = pure ScoreTypeSum
3296− parse "none" = pure ScoreTypeNone
3297− parse t = fail ("Unexpected ScoreType: " <> show t)
3299−instance ToJSON Distance where
3300− toJSON (Distance dCoefficient dUnit) =
3301− String boltedTogether where
3302− coefText = showText dCoefficient
3303− (String unitText) = toJSON dUnit
3304− boltedTogether = mappend coefText unitText
3306−instance FromJSON Distance where
3307− parseJSON = withText "Distance" parse
3308− where parse t = Distance <$> parseCoeff nT
3309− <*> parseJSON (String unitT)
3310− where (nT, unitT) = T.span validForNumber t
3311− -- may be a better way to do this
3312− validForNumber '-' = True
3313− validForNumber '.' = True
3314− validForNumber 'e' = True
3315− validForNumber c = isNumber c
3316− parseCoeff "" = fail "Empty string cannot be parsed as number"
3317− parseCoeff s = return (read (T.unpack s))
3319−instance ToJSON DistanceUnit where
3320− toJSON Miles = String "mi"
3321− toJSON Yards = String "yd"
3322− toJSON Feet = String "ft"
3323− toJSON Inches = String "in"
3324− toJSON Kilometers = String "km"
3325− toJSON Meters = String "m"
3326− toJSON Centimeters = String "cm"
3327− toJSON Millimeters = String "mm"
3328− toJSON NauticalMiles = String "nmi"
3331−instance FromJSON DistanceUnit where
3332− parseJSON = withText "DistanceUnit" parse
3333− where parse "mi" = pure Miles
3334− parse "yd" = pure Yards
3335− parse "ft" = pure Feet
3336− parse "in" = pure Inches
3337− parse "km" = pure Kilometers
3338− parse "m" = pure Meters
3339− parse "cm" = pure Centimeters
3340− parse "mm" = pure Millimeters
3341− parse "nmi" = pure NauticalMiles
3342− parse u = fail ("Unrecognized DistanceUnit: " <> show u)
3344−instance ToJSON DistanceType where
3345− toJSON Arc = String "arc"
3346− toJSON SloppyArc = String "sloppy_arc"
3347− toJSON Plane = String "plane"
3349−instance FromJSON DistanceType where
3350− parseJSON = withText "DistanceType" parse
3351− where parse "arc" = pure Arc
3352− parse "sloppy_arc" = pure SloppyArc
3353− parse "plane" = pure Plane
3354− parse t = fail ("Unrecognized DistanceType: " <> show t)
3357−instance ToJSON OptimizeBbox where
3358− toJSON NoOptimizeBbox = String "none"
3359− toJSON (OptimizeGeoFilterType gft) = toJSON gft
3361−instance FromJSON OptimizeBbox where
3362− parseJSON v = withText "NoOptimizeBbox" parseNoOptimize v
3363− <|> parseOptimize v
3364− where parseNoOptimize "none" = pure NoOptimizeBbox
3365− parseNoOptimize _ = mzero
3366− parseOptimize = fmap OptimizeGeoFilterType . parseJSON
3368−instance ToJSON GeoBoundingBoxConstraint where
3369− toJSON (GeoBoundingBoxConstraint
3370− (FieldName gbbcGeoBBField) gbbcConstraintBox cache type') =
3371− object [gbbcGeoBBField .= gbbcConstraintBox
3372− , "_cache" .= cache
3375−instance FromJSON GeoBoundingBoxConstraint where
3376− parseJSON = withObject "GeoBoundingBoxConstraint" parse
3377− where parse o = case HM.toList (deleteSeveral ["type", "_cache"] o) of
3378− [(fn, v)] -> GeoBoundingBoxConstraint (FieldName fn)
3380− <*> o .:? "_cache" .!= defaultCache
3382− _ -> fail "Could not find field name for GeoBoundingBoxConstraint"
3384−instance ToJSON GeoFilterType where
3385− toJSON GeoFilterMemory = String "memory"
3386− toJSON GeoFilterIndexed = String "indexed"
3388−instance FromJSON GeoFilterType where
3389− parseJSON = withText "GeoFilterType" parse
3390− where parse "memory" = pure GeoFilterMemory
3391− parse "indexed" = pure GeoFilterIndexed
3392− parse t = fail ("Unrecognized GeoFilterType: " <> show t)
3394−instance ToJSON GeoBoundingBox where
3395− toJSON (GeoBoundingBox gbbTopLeft gbbBottomRight) =
3396− object ["top_left" .= gbbTopLeft
3397− , "bottom_right" .= gbbBottomRight]
3399−instance FromJSON GeoBoundingBox where
3400− parseJSON = withObject "GeoBoundingBox" parse
3401− where parse o = GeoBoundingBox
3402− <$> o .: "top_left"
3403− <*> o .: "bottom_right"
3405−instance ToJSON LatLon where
3406− toJSON (LatLon lLat lLon) =
3407− object ["lat" .= lLat
3410−instance FromJSON LatLon where
3411− parseJSON = withObject "LatLon" parse
3412− where parse o = LatLon <$> o .: "lat"
3415−-- index for smaller ranges, fielddata for longer ranges
3416−instance ToJSON RangeExecution where
3417− toJSON RangeExecutionIndex = "index"
3418− toJSON RangeExecutionFielddata = "fielddata"
3421−instance FromJSON RangeExecution where
3422− parseJSON = withText "RangeExecution" parse
3423− where parse "index" = pure RangeExecutionIndex
3424− parse "fielddata" = pure RangeExecutionFielddata
3425− parse t = error ("Unrecognized RangeExecution " <> show t)
3427−instance ToJSON RegexpFlags where
3428− toJSON AllRegexpFlags = String "ALL"
3429− toJSON NoRegexpFlags = String "NONE"
3430− toJSON (SomeRegexpFlags (h :| fs)) = String $ T.intercalate "|" flagStrs
3431− where flagStrs = map flagStr . nub $ h:fs
3432− flagStr AnyString = "ANYSTRING"
3433− flagStr Automaton = "AUTOMATON"
3434− flagStr Complement = "COMPLEMENT"
3435− flagStr Empty = "EMPTY"
3436− flagStr Intersection = "INTERSECTION"
3437− flagStr Interval = "INTERVAL"
3439−instance FromJSON RegexpFlags where
3440− parseJSON = withText "RegexpFlags" parse
3441− where parse "ALL" = pure AllRegexpFlags
3442− parse "NONE" = pure NoRegexpFlags
3443− parse t = SomeRegexpFlags <$> parseNEJSON (String <$> T.splitOn "|" t)
3445−instance FromJSON RegexpFlag where
3446− parseJSON = withText "RegexpFlag" parse
3447− where parse "ANYSTRING" = pure AnyString
3448− parse "AUTOMATON" = pure Automaton
3449− parse "COMPLEMENT" = pure Complement
3450− parse "EMPTY" = pure Empty
3451− parse "INTERSECTION" = pure Intersection
3452− parse "INTERVAL" = pure Interval
3453− parse f = fail ("Unknown RegexpFlag: " <> show f)
3455−instance ToJSON Term where
3456− toJSON (Term field value) = object ["term" .= object
3459−instance FromJSON Term where
3460− parseJSON = withObject "Term" parse
3461− where parse o = do termObj <- o .: "term"
3462− case HM.toList termObj of
3463− [(fn, v)] -> Term fn <$> parseJSON v
3464− _ -> fail "Expected object with 1 field-named key"
3466−instance ToJSON BoolMatch where
3467− toJSON (MustMatch term cache) = object ["must" .= term,
3469− toJSON (MustNotMatch term cache) = object ["must_not" .= term,
3471− toJSON (ShouldMatch terms cache) = object ["should" .= fmap toJSON terms,
3474−instance FromJSON BoolMatch where
3475− parseJSON = withObject "BoolMatch" parse
3476− where parse o = mustMatch `taggedWith` "must"
3477− <|> mustNotMatch `taggedWith` "must_not"
3478− <|> shouldMatch `taggedWith` "should"
3479− where taggedWith parser k = parser =<< o .: k
3480− mustMatch t = MustMatch t <$> o .:? "_cache" .!= defaultCache
3481− mustNotMatch t = MustNotMatch t <$> o .:? "_cache" .!= defaultCache
3482− shouldMatch t = ShouldMatch t <$> o .:? "_cache" .!= defaultCache
3484instance (FromJSON a) => FromJSON (SearchResult a) where
529instance (FromJSON a) => FromJSON (SearchResult a) where
3485 parseJSON (Object v) = SearchResult <$>
530 parseJSON (Object v) = SearchResult <$>
@@ -3488,71 +533,10 @@ instance (FromJSON a) => FromJSON (SearchResult a) where
3488 v .: "_shards" <*>
533 v .: "_shards" <*>
3489 v .: "hits" <*>
534 v .: "hits" <*>
3490 v .:? "aggregations" <*>
535 v .:? "aggregations" <*>
3491− v .:? "_scroll_id"
536+ v .:? "_scroll_id" <*>
3492− parseJSON _ = empty
537+ v .:? "suggest"
3494−instance (FromJSON a) => FromJSON (SearchHits a) where
3495− parseJSON (Object v) = SearchHits <$>
3497− v .: "max_score" <*>
3499− parseJSON _ = empty
3501−instance (FromJSON a) => FromJSON (Hit a) where
3502− parseJSON (Object v) = Hit <$>
3507− v .:? "_source" <*>
3509 parseJSON _ = empty
538 parseJSON _ = empty
3511−instance FromJSON ShardResult where
540+newtype ScrollId =
3512− parseJSON (Object v) = ShardResult <$>
541+ ScrollId Text
3513− v .: "total" <*>
542+ deriving (Eq, Show, Ord, ToJSON, FromJSON)
3514− v .: "successful" <*>
3516− parseJSON _ = empty
3519−instance FromJSON DocVersion where
3522− maybe (fail "DocVersion out of range") return $ mkDocVersion i
3524−-- This insanity is because ES *sometimes* returns Replica/Shard counts as strings
3525−instance FromJSON ReplicaCount where
3526− parseJSON v = parseAsInt v
3527− <|> parseAsString v
3528− where parseAsInt = fmap ReplicaCount . parseJSON
3529− parseAsString = withText "ReplicaCount" (fmap ReplicaCount . parseReadText)
3531−instance FromJSON ShardCount where
3532− parseJSON v = parseAsInt v
3533− <|> parseAsString v
3534− where parseAsInt = fmap ShardCount . parseJSON
3535− parseAsString = withText "ShardCount" (fmap ShardCount . parseReadText)
3537−instance Bounded DocVersion where
3538− minBound = DocVersion 1
3539− maxBound = DocVersion 9200000000000000000 -- 9.2e+18
3541−instance Enum DocVersion where
3543− | x /= maxBound = DocVersion (succ $ docVersionNumber x)
3544− | otherwise = succError "DocVersion"
3546− | x /= minBound = DocVersion (pred $ docVersionNumber x)
3547− | otherwise = predError "DocVersion"
3549− fromMaybe (error $ show i ++ " out of DocVersion range") $ mkDocVersion i
3550− fromEnum = docVersionNumber
3551− enumFrom = boundedEnumFrom
3552− enumFromThen = boundedEnumFromThen
3554−-- | Username type used for HTTP Basic authentication. See 'basicAuthHook'.
3555−newtype EsUsername = EsUsername { esUsername :: Text } deriving (Show, Eq)
3557−-- | Password type used for HTTP Basic authentication. See 'basicAuthHook'.
3558−newtype EsPassword = EsPassword { esPassword :: Text } deriving (Show, Eq)