@@ -1,8 +1,8 @@
1+{-# LANGUAGE DeriveGeneric #-}
1{-# LANGUAGE FlexibleContexts #-}
2{-# LANGUAGE FlexibleContexts #-}
2{-# LANGUAGE FlexibleInstances #-}
3{-# LANGUAGE FlexibleInstances #-}
3{-# LANGUAGE GeneralizedNewtypeDeriving #-}
4{-# LANGUAGE GeneralizedNewtypeDeriving #-}
4{-# LANGUAGE MultiParamTypeClasses #-}
5{-# LANGUAGE MultiParamTypeClasses #-}
5−{-# LANGUAGE NamedFieldPuns #-}
6{-# LANGUAGE OverloadedStrings #-}
6{-# LANGUAGE OverloadedStrings #-}
7{-# LANGUAGE RecordWildCards #-}
7{-# LANGUAGE RecordWildCards #-}
8{-# LANGUAGE UndecidableInstances #-}
8{-# LANGUAGE UndecidableInstances #-}
@@ -10,11 +10,11 @@
10-------------------------------------------------------------------------------
10-------------------------------------------------------------------------------
12-- Module : Database.Bloodhound.Types
12-- Module : Database.Bloodhound.Types
13−-- Copyright : (C) 2014, 2018 Chris Allen
13+-- Copyright : (C) 2014 Chris Allen
14-- License : BSD-style (see the file LICENSE)
14-- License : BSD-style (see the file LICENSE)
15-- Maintainer : Chris Allen <cma@bitemyapp.com
15-- Maintainer : Chris Allen <cma@bitemyapp.com
16-- Stability : provisional
16-- Stability : provisional
17−-- Portability : RecordWildCards
17+-- Portability : DeriveGeneric, RecordWildCards
19-- Data types for describing actions and data structures performed to interact
19-- Data types for describing actions and data structures performed to interact
20-- with Elasticsearch. The two main buckets your queries against Elasticsearch
20-- with Elasticsearch. The two main buckets your queries against Elasticsearch
@@ -42,63 +42,25 @@ module Database.Bloodhound.Types
42 , mkTermsAggregation
42 , mkTermsAggregation
43 , mkTermsScriptAggregation
43 , mkTermsScriptAggregation
44 , mkDateHistogram
44 , mkDateHistogram
45− , mkCardinalityAggregation
46 , mkDocVersion
45 , mkDocVersion
48− , mkExtendedStatsAggregation
49 , docVersionNumber
46 , docVersionNumber
52 , toDateHistogram
48 , toDateHistogram
54 , omitNulls
49 , omitNulls
57− , BHEnv
52+ , BHEnv(..)
62 , MonadBH(..)
53 , MonadBH(..)
63 , Version(..)
54 , Version(..)
67 , Status(..)
55 , Status(..)
68 , Existence(..)
56 , Existence(..)
69 , NullValue(..)
57 , NullValue(..)
70 , IndexSettings(..)
58 , IndexSettings(..)
71− , UpdatableIndexSetting(..)
72− , IndexSettingsSummary(..)
73− , AllocationPolicy(..)
81− , InitialShardCount(..)
85 , IndexTemplate(..)
59 , IndexTemplate(..)
86 , Server(..)
60 , Server(..)
88 , EsResult(..)
62 , EsResult(..)
89 , EsResultFound(..)
63 , EsResultFound(..)
91− , EsProtocolException(..)
94− , IndexAliasAction(..)
95− , IndexAliasCreate(..)
96− , IndexAliasSummary(..)
97− , IndexAliasesSummary(..)
99− , SearchAliasRouting(..)
100− , IndexAliasRouting(..)
102 , DocVersion
64 , DocVersion
103 , ExternalDocVersion(..)
65 , ExternalDocVersion(..)
104 , VersionControl(..)
66 , VersionControl(..)
@@ -108,7 +70,7 @@ module Database.Bloodhound.Types
108 , Search(..)
70 , Search(..)
109 , SearchType(..)
71 , SearchType(..)
110 , SearchResult(..)
72 , SearchResult(..)
111− , ScrollId(..)
73+ , ScrollId
112 , SearchHits(..)
74 , SearchHits(..)
113 , TrackSortScores
75 , TrackSortScores
114 , From(..)
76 , From(..)
@@ -120,8 +82,8 @@ module Database.Bloodhound.Types
120 , Pattern(..)
82 , Pattern(..)
121 , ShardResult(..)
83 , ShardResult(..)
122 , Hit(..)
84 , Hit(..)
124 , Filter(..)
85 , Filter(..)
125 , BoolMatch(..)
87 , BoolMatch(..)
126 , Term(..)
88 , Term(..)
127 , GeoPoint(..)
89 , GeoPoint(..)
@@ -148,22 +110,8 @@ module Database.Bloodhound.Types
148 , RegexpFlags(..)
110 , RegexpFlags(..)
149 , RegexpFlag(..)
111 , RegexpFlag(..)
150 , FieldName(..)
112 , FieldName(..)
154 , Script(..)
113 , Script(..)
155− , ScriptLanguage(..)
161 , IndexName(..)
114 , IndexName(..)
162− , IndexSelection(..)
165− , ForceMergeIndexSettings(..)
166− , defaultForceMergeIndexSettings
167 , TemplateName(..)
115 , TemplateName(..)
168 , TemplatePattern(..)
116 , TemplatePattern(..)
169 , MappingName(..)
117 , MappingName(..)
@@ -187,19 +135,8 @@ module Database.Bloodhound.Types
187 , BoolQuery(..)
135 , BoolQuery(..)
188 , BoostingQuery(..)
136 , BoostingQuery(..)
189 , CommonTermsQuery(..)
137 , CommonTermsQuery(..)
190− , FunctionScoreQuery(..)
193− , FunctionScoreFunctions(..)
194− , ComponentFunctionScoreFunction(..)
195− , FunctionScoreFunction(..)
198− , FieldValueFactor(..)
200− , FactorModifier(..)
201− , FactorMissingFieldValue(..)
202 , DisMaxQuery(..)
138 , DisMaxQuery(..)
203 , FuzzyLikeThisQuery(..)
140 , FuzzyLikeThisQuery(..)
204 , FuzzyLikeFieldQuery(..)
141 , FuzzyLikeFieldQuery(..)
205 , FuzzyQuery(..)
142 , FuzzyQuery(..)
@@ -215,16 +152,10 @@ module Database.Bloodhound.Types
215 , RangeQuery(..)
152 , RangeQuery(..)
216 , RegexpQuery(..)
153 , RegexpQuery(..)
217 , QueryString(..)
154 , QueryString(..)
218− , TemplateQueryInline(..)
219− , TemplateQueryKeyValuePairs(..)
221 , BooleanOperator(..)
155 , BooleanOperator(..)
222 , ZeroTermsQuery(..)
156 , ZeroTermsQuery(..)
223 , CutoffFrequency(..)
157 , CutoffFrequency(..)
224 , Analyzer(..)
158 , Analyzer(..)
228 , MaxExpansions(..)
159 , MaxExpansions(..)
229 , Lenient(..)
160 , Lenient(..)
230 , MatchQueryType(..)
161 , MatchQueryType(..)
@@ -271,118 +202,16 @@ module Database.Bloodhound.Types
271 , CollectionMode(..)
202 , CollectionMode(..)
272 , TermOrder(..)
203 , TermOrder(..)
273 , TermInclusion(..)
204 , TermInclusion(..)
274− , SnapshotRepoSelection(..)
275− , GenericSnapshotRepo(..)
277− , SnapshotRepoConversionError(..)
278− , SnapshotRepoType(..)
279− , GenericSnapshotRepoSettings(..)
280− , SnapshotRepoUpdateSettings(..)
281− , defaultSnapshotRepoUpdateSettings
282− , SnapshotRepoName(..)
283− , SnapshotRepoPattern(..)
284− , SnapshotVerification(..)
285− , SnapshotNodeVerification(..)
292− , NodeBreakersStats(..)
293− , NodeBreakerStats(..)
295− , NodeTransportStats(..)
297− , NodeDataPathStats(..)
298− , NodeFSTotalStats(..)
299− , NodeNetworkStats(..)
300− , NodeThreadPoolsStats(..)
301− , NodeThreadPoolStats(..)
303− , JVMBufferPoolStats(..)
306− , NodeProcessStats(..)
309− , NodeIndicesStats(..)
313− , NodePluginInfo(..)
315− , NodeTransportInfo(..)
316− , BoundTransportAddress(..)
317− , NodeNetworkInfo(..)
319− , NetworkInterfaceName(..)
320− , NodeNetworkInterface(..)
321− , NodeThreadPoolsInfo(..)
322− , NodeThreadPoolInfo(..)
323− , ThreadPoolSize(..)
324− , ThreadPoolType(..)
327− , JVMGCCollector(..)
332− , NodeProcessInfo(..)
333− , FsSnapshotRepo(..)
334− , SnapshotCreateSettings(..)
335− , defaultSnapshotCreateSettings
336− , SnapshotSelection(..)
337− , SnapshotPattern(..)
339− , SnapshotShardFailure(..)
343− , SnapshotRestoreSettings(..)
344− , defaultSnapshotRestoreSettings
345− , RestoreRenamePattern(..)
346− , RestoreRenameToken(..)
350− , RestoreIndexSettings(..)
353− , PhraseSuggester(..)
354− , PhraseSuggesterHighlighter(..)
355− , PhraseSuggesterCollate(..)
357− , SuggestOptions(..)
358− , SuggestResponse(..)
359− , NamedSuggestionResponse(..)
360− , DirectGenerators(..)
361− , mkDirectGenerators
362− , DirectGeneratorSuggestModeTypes (..)
364 , Aggregation(..)
206 , Aggregation(..)
365 , Aggregations
207 , Aggregations
366 , AggregationResults
208 , AggregationResults
368 , Bucket(..)
209 , Bucket(..)
369 , BucketAggregation(..)
210 , BucketAggregation(..)
370 , TermsAggregation(..)
211 , TermsAggregation(..)
371− , MissingAggregation(..)
372 , ValueCountAggregation(..)
212 , ValueCountAggregation(..)
373 , FilterAggregation(..)
213 , FilterAggregation(..)
374− , CardinalityAggregation(..)
375− , MaxAggregation(..)
376 , DateHistogramAggregation(..)
214 , DateHistogramAggregation(..)
377− , DateRangeAggregation(..)
378− , DateRangeAggRange(..)
380− , DateMathAnchor(..)
381− , DateMathModifier(..)
383− , TopHitsAggregation(..)
384− , StatisticsAggregation(..)
387 , Highlights(..)
216 , Highlights(..)
388 , FieldHighlight(..)
217 , FieldHighlight(..)
@@ -396,36 +225,505 @@ module Database.Bloodhound.Types
396 , HighlightTag(..)
225 , HighlightTag(..)
397 , HitHighlight
226 , HitHighlight
400 , TermsResult(..)
228 , TermsResult(..)
401 , DateHistogramResult(..)
229 , DateHistogramResult(..)
402− , DateRangeResult(..)
230+ ) where
403− , TopHitResult(..)
231+
404−
232+import Control.Applicative
405− , EsUsername(..)
233+import Control.Monad.Catch
406− , EsPassword(..)
234+import Control.Monad.Except
407−
235+import Control.Monad.Reader
408− , Analysis(..)
236+import Control.Monad.State
409− , AnalyzerDefinition(..)
237+import Control.Monad.Writer
410− , TokenizerDefinition(..)
238+import Data.Aeson
411− , TokenFilterDefinition(..)
239+import Data.Aeson.Types (Pair, emptyObject, parseMaybe)
412− , CharFilterDefinition(..)
240+import qualified Data.ByteString.Lazy.Char8 as L
413− , Ngram(..)
241+import qualified Data.HashMap.Strict as HM (union)
414− , TokenChar(..)
242+import Data.List (foldl', nub)
415− , Shingle(..)
243+import Data.List.NonEmpty (NonEmpty (..), toList)
416− , Language(..)
244+import qualified Data.Map.Strict as M
417− ) where
245+import Data.Maybe
418−
246+import Data.Text (Text)
419−import Bloodhound.Import
247+import qualified Data.Text as T
420−
248+import Data.Time.Clock (UTCTime)
421−import Database.Bloodhound.Internal.Aggregation
249+import qualified Data.Vector as V
422−import Database.Bloodhound.Internal.Analysis
250+import GHC.Enum
423−import Database.Bloodhound.Internal.Client
251+import GHC.Generics (Generic)
424−import Database.Bloodhound.Internal.Highlight
252+import Network.HTTP.Client
425−import Database.Bloodhound.Internal.Newtypes
253+import qualified Network.HTTP.Types.Method as NHTM
426−import Database.Bloodhound.Internal.Query
254+
427−import Database.Bloodhound.Internal.Sort
255+import Database.Bloodhound.Types.Class
428−import Database.Bloodhound.Internal.Suggest
256+
258+-- >>> :set -XOverloadedStrings
259+-- >>> import Database.Bloodhound
260+-- >>> let testServer = (Server "http://localhost:9200")
261+-- >>> let testIndex = IndexName "twitter"
262+-- >>> let testMapping = MappingName "tweet"
263+-- >>> let defaultIndexSettings = IndexSettings (ShardCount 3) (ReplicaCount 2)
265+-- defaultIndexSettings is exported by Database.Bloodhound as well
266+-- no trailing slashes in servers, library handles building the path.
268+{-| Common environment for Elasticsearch calls. Connections will be
269+ pipelined according to the provided HTTP connection manager.
271+data BHEnv = BHEnv { bhServer :: Server
272+ , bhManager :: Manager
275+{-| All API calls to Elasticsearch operate within
276+ MonadBH. The idea is that it can be easily embedded in your
277+ own monad transformer stack. A default instance for a ReaderT and
278+ alias 'BH' is provided for the simple case.
280+class (Functor m, Applicative m, MonadIO m) => MonadBH m where
283+newtype BH m a = BH {
284+ unBH :: ReaderT BHEnv m a
285+ } deriving ( Functor
299+instance MonadTrans BH where
302+instance (MonadReader r m) => MonadReader r (BH m) where
304+ local f (BH (ReaderT m)) = BH $ ReaderT $ \r ->
307+instance (Functor m, Applicative m, MonadIO m) => MonadBH (BH m) where
308+ getBHEnv = BH getBHEnv
311+instance (Functor m, Applicative m, MonadIO m) => MonadBH (ReaderT BHEnv m) where
314+runBH :: BHEnv -> BH m a -> m a
315+runBH e f = runReaderT (unBH f) e
317+{-| 'Version' is embedded in 'Status' -}
318+data Version = Version { number :: Text
319+ , build_hash :: Text
320+ , build_timestamp :: UTCTime
321+ , build_snapshot :: Bool
322+ , lucene_version :: Text } deriving (Eq, Show, Generic)
324+{-| 'Status' is a data type for describing the JSON body returned by
325+ Elasticsearch when you query its status. This was deprecated in 1.2.0.
327+ <http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-status.html#indices-status>
330+data Status = Status { ok :: Maybe Bool
333+ , version :: Version
334+ , tagline :: Text } deriving (Eq, Show)
336+{-| 'IndexSettings' is used to configure the shards and replicas when you create
337+ an Elasticsearch Index.
339+ <http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-create-index.html>
343+ IndexSettings { indexShards :: ShardCount
344+ , indexReplicas :: ReplicaCount } deriving (Eq, Show)
346+{-| 'defaultIndexSettings' is an 'IndexSettings' with 3 shards and 2 replicas. -}
347+defaultIndexSettings :: IndexSettings
348+defaultIndexSettings = IndexSettings (ShardCount 3) (ReplicaCount 2)
350+{-| 'Reply' and 'Method' are type synonyms from 'Network.HTTP.Types.Method.Method' -}
351+type Reply = Network.HTTP.Client.Response L.ByteString
352+type Method = NHTM.Method
354+{-| 'OpenCloseIndex' is a sum type for opening and closing indices.
356+ <http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-open-close.html>
358+data OpenCloseIndex = OpenIndex | CloseIndex deriving (Eq, Show)
360+data FieldType = GeoPointType
366+ | ByteType deriving (Eq, Show)
368+data FieldDefinition =
369+ FieldDefinition { fieldType :: FieldType } deriving (Eq, Show)
371+{-| An 'IndexTemplate' defines a template that will automatically be
372+ applied to new indices created. The templates include both
373+ 'IndexSettings' and mappings, and a simple 'TemplatePattern' that
374+ controls if the template will be applied to the index created.
375+ Specify mappings as follows: @[toJSON TweetMapping, ...]@
377+ https://www.elastic.co/guide/en/elasticsearch/reference/1.7/indices-templates.html
380+ IndexTemplate { templatePattern :: TemplatePattern
381+ , templateSettings :: Maybe IndexSettings
382+ , templateMappings :: [Value]
386+ MappingField { mappingFieldName :: FieldName
387+ , fieldDefinition :: FieldDefinition } deriving (Eq, Show)
389+{-| Support for type reification of 'Mapping's is currently incomplete, for
390+ now the mapping API verbiage expects a 'ToJSON'able blob.
392+ Indexes have mappings, mappings are schemas for the documents contained in the
393+ index. I'd recommend having only one mapping per index, always having a mapping,
394+ and keeping different kinds of documents separated if possible.
396+data Mapping = Mapping { typeName :: TypeName
397+ , mappingFields :: [MappingField] } deriving (Eq, Show)
399+{-| 'BulkOperation' is a sum type for expressing the four kinds of bulk
400+ operation index, create, delete, and update. 'BulkIndex' behaves like an
401+ "upsert", 'BulkCreate' will fail if a document already exists at the DocId.
403+ <http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-bulk.html#docs-bulk>
406+ BulkIndex IndexName MappingName DocId Value
407+ | BulkCreate IndexName MappingName DocId Value
408+ | BulkDelete IndexName MappingName DocId
409+ | BulkUpdate IndexName MappingName DocId Value deriving (Eq, Show)
411+{-| 'EsResult' describes the standard wrapper JSON document that you see in
412+ successful Elasticsearch lookups or lookups that couldn't find the document.
414+data EsResult a = EsResult { _index :: Text
417+ , foundResult :: Maybe (EsResultFound a)} deriving (Eq, Show)
420+{-| 'EsResultFound' contains the document and its metadata inside of an
421+ 'EsResult' when the document was successfully found.
423+data EsResultFound a = EsResultFound { _version :: DocVersion
424+ , _source :: a } deriving (Eq, Show)
428+{-| 'DocVersion' is an integer version number for a document between 1
429+and 9.2e+18 used for <<https://www.elastic.co/guide/en/elasticsearch/guide/current/optimistic-concurrency-control.html optimistic concurrency control>>.
431+newtype DocVersion = DocVersion {
432+ docVersionNumber :: Int
433+ } deriving (Eq, Show, Ord, ToJSON)
435+-- | Smart constructor for in-range doc version
436+mkDocVersion :: Int -> Maybe DocVersion
438+ | i >= (docVersionNumber minBound) && i <= (docVersionNumber maxBound) =
440+ | otherwise = Nothing
443+{-| 'ExternalDocVersion' is a convenience wrapper if your code uses its
444+own version numbers instead of ones from ES.
446+newtype ExternalDocVersion = ExternalDocVersion DocVersion
447+ deriving (Eq, Show, Ord, Bounded, Enum, ToJSON)
449+{-| 'VersionControl' is specified when indexing documents as a
450+optimistic concurrency control.
452+data VersionControl = NoVersionControl
453+ -- ^ Don't send a version. This is a pure overwrite.
454+ | InternalVersion DocVersion
455+ -- ^ Use the default ES versioning scheme. Only
456+ -- index the document if the version is the same
457+ -- as the one specified. Only applicable to
458+ -- updates, as you should be getting Version from
460+ | ExternalGT ExternalDocVersion
461+ -- ^ Use your own version numbering. Only index
462+ -- the document if the version is strictly higher
463+ -- OR the document doesn't exist. The given
464+ -- version will be used as the new version number
465+ -- for the stored document. N.B. All updates must
466+ -- increment this number, meaning there is some
467+ -- global, external ordering of updates.
468+ | ExternalGTE ExternalDocVersion
469+ -- ^ Use your own version numbering. Only index
470+ -- the document if the version is equal or higher
471+ -- than the stored version. Will succeed if there
472+ -- is no existing document. The given version will
473+ -- be used as the new version number for the
474+ -- stored document. Use with care, as this could
475+ -- result in data loss.
476+ | ForceVersion ExternalDocVersion
477+ -- ^ The document will always be indexed and the
478+ -- given version will be the new version. This is
479+ -- typically used for correcting errors. Use with
480+ -- care, as this could result in data loss.
481+ deriving (Show, Eq, Ord)
483+{-| 'DocumentParent' is used to specify a parent document.
485+newtype DocumentParent = DocumentParent DocId
488+{-| 'IndexDocumentSettings' are special settings supplied when indexing
489+a document. For the best backwards compatiblity when new fields are
490+added, you should probably prefer to start with 'defaultIndexDocumentSettings'
492+data IndexDocumentSettings =
493+ IndexDocumentSettings { idsVersionControl :: VersionControl
494+ , idsParent :: Maybe DocumentParent
495+ } deriving (Eq, Show)
497+{-| Reasonable default settings. Chooses no version control and no parent.
499+defaultIndexDocumentSettings :: IndexDocumentSettings
500+defaultIndexDocumentSettings = IndexDocumentSettings NoVersionControl Nothing
502+{-| 'Sort' is a synonym for a list of 'SortSpec's. Sort behavior is order
503+ dependent with later sorts acting as tie-breakers for earlier sorts.
505+type Sort = [SortSpec]
507+{-| The two main kinds of 'SortSpec' are 'DefaultSortSpec' and
508+ 'GeoDistanceSortSpec'. The latter takes a 'SortOrder', 'GeoPoint', and
509+ 'DistanceUnit' to express "nearness" to a single geographical point as a
512+<http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#search-request-sort>
514+data SortSpec = DefaultSortSpec DefaultSort
515+ | GeoDistanceSortSpec SortOrder GeoPoint DistanceUnit deriving (Eq, Show)
517+{-| 'DefaultSort' is usually the kind of 'SortSpec' you'll want. There's a
518+ 'mkSort' convenience function for when you want to specify only the most
521+<http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#search-request-sort>
524+ DefaultSort { sortFieldName :: FieldName
525+ , sortOrder :: SortOrder
527+ , ignoreUnmapped :: Bool
528+ , sortMode :: Maybe SortMode
529+ , missingSort :: Maybe Missing
530+ , nestedFilter :: Maybe Filter } deriving (Eq, Show)
532+{-| 'SortOrder' is 'Ascending' or 'Descending', as you might expect. These get
533+ encoded into "asc" or "desc" when turned into JSON.
535+<http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#search-request-sort>
537+data SortOrder = Ascending
538+ | Descending deriving (Eq, Show)
540+{-| 'Missing' prescribes how to handle missing fields. A missing field can be
541+ sorted last, first, or using a custom value as a substitute.
543+<http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#_missing_values>
545+data Missing = LastMissing
547+ | CustomMissing Text deriving (Eq, Show)
549+{-| 'SortMode' prescribes how to handle sorting array/multi-valued fields.
551+http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#_sort_mode_option
553+data SortMode = SortMin
556+ | SortAvg deriving (Eq, Show)
558+{-| 'mkSort' defaults everything but the 'FieldName' and the 'SortOrder' so
559+ that you can concisely describe the usual kind of 'SortSpec's you want.
561+mkSort :: FieldName -> SortOrder -> DefaultSort
562+mkSort fieldName sOrder = DefaultSort fieldName sOrder False Nothing Nothing Nothing
564+{-| 'Cache' is for telling ES whether it should cache a 'Filter' not.
565+ 'Query's cannot be cached.
567+type Cache = Bool -- caching on/off
568+defaultCache :: Cache
571+{-| 'PrefixValue' is used in 'PrefixQuery' as the main query component.
573+type PrefixValue = Text
575+{-| 'BooleanOperator' is the usual And/Or operators with an ES compatible
576+ JSON encoding baked in. Used all over the place.
578+data BooleanOperator = And | Or deriving (Eq, Show)
580+{-| 'ShardCount' is part of 'IndexSettings'
582+newtype ShardCount = ShardCount Int deriving (Eq, Show, Generic)
584+{-| 'ReplicaCount' is part of 'IndexSettings'
586+newtype ReplicaCount = ReplicaCount Int deriving (Eq, Show, Generic)
588+{-| 'Server' is used with the client functions to point at the ES instance
590+newtype Server = Server Text deriving (Eq, Show)
592+{-| 'IndexName' is used to describe which index to query/create/delete
594+newtype IndexName = IndexName Text deriving (Eq, Generic, Show)
596+{-| 'TemplateName' is used to describe which template to query/create/delete
598+newtype TemplateName = TemplateName Text deriving (Eq, Show, Generic)
600+{-| 'TemplatePattern' represents a pattern which is matched against index names
602+newtype TemplatePattern = TemplatePattern Text deriving (Eq, Show, Generic)
604+{-| 'MappingName' is part of mappings which are how ES describes and schematizes
605+ the data in the indices.
607+newtype MappingName = MappingName Text deriving (Eq, Generic, Show)
609+{-| 'DocId' is a generic wrapper value for expressing unique Document IDs.
610+ Can be set by the user or created by ES itself. Often used in client
611+ functions for poking at specific documents.
613+newtype DocId = DocId Text deriving (Eq, Generic, Show)
615+{-| 'QueryString' is used to wrap query text bodies, be they human written or not.
617+newtype QueryString = QueryString Text deriving (Eq, Generic, Show)
619+{-| 'FieldName' is used all over the place wherever a specific field within
620+ a document needs to be specified, usually in 'Query's or 'Filter's.
622+newtype FieldName = FieldName Text deriving (Eq, Show)
625+{-| 'Script' is often used in place of 'FieldName' to specify more
626+complex ways of extracting a value from a document.
628+newtype Script = Script { scriptText :: Text } deriving (Eq, Show)
630+{-| 'CacheName' is used in 'RegexpFilter' for describing the
631+ 'CacheKey' keyed caching behavior.
633+newtype CacheName = CacheName Text deriving (Eq, Show)
635+{-| 'CacheKey' is used in 'RegexpFilter' to key regex caching.
638+ CacheKey Text deriving (Eq, Show)
640+ Existence Bool deriving (Eq, Show)
642+ NullValue Bool deriving (Eq, Show)
643+newtype CutoffFrequency =
644+ CutoffFrequency Double deriving (Eq, Show, Generic)
646+ Analyzer Text deriving (Eq, Show, Generic)
647+newtype MaxExpansions =
648+ MaxExpansions Int deriving (Eq, Show, Generic)
650+{-| 'Lenient', if set to true, will cause format based failures to be
651+ ignored. I don't know what the bloody default is, Elasticsearch
652+ documentation didn't say what it was. Let me know if you figure it out.
655+ Lenient Bool deriving (Eq, Show, Generic)
657+ Tiebreaker Double deriving (Eq, Show, Generic)
659+ Boost Double deriving (Eq, Show, Generic)
661+ BoostTerms Double deriving (Eq, Show, Generic)
663+{-| 'MinimumMatch' controls how many should clauses in the bool query should
664+ match. Can be an absolute value (2) or a percentage (30%) or a
665+ combination of both.
667+newtype MinimumMatch =
668+ MinimumMatch Int deriving (Eq, Show, Generic)
669+newtype MinimumMatchText =
670+ MinimumMatchText Text deriving (Eq, Show)
671+newtype DisableCoord =
672+ DisableCoord Bool deriving (Eq, Show, Generic)
673+newtype IgnoreTermFrequency =
674+ IgnoreTermFrequency Bool deriving (Eq, Show, Generic)
675+newtype MinimumTermFrequency =
676+ MinimumTermFrequency Int deriving (Eq, Show, Generic)
677+newtype MaxQueryTerms =
678+ MaxQueryTerms Int deriving (Eq, Show, Generic)
680+ Fuzziness Double deriving (Eq, Show, Generic)
682+{-| 'PrefixLength' is the prefix length used in queries, defaults to 0. -}
683+newtype PrefixLength =
684+ PrefixLength Int deriving (Eq, Show, Generic)
686+ TypeName Text deriving (Eq, Show, Generic)
687+newtype PercentMatch =
688+ PercentMatch Double deriving (Eq, Show, Generic)
690+ StopWord Text deriving (Eq, Show, Generic)
692+ QueryPath Text deriving (Eq, Show, Generic)
694+{-| Allowing a wildcard at the beginning of a word (eg "*ing") is particularly
695+ heavy, because all terms in the index need to be examined, just in case
696+ they match. Leading wildcards can be disabled by setting
697+ 'AllowLeadingWildcard' to false. -}
698+newtype AllowLeadingWildcard =
699+ AllowLeadingWildcard Bool deriving (Eq, Show, Generic)
700+newtype LowercaseExpanded =
701+ LowercaseExpanded Bool deriving (Eq, Show, Generic)
702+newtype EnablePositionIncrements =
703+ EnablePositionIncrements Bool deriving (Eq, Show, Generic)
705+{-| By default, wildcard terms in a query are not analyzed.
706+ Setting 'AnalyzeWildcard' to true enables best-effort analysis.
708+newtype AnalyzeWildcard = AnalyzeWildcard Bool deriving (Eq, Show, Generic)
710+{-| 'GeneratePhraseQueries' defaults to false.
712+newtype GeneratePhraseQueries =
713+ GeneratePhraseQueries Bool deriving (Eq, Show, Generic)
715+{-| 'Locale' is used for string conversions - defaults to ROOT.
717+newtype Locale = Locale Text deriving (Eq, Show, Generic)
718+newtype MaxWordLength = MaxWordLength Int deriving (Eq, Show, Generic)
719+newtype MinWordLength = MinWordLength Int deriving (Eq, Show, Generic)
721+{-| 'PhraseSlop' sets the default slop for phrases, 0 means exact
722+ phrase matches. Default is 0.
724+newtype PhraseSlop = PhraseSlop Int deriving (Eq, Show, Generic)
725+newtype MinDocFrequency = MinDocFrequency Int deriving (Eq, Show, Generic)
726+newtype MaxDocFrequency = MaxDocFrequency Int deriving (Eq, Show, Generic)
430{-| 'unpackId' is a silly convenience function that gets used once.
728{-| 'unpackId' is a silly convenience function that gets used once.
@@ -433,6 +731,8 @@ unpackId :: DocId -> Text
433unpackId (DocId docId) = docId
731unpackId (DocId docId) = docId
435type TrackSortScores = Bool
733type TrackSortScores = Bool
734+newtype From = From Int deriving (Eq, Show, ToJSON)
735+newtype Size = Size Int deriving (Eq, Show, ToJSON)
437data Search = Search { queryBody :: Maybe Query
737data Search = Search { queryBody :: Maybe Query
438 , filterBody :: Maybe Filter
738 , filterBody :: Maybe Filter
@@ -444,87 +744,1638 @@ data Search = Search { queryBody :: Maybe Query
444 , from :: From
744 , from :: From
445 , size :: Size
745 , size :: Size
446 , searchType :: SearchType
746 , searchType :: SearchType
447− , searchAfterKey :: Maybe SearchAfterKey
448 , fields :: Maybe [FieldName]
747 , fields :: Maybe [FieldName]
449− , scriptFields :: Maybe ScriptFields
450− , source :: Maybe Source
748+ , source :: Maybe Source } deriving (Eq, Show)
451− , suggestBody :: Maybe Suggest -- ^ Only one Suggestion request / response per Search is supported.
452− } deriving (Eq, Show)
455−instance ToJSON Search where
456− toJSON (Search mquery sFilter sort searchAggs
457− highlight sTrackSortScores sFrom sSize _ sAfter sFields
458− sScriptFields sSource sSuggest) =
459− omitNulls [ "query" .= query'
461− , "aggregations" .= searchAggs
462− , "highlight" .= highlight
465− , "track_scores" .= sTrackSortScores
466− , "search_after" .= sAfter
467− , "fields" .= sFields
468− , "script_fields" .= sScriptFields
469− , "_source" .= sSource
470− , "suggest" .= sSuggest]
472− where query' = case sFilter of
477− $ mkBoolQuery (maybeToList mquery)
480data SearchType = SearchTypeQueryThenFetch
750data SearchType = SearchTypeQueryThenFetch
481 | SearchTypeDfsQueryThenFetch
751 | SearchTypeDfsQueryThenFetch
754+ | SearchTypeQueryAndFetch
755+ | SearchTypeDfsQueryAndFetch
482 deriving (Eq, Show)
756 deriving (Eq, Show)
484data Source =
758data Source =
485− NoSource
759+ NoSource
486 | SourcePatterns PatternOrPatterns
760 | SourcePatterns PatternOrPatterns
487 | SourceIncludeExclude Include Exclude
761 | SourceIncludeExclude Include Exclude
488− deriving (Eq, Show)
762+ deriving (Show, Eq)
764+data PatternOrPatterns = PopPattern Pattern
765+ | PopPatterns [Pattern] deriving (Eq, Show)
767+data Include = Include [Pattern] deriving (Eq, Show)
768+data Exclude = Exclude [Pattern] deriving (Eq, Show)
770+newtype Pattern = Pattern Text deriving (Eq, Show)
772+data Highlights = Highlights { globalsettings :: Maybe HighlightSettings
773+ , highlightFields :: [FieldHighlight]
774+ } deriving (Show, Eq)
776+data FieldHighlight = FieldHighlight FieldName (Maybe HighlightSettings)
780+data HighlightSettings = Plain PlainHighlight
781+ | Postings PostingsHighlight
782+ | FastVector FastVectorHighlight
784+data PlainHighlight =
785+ PlainHighlight { plainCommon :: Maybe CommonHighlight
786+ , plainNonPost :: Maybe NonPostings } deriving (Show, Eq)
788+ -- This requires that index_options are set to 'offset' in the mapping.
789+data PostingsHighlight = PostingsHighlight (Maybe CommonHighlight) deriving (Show, Eq)
791+-- This requires that term_vector is set to 'with_positions_offsets' in the mapping.
792+data FastVectorHighlight =
793+ FastVectorHighlight { fvCommon :: Maybe CommonHighlight
794+ , fvNonPostSettings :: Maybe NonPostings
795+ , boundaryChars :: Maybe Text
796+ , boundaryMaxScan :: Maybe Int
797+ , fragmentOffset :: Maybe Int
798+ , matchedFields :: [Text]
799+ , phraseLimit :: Maybe Int
800+ } deriving (Show, Eq)
802+data CommonHighlight =
803+ CommonHighlight { order :: Maybe Text
804+ , forceSource :: Maybe Bool
805+ , tag :: Maybe HighlightTag
806+ , encoder :: Maybe HighlightEncoder
807+ , noMatchSize :: Maybe Int
808+ , highlightQuery :: Maybe Query
809+ , requireFieldMatch :: Maybe Bool
810+ } deriving (Show, Eq)
812+-- Settings that are only applicable to FastVector and Plain highlighters.
814+ NonPostings { fragmentSize :: Maybe Int
815+ , numberOfFragments :: Maybe Int} deriving (Show, Eq)
817+data HighlightEncoder = DefaultEncoder
821+-- NOTE: Should the tags use some kind of HTML type, rather than Text?
822+data HighlightTag = TagSchema Text
823+ | CustomTags ([Text], [Text]) -- Only uses more than the first value in the lists if fvh
828+ TermQuery Term (Maybe Boost)
829+ | TermsQuery (NonEmpty Term)
830+ | QueryMatchQuery MatchQuery
831+ | QueryMultiMatchQuery MultiMatchQuery
832+ | QueryBoolQuery BoolQuery
833+ | QueryBoostingQuery BoostingQuery
834+ | QueryCommonTermsQuery CommonTermsQuery
835+ | ConstantScoreFilter Filter Boost
836+ | ConstantScoreQuery Query Boost
837+ | QueryDisMaxQuery DisMaxQuery
838+ | QueryFilteredQuery FilteredQuery
839+ | QueryFuzzyLikeThisQuery FuzzyLikeThisQuery
840+ | QueryFuzzyLikeFieldQuery FuzzyLikeFieldQuery
841+ | QueryFuzzyQuery FuzzyQuery
842+ | QueryHasChildQuery HasChildQuery
843+ | QueryHasParentQuery HasParentQuery
844+ | IdsQuery MappingName [DocId]
845+ | QueryIndicesQuery IndicesQuery
846+ | MatchAllQuery (Maybe Boost)
847+ | QueryMoreLikeThisQuery MoreLikeThisQuery
848+ | QueryMoreLikeThisFieldQuery MoreLikeThisFieldQuery
849+ | QueryNestedQuery NestedQuery
850+ | QueryPrefixQuery PrefixQuery
851+ | QueryQueryStringQuery QueryStringQuery
852+ | QuerySimpleQueryStringQuery SimpleQueryStringQuery
853+ | QueryRangeQuery RangeQuery
854+ | QueryRegexpQuery RegexpQuery
858+ RegexpQuery { regexpQueryField :: FieldName
859+ , regexpQuery :: Regexp
860+ , regexpQueryFlags :: RegexpFlags
861+ , regexpQueryBoost :: Maybe Boost
862+ } deriving (Eq, Show)
865+ RangeQuery { rangeQueryField :: FieldName
866+ , rangeQueryRange :: RangeValue
867+ , rangeQueryBoost :: Boost } deriving (Eq, Show)
869+mkRangeQuery :: FieldName -> RangeValue -> RangeQuery
870+mkRangeQuery f r = RangeQuery f r (Boost 1.0)
872+data SimpleQueryStringQuery =
873+ SimpleQueryStringQuery
874+ { simpleQueryStringQuery :: QueryString
875+ , simpleQueryStringField :: Maybe FieldOrFields
876+ , simpleQueryStringOperator :: Maybe BooleanOperator
877+ , simpleQueryStringAnalyzer :: Maybe Analyzer
878+ , simpleQueryStringFlags :: Maybe [SimpleQueryFlag]
879+ , simpleQueryStringLowercaseExpanded :: Maybe LowercaseExpanded
880+ , simpleQueryStringLocale :: Maybe Locale
881+ } deriving (Eq, Show)
883+data SimpleQueryFlag =
890+ | SimpleQueryPrecedence
892+ | SimpleQueryWhitespace
895+ | SimpleQuerySlop deriving (Eq, Show)
897+-- use_dis_max and tie_breaker when fields are plural?
898+data QueryStringQuery =
900+ { queryStringQuery :: QueryString
901+ , queryStringDefaultField :: Maybe FieldName
902+ , queryStringOperator :: Maybe BooleanOperator
903+ , queryStringAnalyzer :: Maybe Analyzer
904+ , queryStringAllowLeadingWildcard :: Maybe AllowLeadingWildcard
905+ , queryStringLowercaseExpanded :: Maybe LowercaseExpanded
906+ , queryStringEnablePositionIncrements :: Maybe EnablePositionIncrements
907+ , queryStringFuzzyMaxExpansions :: Maybe MaxExpansions
908+ , queryStringFuzziness :: Maybe Fuzziness
909+ , queryStringFuzzyPrefixLength :: Maybe PrefixLength
910+ , queryStringPhraseSlop :: Maybe PhraseSlop
911+ , queryStringBoost :: Maybe Boost
912+ , queryStringAnalyzeWildcard :: Maybe AnalyzeWildcard
913+ , queryStringGeneratePhraseQueries :: Maybe GeneratePhraseQueries
914+ , queryStringMinimumShouldMatch :: Maybe MinimumMatch
915+ , queryStringLenient :: Maybe Lenient
916+ , queryStringLocale :: Maybe Locale
917+ } deriving (Eq, Show)
919+mkQueryStringQuery :: QueryString -> QueryStringQuery
920+mkQueryStringQuery qs =
921+ QueryStringQuery qs Nothing Nothing
922+ Nothing Nothing Nothing Nothing
923+ Nothing Nothing Nothing Nothing
924+ Nothing Nothing Nothing Nothing
927+data FieldOrFields = FofField FieldName
928+ | FofFields [FieldName] deriving (Eq, Show)
932+ { prefixQueryField :: FieldName
933+ , prefixQueryPrefixValue :: Text
934+ , prefixQueryBoost :: Maybe Boost } deriving (Eq, Show)
938+ { nestedQueryPath :: QueryPath
939+ , nestedQueryScoreType :: ScoreType
940+ , nestedQuery :: Query } deriving (Eq, Show)
942+data MoreLikeThisFieldQuery =
943+ MoreLikeThisFieldQuery
944+ { moreLikeThisFieldText :: Text
945+ , moreLikeThisFieldFields :: FieldName
946+ -- default 0.3 (30%)
947+ , moreLikeThisFieldPercentMatch :: Maybe PercentMatch
948+ , moreLikeThisFieldMinimumTermFreq :: Maybe MinimumTermFrequency
949+ , moreLikeThisFieldMaxQueryTerms :: Maybe MaxQueryTerms
950+ , moreLikeThisFieldStopWords :: Maybe [StopWord]
951+ , moreLikeThisFieldMinDocFrequency :: Maybe MinDocFrequency
952+ , moreLikeThisFieldMaxDocFrequency :: Maybe MaxDocFrequency
953+ , moreLikeThisFieldMinWordLength :: Maybe MinWordLength
954+ , moreLikeThisFieldMaxWordLength :: Maybe MaxWordLength
955+ , moreLikeThisFieldBoostTerms :: Maybe BoostTerms
956+ , moreLikeThisFieldBoost :: Maybe Boost
957+ , moreLikeThisFieldAnalyzer :: Maybe Analyzer
958+ } deriving (Eq, Show)
960+data MoreLikeThisQuery =
962+ { moreLikeThisText :: Text
963+ , moreLikeThisFields :: Maybe [FieldName]
964+ -- default 0.3 (30%)
965+ , moreLikeThisPercentMatch :: Maybe PercentMatch
966+ , moreLikeThisMinimumTermFreq :: Maybe MinimumTermFrequency
967+ , moreLikeThisMaxQueryTerms :: Maybe MaxQueryTerms
968+ , moreLikeThisStopWords :: Maybe [StopWord]
969+ , moreLikeThisMinDocFrequency :: Maybe MinDocFrequency
970+ , moreLikeThisMaxDocFrequency :: Maybe MaxDocFrequency
971+ , moreLikeThisMinWordLength :: Maybe MinWordLength
972+ , moreLikeThisMaxWordLength :: Maybe MaxWordLength
973+ , moreLikeThisBoostTerms :: Maybe BoostTerms
974+ , moreLikeThisBoost :: Maybe Boost
975+ , moreLikeThisAnalyzer :: Maybe Analyzer
976+ } deriving (Eq, Show)
980+ { indicesQueryIndices :: [IndexName]
981+ , indicesQuery :: Query
983+ , indicesQueryNoMatch :: Maybe Query } deriving (Eq, Show)
985+data HasParentQuery =
987+ { hasParentQueryType :: TypeName
988+ , hasParentQuery :: Query
989+ , hasParentQueryScoreType :: Maybe ScoreType } deriving (Eq, Show)
993+ { hasChildQueryType :: TypeName
994+ , hasChildQuery :: Query
995+ , hasChildQueryScoreType :: Maybe ScoreType } deriving (Eq, Show)
1001+ | ScoreTypeNone deriving (Eq, Show)
1004+ FuzzyQuery { fuzzyQueryField :: FieldName
1005+ , fuzzyQueryValue :: Text
1006+ , fuzzyQueryPrefixLength :: PrefixLength
1007+ , fuzzyQueryMaxExpansions :: MaxExpansions
1008+ , fuzzyQueryFuzziness :: Fuzziness
1009+ , fuzzyQueryBoost :: Maybe Boost
1010+ } deriving (Eq, Show)
1012+data FuzzyLikeFieldQuery =
1013+ FuzzyLikeFieldQuery
1014+ { fuzzyLikeField :: FieldName
1015+ -- anaphora is good for the soul.
1016+ , fuzzyLikeFieldText :: Text
1017+ , fuzzyLikeFieldMaxQueryTerms :: MaxQueryTerms
1018+ , fuzzyLikeFieldIgnoreTermFrequency :: IgnoreTermFrequency
1019+ , fuzzyLikeFieldFuzziness :: Fuzziness
1020+ , fuzzyLikeFieldPrefixLength :: PrefixLength
1021+ , fuzzyLikeFieldBoost :: Boost
1022+ , fuzzyLikeFieldAnalyzer :: Maybe Analyzer
1023+ } deriving (Eq, Show)
1025+data FuzzyLikeThisQuery =
1027+ { fuzzyLikeFields :: [FieldName]
1028+ , fuzzyLikeText :: Text
1029+ , fuzzyLikeMaxQueryTerms :: MaxQueryTerms
1030+ , fuzzyLikeIgnoreTermFrequency :: IgnoreTermFrequency
1031+ , fuzzyLikeFuzziness :: Fuzziness
1032+ , fuzzyLikePrefixLength :: PrefixLength
1033+ , fuzzyLikeBoost :: Boost
1034+ , fuzzyLikeAnalyzer :: Maybe Analyzer
1035+ } deriving (Eq, Show)
1037+data FilteredQuery =
1039+ { filteredQuery :: Query
1040+ , filteredFilter :: Filter } deriving (Eq, Show)
1043+ DisMaxQuery { disMaxQueries :: [Query]
1045+ , disMaxTiebreaker :: Tiebreaker
1046+ , disMaxBoost :: Maybe Boost
1047+ } deriving (Eq, Show)
1050+ MatchQuery { matchQueryField :: FieldName
1051+ , matchQueryQueryString :: QueryString
1052+ , matchQueryOperator :: BooleanOperator
1053+ , matchQueryZeroTerms :: ZeroTermsQuery
1054+ , matchQueryCutoffFrequency :: Maybe CutoffFrequency
1055+ , matchQueryMatchType :: Maybe MatchQueryType
1056+ , matchQueryAnalyzer :: Maybe Analyzer
1057+ , matchQueryMaxExpansions :: Maybe MaxExpansions
1058+ , matchQueryLenient :: Maybe Lenient
1059+ , matchQueryBoost :: Maybe Boost } deriving (Eq, Show)
1061+{-| 'mkMatchQuery' is a convenience function that defaults the less common parameters,
1062+ enabling you to provide only the 'FieldName' and 'QueryString' to make a 'MatchQuery'
1064+mkMatchQuery :: FieldName -> QueryString -> MatchQuery
1065+mkMatchQuery field query = MatchQuery field query Or ZeroTermsNone Nothing Nothing Nothing Nothing Nothing Nothing
1067+data MatchQueryType =
1069+ | MatchPhrasePrefix deriving (Eq, Show)
1071+data MultiMatchQuery =
1072+ MultiMatchQuery { multiMatchQueryFields :: [FieldName]
1073+ , multiMatchQueryString :: QueryString
1074+ , multiMatchQueryOperator :: BooleanOperator
1075+ , multiMatchQueryZeroTerms :: ZeroTermsQuery
1076+ , multiMatchQueryTiebreaker :: Maybe Tiebreaker
1077+ , multiMatchQueryType :: Maybe MultiMatchQueryType
1078+ , multiMatchQueryCutoffFrequency :: Maybe CutoffFrequency
1079+ , multiMatchQueryAnalyzer :: Maybe Analyzer
1080+ , multiMatchQueryMaxExpansions :: Maybe MaxExpansions
1081+ , multiMatchQueryLenient :: Maybe Lenient } deriving (Eq, Show)
1083+{-| 'mkMultiMatchQuery' is a convenience function that defaults the less common parameters,
1084+ enabling you to provide only the list of 'FieldName's and 'QueryString' to
1085+ make a 'MultiMatchQuery'.
1088+mkMultiMatchQuery :: [FieldName] -> QueryString -> MultiMatchQuery
1089+mkMultiMatchQuery matchFields query =
1090+ MultiMatchQuery matchFields query
1091+ Or ZeroTermsNone Nothing Nothing Nothing Nothing Nothing Nothing
1093+data MultiMatchQueryType =
1094+ MultiMatchBestFields
1095+ | MultiMatchMostFields
1096+ | MultiMatchCrossFields
1098+ | MultiMatchPhrasePrefix deriving (Eq, Show)
1101+ BoolQuery { boolQueryMustMatch :: [Query]
1102+ , boolQueryMustNotMatch :: [Query]
1103+ , boolQueryShouldMatch :: [Query]
1104+ , boolQueryMinimumShouldMatch :: Maybe MinimumMatch
1105+ , boolQueryBoost :: Maybe Boost
1106+ , boolQueryDisableCoord :: Maybe DisableCoord
1107+ } deriving (Eq, Show)
1109+mkBoolQuery :: [Query] -> [Query] -> [Query] -> BoolQuery
1110+mkBoolQuery must mustNot should =
1111+ BoolQuery must mustNot should Nothing Nothing Nothing
1113+data BoostingQuery =
1114+ BoostingQuery { positiveQuery :: Query
1115+ , negativeQuery :: Query
1116+ , negativeBoost :: Boost } deriving (Eq, Show)
1118+data CommonTermsQuery =
1119+ CommonTermsQuery { commonField :: FieldName
1120+ , commonQuery :: QueryString
1121+ , commonCutoffFrequency :: CutoffFrequency
1122+ , commonLowFreqOperator :: BooleanOperator
1123+ , commonHighFreqOperator :: BooleanOperator
1124+ , commonMinimumShouldMatch :: Maybe CommonMinimumMatch
1125+ , commonBoost :: Maybe Boost
1126+ , commonAnalyzer :: Maybe Analyzer
1127+ , commonDisableCoord :: Maybe DisableCoord
1128+ } deriving (Eq, Show)
1130+data CommonMinimumMatch =
1131+ CommonMinimumMatchHighLow MinimumMatchHighLow
1132+ | CommonMinimumMatch MinimumMatch
1133+ deriving (Eq, Show)
1135+data MinimumMatchHighLow =
1136+ MinimumMatchHighLow { lowFreq :: MinimumMatch
1137+ , highFreq :: MinimumMatch } deriving (Eq, Show)
1139+data Filter = AndFilter [Filter] Cache
1140+ | OrFilter [Filter] Cache
1141+ | NotFilter Filter Cache
1143+ | BoolFilter BoolMatch
1144+ | ExistsFilter FieldName -- always cached
1145+ | GeoBoundingBoxFilter GeoBoundingBoxConstraint
1146+ | GeoDistanceFilter GeoPoint Distance DistanceType OptimizeBbox Cache
1147+ | GeoDistanceRangeFilter GeoPoint DistanceRange
1148+ | GeoPolygonFilter FieldName [LatLon]
1149+ | IdsFilter MappingName [DocId]
1151+ | MissingFilter FieldName Existence NullValue
1152+ | PrefixFilter FieldName PrefixValue Cache
1153+ | QueryFilter Query Cache
1154+ | RangeFilter FieldName RangeValue RangeExecution Cache
1155+ | RegexpFilter FieldName Regexp RegexpFlags CacheName Cache CacheKey
1156+ | TermFilter Term Cache
1157+ deriving (Eq, Show)
1159+data ZeroTermsQuery = ZeroTermsNone
1160+ | ZeroTermsAll deriving (Eq, Show)
1162+data RangeExecution = RangeExecutionIndex
1163+ | RangeExecutionFielddata deriving (Eq, Show)
1165+newtype Regexp = Regexp Text deriving (Eq, Show)
1167+data RegexpFlags = AllRegexpFlags
1169+ | SomeRegexpFlags (NonEmpty RegexpFlag) deriving (Eq, Show)
1171+data RegexpFlag = AnyString
1176+ | Interval deriving (Eq, Show)
1178+newtype LessThan = LessThan Double deriving (Eq, Show)
1179+newtype LessThanEq = LessThanEq Double deriving (Eq, Show)
1180+newtype GreaterThan = GreaterThan Double deriving (Eq, Show)
1181+newtype GreaterThanEq = GreaterThanEq Double deriving (Eq, Show)
1183+newtype LessThanD = LessThanD UTCTime deriving (Eq, Show)
1184+newtype LessThanEqD = LessThanEqD UTCTime deriving (Eq, Show)
1185+newtype GreaterThanD = GreaterThanD UTCTime deriving (Eq, Show)
1186+newtype GreaterThanEqD = GreaterThanEqD UTCTime deriving (Eq, Show)
1188+data RangeValue = RangeDateLte LessThanEqD
1189+ | RangeDateLt LessThanD
1190+ | RangeDateGte GreaterThanEqD
1191+ | RangeDateGt GreaterThanD
1192+ | RangeDateGtLt GreaterThanD LessThanD
1193+ | RangeDateGteLte GreaterThanEqD LessThanEqD
1194+ | RangeDateGteLt GreaterThanEqD LessThanD
1195+ | RangeDateGtLte GreaterThanD LessThanEqD
1196+ | RangeDoubleLte LessThanEq
1197+ | RangeDoubleLt LessThan
1198+ | RangeDoubleGte GreaterThanEq
1199+ | RangeDoubleGt GreaterThan
1200+ | RangeDoubleGtLt GreaterThan LessThan
1201+ | RangeDoubleGteLte GreaterThanEq LessThanEq
1202+ | RangeDoubleGteLt GreaterThanEq LessThan
1203+ | RangeDoubleGtLte GreaterThan LessThanEq
1204+ deriving (Eq, Show)
1206+rangeValueToPair :: RangeValue -> [Pair]
1207+rangeValueToPair rv = case rv of
1208+ RangeDateLte (LessThanEqD t) -> ["lte" .= t]
1209+ RangeDateGte (GreaterThanEqD t) -> ["gte" .= t]
1210+ RangeDateLt (LessThanD t) -> ["lt" .= t]
1211+ RangeDateGt (GreaterThanD t) -> ["gt" .= t]
1212+ RangeDateGteLte (GreaterThanEqD l) (LessThanEqD g) -> ["gte" .= l, "lte" .= g]
1213+ RangeDateGtLte (GreaterThanD l) (LessThanEqD g) -> ["gt" .= l, "lte" .= g]
1214+ RangeDateGteLt (GreaterThanEqD l) (LessThanD g) -> ["gte" .= l, "lt" .= g]
1215+ RangeDateGtLt (GreaterThanD l) (LessThanD g) -> ["gt" .= l, "lt" .= g]
1216+ RangeDoubleLte (LessThanEq t) -> ["lte" .= t]
1217+ RangeDoubleGte (GreaterThanEq t) -> ["gte" .= t]
1218+ RangeDoubleLt (LessThan t) -> ["lt" .= t]
1219+ RangeDoubleGt (GreaterThan t) -> ["gt" .= t]
1220+ RangeDoubleGteLte (GreaterThanEq l) (LessThanEq g) -> ["gte" .= l, "lte" .= g]
1221+ RangeDoubleGtLte (GreaterThan l) (LessThanEq g) -> ["gt" .= l, "lte" .= g]
1222+ RangeDoubleGteLt (GreaterThanEq l) (LessThan g) -> ["gte" .= l, "lt" .= g]
1223+ RangeDoubleGtLt (GreaterThan l) (LessThan g) -> ["gt" .= l, "lt" .= g]
1225+data Term = Term { termField :: Text
1226+ , termValue :: Text } deriving (Eq, Show)
1228+data BoolMatch = MustMatch Term Cache
1229+ | MustNotMatch Term Cache
1230+ | ShouldMatch [Term] Cache deriving (Eq, Show)
1232+-- "memory" or "indexed"
1233+data GeoFilterType = GeoFilterMemory
1234+ | GeoFilterIndexed deriving (Eq, Show)
1236+data LatLon = LatLon { lat :: Double
1237+ , lon :: Double } deriving (Eq, Show)
1239+data GeoBoundingBox =
1240+ GeoBoundingBox { topLeft :: LatLon
1241+ , bottomRight :: LatLon } deriving (Eq, Show)
1243+data GeoBoundingBoxConstraint =
1244+ GeoBoundingBoxConstraint { geoBBField :: FieldName
1245+ , constraintBox :: GeoBoundingBox
1246+ , bbConstraintcache :: Cache
1247+ , geoType :: GeoFilterType
1248+ } deriving (Eq, Show)
1251+ GeoPoint { geoField :: FieldName
1252+ , latLon :: LatLon} deriving (Eq, Show)
1254+data DistanceUnit = Miles
1262+ | NauticalMiles deriving (Eq, Show)
1264+data DistanceType = Arc
1265+ | SloppyArc -- doesn't exist <1.0
1266+ | Plane deriving (Eq, Show)
1268+data OptimizeBbox = OptimizeGeoFilterType GeoFilterType
1269+ | NoOptimizeBbox deriving (Eq, Show)
1272+ Distance { coefficient :: Double
1273+ , unit :: DistanceUnit } deriving (Eq, Show)
1275+data DistanceRange =
1276+ DistanceRange { distanceFrom :: Distance
1277+ , distanceTo :: Distance } deriving (Eq, Show)
1279+data SearchResult a =
1280+ SearchResult { took :: Int
1282+ , shards :: ShardResult
1283+ , searchHits :: SearchHits a
1284+ , aggregations :: Maybe AggregationResults
1285+ , scrollId :: Maybe ScrollId } deriving (Eq, Show)
1287+type ScrollId = Text -- Fixme: Newtype
1289+type Score = Maybe Double
1292+ SearchHits { hitsTotal :: Int
1293+ , maxScore :: Score
1294+ , hits :: [Hit a] } deriving (Eq, Show)
1297+instance Monoid (SearchHits a) where
1298+ mempty = SearchHits 0 Nothing mempty
1299+ mappend (SearchHits ta ma ha) (SearchHits tb mb hb) =
1300+ SearchHits (ta + tb) (max ma mb) (ha <> hb)
1304+ Hit { hitIndex :: IndexName
1305+ , hitType :: MappingName
1306+ , hitDocId :: DocId
1307+ , hitScore :: Score
1309+ , hitHighlight :: Maybe HitHighlight } deriving (Eq, Show)
1312+ ShardResult { shardTotal :: Int
1313+ , shardsSuccessful :: Int
1314+ , shardsFailed :: Int } deriving (Eq, Show, Generic)
1316+type HitHighlight = M.Map Text [Text]
1318+showText :: Show a => a -> Text
1319+showText = T.pack . show
1321+type Aggregations = M.Map Text Aggregation
1323+emptyAggregations :: Aggregations
1324+emptyAggregations = M.empty
1326+mkAggregations :: Text -> Aggregation -> Aggregations
1327+mkAggregations name aggregation = M.insert name aggregation emptyAggregations
1329+data TermOrder = TermOrder{ termSortField :: Text
1330+ , termSortOrder :: SortOrder } deriving (Eq, Show)
1332+data TermInclusion = TermInclusion Text
1333+ | TermPattern Text Text deriving (Eq, Show)
1335+data CollectionMode = BreadthFirst
1336+ | DepthFirst deriving (Eq, Show)
1338+data ExecutionHint = Ordinals
1340+ | GlobalOrdinalsHash
1341+ | GlobalOrdinalsLowCardinality
1342+ | Map deriving (Eq, Show)
1344+data TimeInterval = Weeks
1348+ | Seconds deriving (Eq)
1350+data Interval = Year
1358+ | FractionalInterval Float TimeInterval deriving (Eq, Show)
1360+data Aggregation = TermsAgg TermsAggregation
1361+ | DateHistogramAgg DateHistogramAggregation
1362+ | ValueCountAgg ValueCountAggregation
1363+ | FilterAgg FilterAggregation deriving (Eq, Show)
1366+data TermsAggregation = TermsAggregation { term :: Either Text Text
1367+ , termInclude :: Maybe TermInclusion
1368+ , termExclude :: Maybe TermInclusion
1369+ , termOrder :: Maybe TermOrder
1370+ , termMinDocCount :: Maybe Int
1371+ , termSize :: Maybe Int
1372+ , termShardSize :: Maybe Int
1373+ , termCollectMode :: Maybe CollectionMode
1374+ , termExecutionHint :: Maybe ExecutionHint
1375+ , termAggs :: Maybe Aggregations
1376+ } deriving (Eq, Show)
1378+data DateHistogramAggregation = DateHistogramAggregation { dateField :: FieldName
1379+ , dateInterval :: Interval
1380+ , dateFormat :: Maybe Text
1381+ -- pre and post deprecated in 1.5
1382+ , datePreZone :: Maybe Text
1383+ , datePostZone :: Maybe Text
1384+ , datePreOffset :: Maybe Text
1385+ , datePostOffset :: Maybe Text
1386+ , dateAggs :: Maybe Aggregations
1387+ } deriving (Eq, Show)
1389+-- | See <https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-valuecount-aggregation.html> for more information.
1390+data ValueCountAggregation = FieldValueCount FieldName
1391+ | ScriptValueCount Script deriving (Eq, Show)
1393+-- | 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.
1394+data FilterAggregation = FilterAggregation { faFilter :: Filter
1395+ , faAggs :: Maybe Aggregations} deriving (Eq, Show)
1397+mkTermsAggregation :: Text -> TermsAggregation
1398+mkTermsAggregation t = TermsAggregation (Left t) Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing
1400+mkTermsScriptAggregation :: Text -> TermsAggregation
1401+mkTermsScriptAggregation t = TermsAggregation (Right t) Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing
1403+mkDateHistogram :: FieldName -> Interval -> DateHistogramAggregation
1404+mkDateHistogram t i = DateHistogramAggregation t i Nothing Nothing Nothing Nothing Nothing Nothing
1406+instance ToJSON TermOrder where
1407+ toJSON (TermOrder termSortField termSortOrder) = object [termSortField .= termSortOrder]
1409+instance ToJSON TermInclusion where
1410+ toJSON (TermInclusion x) = toJSON x
1411+ toJSON (TermPattern pattern flags) = omitNulls [ "pattern" .= pattern,
1414+instance ToJSON CollectionMode where
1415+ toJSON BreadthFirst = "breadth_first"
1416+ toJSON DepthFirst = "depth_first"
1418+instance ToJSON ExecutionHint where
1419+ toJSON Ordinals = "ordinals"
1420+ toJSON GlobalOrdinals = "global_ordinals"
1421+ toJSON GlobalOrdinalsHash = "global_ordinals_hash"
1422+ toJSON GlobalOrdinalsLowCardinality = "global_ordinals_low_cardinality"
1425+instance ToJSON Interval where
1426+ toJSON Year = "year"
1427+ toJSON Quarter = "quarter"
1428+ toJSON Month = "month"
1429+ toJSON Week = "week"
1431+ toJSON Hour = "hour"
1432+ toJSON Minute = "minute"
1433+ toJSON Second = "second"
1434+ toJSON (FractionalInterval fraction interval) = toJSON $ show fraction ++ show interval
1436+instance Show TimeInterval where
1443+instance ToJSON Aggregation where
1444+ toJSON (TermsAgg (TermsAggregation term include exclude order minDocCount size shardSize collectMode executionHint termAggs)) =
1445+ omitNulls ["terms" .= omitNulls [ toJSON' term,
1446+ "include" .= include,
1447+ "exclude" .= exclude,
1449+ "min_doc_count" .= minDocCount,
1451+ "shard_size" .= shardSize,
1452+ "collect_mode" .= collectMode,
1453+ "execution_hint" .= executionHint
1455+ "aggs" .= termAggs ]
1457+ toJSON' x = case x of { Left y -> "field" .= y; Right y -> "script" .= y }
1459+ toJSON (DateHistogramAgg (DateHistogramAggregation field interval format preZone postZone preOffset postOffset dateHistoAggs)) =
1460+ omitNulls ["date_histogram" .= omitNulls [ "field" .= field,
1461+ "interval" .= interval,
1462+ "format" .= format,
1463+ "pre_zone" .= preZone,
1464+ "post_zone" .= postZone,
1465+ "pre_offset" .= preOffset,
1466+ "post_offset" .= postOffset
1468+ "aggs" .= dateHistoAggs ]
1469+ toJSON (ValueCountAgg a) = object ["value_count" .= v]
1470+ where v = case a of
1471+ (FieldValueCount (FieldName n)) -> object ["field" .= n]
1472+ (ScriptValueCount (Script s)) -> object ["script" .= s]
1473+ toJSON (FilterAgg (FilterAggregation filt ags)) =
1474+ omitNulls [ "filter" .= filt
1477+type AggregationResults = M.Map Text Value
1479+class BucketAggregation a where
1481+ docCount :: a -> Int
1482+ aggs :: a -> Maybe AggregationResults
1485+data Bucket a = Bucket { buckets :: [a]} deriving (Show)
1487+data TermsResult = TermsResult { termKey :: Text
1488+ , termsDocCount :: Int
1489+ , termsAggs :: Maybe AggregationResults } deriving (Show)
1491+data DateHistogramResult = DateHistogramResult { dateKey :: Int
1492+ , dateKeyStr :: Maybe Text
1493+ , dateDocCount :: Int
1494+ , dateHistogramAggs :: Maybe AggregationResults } deriving (Show)
1496+toTerms :: Text -> AggregationResults -> Maybe (Bucket TermsResult)
1497+toTerms t a = M.lookup t a >>= deserialize
1498+ where deserialize = parseMaybe parseJSON
1500+toDateHistogram :: Text -> AggregationResults -> Maybe (Bucket DateHistogramResult)
1501+toDateHistogram t a = M.lookup t a >>= deserialize
1502+ where deserialize = parseMaybe parseJSON
1504+instance BucketAggregation TermsResult where
1506+ docCount = termsDocCount
1509+instance BucketAggregation DateHistogramResult where
1510+ key = showText . dateKey
1511+ docCount = dateDocCount
1512+ aggs = dateHistogramAggs
1514+instance (FromJSON a, BucketAggregation a) => FromJSON (Bucket a) where
1515+ parseJSON (Object v) = Bucket <$>
1517+ parseJSON _ = mempty
1519+instance FromJSON TermsResult where
1520+ parseJSON (Object v) = TermsResult <$>
1522+ v .: "doc_count" <*>
1523+ v .:? "aggregations"
1524+ parseJSON _ = mempty
1526+instance FromJSON DateHistogramResult where
1527+ parseJSON (Object v) = DateHistogramResult <$>
1529+ v .:? "key_as_string" <*>
1530+ v .: "doc_count" <*>
1531+ v .:? "aggregations"
1532+ parseJSON _ = mempty
1534+instance Monoid Filter where
1535+ mempty = IdentityFilter
1536+ mappend a b = AndFilter [a, b] defaultCache
1538+instance Seminearring Filter where
1539+ a <||> b = OrFilter [a, b] defaultCache
1541+instance ToJSON Filter where
1542+ toJSON (AndFilter filters cache) =
1544+ object [ "filters" .= fmap toJSON filters
1545+ , "_cache" .= cache]]
1547+ toJSON (OrFilter filters cache) =
1549+ object [ "filters" .= fmap toJSON filters
1550+ , "_cache" .= cache]]
1552+ toJSON (NotFilter notFilter cache) =
1554+ object ["filter" .= notFilter
1555+ , "_cache" .= cache]]
1557+ toJSON (IdentityFilter) =
1558+ object ["match_all" .= object []]
1560+ toJSON (TermFilter (Term termFilterField termFilterValue) cache) =
1561+ object ["term" .= object base]
1562+ where base = [termFilterField .= termFilterValue,
1565+ toJSON (ExistsFilter (FieldName fieldName)) =
1566+ object ["exists" .= object
1567+ ["field" .= fieldName]]
1569+ toJSON (BoolFilter boolMatch) =
1570+ object ["bool" .= boolMatch]
1572+ toJSON (GeoBoundingBoxFilter bbConstraint) =
1573+ object ["geo_bounding_box" .= bbConstraint]
1575+ toJSON (GeoDistanceFilter (GeoPoint (FieldName distanceGeoField) geoDistLatLon)
1576+ distance distanceType optimizeBbox cache) =
1577+ object ["geo_distance" .=
1578+ object ["distance" .= distance
1579+ , "distance_type" .= distanceType
1580+ , "optimize_bbox" .= optimizeBbox
1581+ , distanceGeoField .= geoDistLatLon
1582+ , "_cache" .= cache]]
1584+ toJSON (GeoDistanceRangeFilter (GeoPoint (FieldName gddrField) drLatLon)
1585+ (DistanceRange geoDistRangeDistFrom drDistanceTo)) =
1586+ object ["geo_distance_range" .=
1587+ object ["from" .= geoDistRangeDistFrom
1588+ , "to" .= drDistanceTo
1589+ , gddrField .= drLatLon]]
1591+ toJSON (GeoPolygonFilter (FieldName geoPolygonFilterField) latLons) =
1592+ object ["geo_polygon" .=
1593+ object [geoPolygonFilterField .=
1594+ object ["points" .= fmap toJSON latLons]]]
1596+ toJSON (IdsFilter (MappingName mappingName) values) =
1598+ object ["type" .= mappingName
1599+ , "values" .= fmap unpackId values]]
1601+ toJSON (LimitFilter limit) =
1602+ object ["limit" .= object ["value" .= limit]]
1604+ toJSON (MissingFilter (FieldName fieldName) (Existence existence) (NullValue nullValue)) =
1605+ object ["missing" .=
1606+ object ["field" .= fieldName
1607+ , "existence" .= existence
1608+ , "null_value" .= nullValue]]
1610+ toJSON (PrefixFilter (FieldName fieldName) fieldValue cache) =
1611+ object ["prefix" .=
1612+ object [fieldName .= fieldValue
1613+ , "_cache" .= cache]]
1615+ toJSON (QueryFilter query False) =
1616+ object ["query" .= toJSON query ]
1617+ toJSON (QueryFilter query True) =
1618+ object ["fquery" .=
1619+ object [ "query" .= toJSON query
1620+ , "_cache" .= True ]]
1622+ toJSON (RangeFilter (FieldName fieldName) rangeValue rangeExecution cache) =
1624+ object [ fieldName .= object (rangeValueToPair rangeValue)
1625+ , "execution" .= rangeExecution
1626+ , "_cache" .= cache]]
1628+ toJSON (RegexpFilter (FieldName fieldName)
1629+ (Regexp regexText) flags (CacheName cacheName) cache (CacheKey cacheKey)) =
1630+ object ["regexp" .=
1631+ object [fieldName .=
1632+ object ["value" .= regexText
1633+ , "flags" .= flags]
1634+ , "_name" .= cacheName
1635+ , "_cache" .= cache
1636+ , "_cache_key" .= cacheKey]]
1638+instance ToJSON GeoPoint where
1639+ toJSON (GeoPoint (FieldName geoPointField) geoPointLatLon) =
1640+ object [ geoPointField .= geoPointLatLon ]
1643+instance ToJSON Query where
1644+ toJSON (TermQuery (Term termQueryField termQueryValue) boost) =
1646+ object [termQueryField .= object merged]]
1648+ base = [ "value" .= termQueryValue ]
1649+ boosted = maybe [] (return . ("boost" .=)) boost
1650+ merged = mappend base boosted
1652+ toJSON (TermsQuery terms) =
1653+ object [ "terms" .= object conjoined ]
1654+ where conjoined = [ getTermsField terms .=
1655+ fmap (toJSON . getTermValue) (toList terms)]
1656+ getTermsField ((Term f _ ) :| _) = f
1657+ getTermValue (Term _ v) = v
1658+ toJSON (IdsQuery idsQueryMappingName docIds) =
1659+ object [ "ids" .= object conjoined ]
1660+ where conjoined = [ "type" .= idsQueryMappingName
1661+ , "values" .= fmap toJSON docIds ]
1663+ toJSON (QueryQueryStringQuery qQueryStringQuery) =
1664+ object [ "query_string" .= qQueryStringQuery ]
1666+ toJSON (QueryMatchQuery matchQuery) =
1667+ object [ "match" .= matchQuery ]
1669+ toJSON (QueryMultiMatchQuery multiMatchQuery) =
1670+ toJSON multiMatchQuery
1672+ toJSON (QueryBoolQuery boolQuery) =
1673+ object [ "bool" .= boolQuery ]
1675+ toJSON (QueryBoostingQuery boostingQuery) =
1676+ object [ "boosting" .= boostingQuery ]
1678+ toJSON (QueryCommonTermsQuery commonTermsQuery) =
1679+ object [ "common" .= commonTermsQuery ]
1681+ toJSON (ConstantScoreFilter csFilter boost) =
1682+ object [ "constant_score" .= csFilter
1683+ , "boost" .= boost]
1685+ toJSON (ConstantScoreQuery query boost) =
1686+ object [ "constant_score" .= query
1687+ , "boost" .= boost]
1689+ toJSON (QueryDisMaxQuery disMaxQuery) =
1690+ object [ "dis_max" .= disMaxQuery ]
1692+ toJSON (QueryFilteredQuery qFilteredQuery) =
1693+ object [ "filtered" .= qFilteredQuery ]
1695+ toJSON (QueryFuzzyLikeThisQuery fuzzyQuery) =
1696+ object [ "fuzzy_like_this" .= fuzzyQuery ]
1698+ toJSON (QueryFuzzyLikeFieldQuery fuzzyFieldQuery) =
1699+ object [ "fuzzy_like_this_field" .= fuzzyFieldQuery ]
1701+ toJSON (QueryFuzzyQuery fuzzyQuery) =
1702+ object [ "fuzzy" .= fuzzyQuery ]
1704+ toJSON (QueryHasChildQuery childQuery) =
1705+ object [ "has_child" .= childQuery ]
1707+ toJSON (QueryHasParentQuery parentQuery) =
1708+ object [ "has_parent" .= parentQuery ]
1710+ toJSON (QueryIndicesQuery qIndicesQuery) =
1711+ object [ "indices" .= qIndicesQuery ]
1713+ toJSON (MatchAllQuery boost) =
1714+ object [ "match_all" .= omitNulls [ "boost" .= boost ] ]
1716+ toJSON (QueryMoreLikeThisQuery query) =
1717+ object [ "more_like_this" .= query ]
1719+ toJSON (QueryMoreLikeThisFieldQuery query) =
1720+ object [ "more_like_this_field" .= query ]
1722+ toJSON (QueryNestedQuery query) =
1723+ object [ "nested" .= query ]
1725+ toJSON (QueryPrefixQuery query) =
1726+ object [ "prefix" .= query ]
1728+ toJSON (QueryRangeQuery query) =
1729+ object [ "range" .= query ]
1731+ toJSON (QueryRegexpQuery query) =
1732+ object [ "regexp" .= query ]
1734+ toJSON (QuerySimpleQueryStringQuery query) =
1735+ object [ "simple_query_string" .= query ]
1738+omitNulls :: [(Text, Value)] -> Value
1739+omitNulls = object . filter notNull where
1740+ notNull (_, Null) = False
1741+ notNull (_, Array a) = (not . V.null) a
1745+instance ToJSON SimpleQueryStringQuery where
1746+ toJSON SimpleQueryStringQuery {..} =
1747+ omitNulls (base ++ maybeAdd)
1748+ where base = [ "query" .= simpleQueryStringQuery ]
1749+ maybeAdd = [ "fields" .= simpleQueryStringField
1750+ , "default_operator" .= simpleQueryStringOperator
1751+ , "analyzer" .= simpleQueryStringAnalyzer
1752+ , "flags" .= simpleQueryStringFlags
1753+ , "lowercase_expanded_terms" .= simpleQueryStringLowercaseExpanded
1754+ , "locale" .= simpleQueryStringLocale ]
1757+instance ToJSON FieldOrFields where
1758+ toJSON (FofField fieldName) =
1760+ toJSON (FofFields fieldNames) =
1763+instance ToJSON SimpleQueryFlag where
1764+ toJSON SimpleQueryAll = "ALL"
1765+ toJSON SimpleQueryNone = "NONE"
1766+ toJSON SimpleQueryAnd = "AND"
1767+ toJSON SimpleQueryOr = "OR"
1768+ toJSON SimpleQueryPrefix = "PREFIX"
1769+ toJSON SimpleQueryPhrase = "PHRASE"
1770+ toJSON SimpleQueryPrecedence = "PRECEDENCE"
1771+ toJSON SimpleQueryEscape = "ESCAPE"
1772+ toJSON SimpleQueryWhitespace = "WHITESPACE"
1773+ toJSON SimpleQueryFuzzy = "FUZZY"
1774+ toJSON SimpleQueryNear = "NEAR"
1775+ toJSON SimpleQuerySlop = "SLOP"
1778+instance ToJSON RegexpQuery where
1779+ toJSON (RegexpQuery (FieldName rqQueryField)
1780+ (Regexp regexpQueryQuery) rqQueryFlags
1782+ object [ rqQueryField .= omitNulls base ]
1783+ where base = [ "value" .= regexpQueryQuery
1784+ , "flags" .= rqQueryFlags
1785+ , "boost" .= rqQueryBoost ]
1788+instance ToJSON QueryStringQuery where
1789+ toJSON (QueryStringQuery qsQueryString
1790+ qsDefaultField qsOperator
1791+ qsAnalyzer qsAllowWildcard
1792+ qsLowercaseExpanded qsEnablePositionIncrements
1793+ qsFuzzyMaxExpansions qsFuzziness
1794+ qsFuzzyPrefixLength qsPhraseSlop
1795+ qsBoost qsAnalyzeWildcard
1796+ qsGeneratePhraseQueries qsMinimumShouldMatch
1797+ qsLenient qsLocale) =
1800+ base = [ "query" .= qsQueryString
1801+ , "default_field" .= qsDefaultField
1802+ , "default_operator" .= qsOperator
1803+ , "analyzer" .= qsAnalyzer
1804+ , "allow_leading_wildcard" .= qsAllowWildcard
1805+ , "lowercase_expanded_terms" .= qsLowercaseExpanded
1806+ , "enable_position_increments" .= qsEnablePositionIncrements
1807+ , "fuzzy_max_expansions" .= qsFuzzyMaxExpansions
1808+ , "fuzziness" .= qsFuzziness
1809+ , "fuzzy_prefix_length" .= qsFuzzyPrefixLength
1810+ , "phrase_slop" .= qsPhraseSlop
1811+ , "boost" .= qsBoost
1812+ , "analyze_wildcard" .= qsAnalyzeWildcard
1813+ , "auto_generate_phrase_queries" .= qsGeneratePhraseQueries
1814+ , "minimum_should_match" .= qsMinimumShouldMatch
1815+ , "lenient" .= qsLenient
1816+ , "locale" .= qsLocale ]
1819+instance ToJSON RangeQuery where
1820+ toJSON (RangeQuery (FieldName fieldName) range boost) =
1821+ object [ fieldName .= conjoined ]
1822+ where conjoined = [ "boost" .= boost ] ++ (rangeValueToPair range)
1825+instance ToJSON PrefixQuery where
1826+ toJSON (PrefixQuery (FieldName fieldName) queryValue boost) =
1827+ object [ fieldName .= omitNulls base ]
1828+ where base = [ "value" .= queryValue
1829+ , "boost" .= boost ]
1832+instance ToJSON NestedQuery where
1833+ toJSON (NestedQuery nqPath nqScoreType nqQuery) =
1834+ object [ "path" .= nqPath
1835+ , "score_mode" .= nqScoreType
1836+ , "query" .= nqQuery ]
1839+instance ToJSON MoreLikeThisFieldQuery where
1840+ toJSON (MoreLikeThisFieldQuery text (FieldName fieldName)
1841+ percent mtf mqt stopwords mindf maxdf
1842+ minwl maxwl boostTerms boost analyzer) =
1843+ object [ fieldName .= omitNulls base ]
1844+ where base = [ "like_text" .= text
1845+ , "percent_terms_to_match" .= percent
1846+ , "min_term_freq" .= mtf
1847+ , "max_query_terms" .= mqt
1848+ , "stop_words" .= stopwords
1849+ , "min_doc_freq" .= mindf
1850+ , "max_doc_freq" .= maxdf
1851+ , "min_word_length" .= minwl
1852+ , "max_word_length" .= maxwl
1853+ , "boost_terms" .= boostTerms
1855+ , "analyzer" .= analyzer ]
1858+instance ToJSON MoreLikeThisQuery where
1859+ toJSON (MoreLikeThisQuery text fields percent
1860+ mtf mqt stopwords mindf maxdf
1861+ minwl maxwl boostTerms boost analyzer) =
1863+ where base = [ "like_text" .= text
1864+ , "fields" .= fields
1865+ , "percent_terms_to_match" .= percent
1866+ , "min_term_freq" .= mtf
1867+ , "max_query_terms" .= mqt
1868+ , "stop_words" .= stopwords
1869+ , "min_doc_freq" .= mindf
1870+ , "max_doc_freq" .= maxdf
1871+ , "min_word_length" .= minwl
1872+ , "max_word_length" .= maxwl
1873+ , "boost_terms" .= boostTerms
1875+ , "analyzer" .= analyzer ]
1878+instance ToJSON IndicesQuery where
1879+ toJSON (IndicesQuery indices query noMatch) =
1880+ omitNulls [ "indices" .= indices
1881+ , "no_match_query" .= noMatch
1882+ , "query" .= query ]
1885+instance ToJSON HasParentQuery where
1886+ toJSON (HasParentQuery queryType query scoreType) =
1887+ omitNulls [ "parent_type" .= queryType
1888+ , "score_type" .= scoreType
1889+ , "query" .= query ]
1892+instance ToJSON HasChildQuery where
1893+ toJSON (HasChildQuery queryType query scoreType) =
1894+ omitNulls [ "query" .= query
1895+ , "score_type" .= scoreType
1896+ , "type" .= queryType ]
1899+instance ToJSON FuzzyQuery where
1900+ toJSON (FuzzyQuery (FieldName fieldName) queryText
1901+ prefixLength maxEx fuzziness boost) =
1902+ object [ fieldName .= omitNulls base ]
1903+ where base = [ "value" .= queryText
1904+ , "fuzziness" .= fuzziness
1905+ , "prefix_length" .= prefixLength
1907+ , "max_expansions" .= maxEx ]
1910+instance ToJSON FuzzyLikeFieldQuery where
1911+ toJSON (FuzzyLikeFieldQuery (FieldName fieldName)
1912+ fieldText maxTerms ignoreFreq fuzziness prefixLength
1914+ object [ fieldName .=
1915+ omitNulls [ "like_text" .= fieldText
1916+ , "max_query_terms" .= maxTerms
1917+ , "ignore_tf" .= ignoreFreq
1918+ , "fuzziness" .= fuzziness
1919+ , "prefix_length" .= prefixLength
1920+ , "analyzer" .= analyzer
1921+ , "boost" .= boost ]]
1924+instance ToJSON FuzzyLikeThisQuery where
1925+ toJSON (FuzzyLikeThisQuery fields text maxTerms
1926+ ignoreFreq fuzziness prefixLength boost analyzer) =
1928+ where base = [ "fields" .= fields
1929+ , "like_text" .= text
1930+ , "max_query_terms" .= maxTerms
1931+ , "ignore_tf" .= ignoreFreq
1932+ , "fuzziness" .= fuzziness
1933+ , "prefix_length" .= prefixLength
1934+ , "analyzer" .= analyzer
1935+ , "boost" .= boost ]
1938+instance ToJSON FilteredQuery where
1939+ toJSON (FilteredQuery query fFilter) =
1940+ object [ "query" .= query
1941+ , "filter" .= fFilter ]
1944+instance ToJSON DisMaxQuery where
1945+ toJSON (DisMaxQuery queries tiebreaker boost) =
1947+ where base = [ "queries" .= queries
1949+ , "tie_breaker" .= tiebreaker ]
1952+instance ToJSON CommonTermsQuery where
1953+ toJSON (CommonTermsQuery (FieldName fieldName)
1954+ (QueryString query) cf lfo hfo msm
1955+ boost analyzer disableCoord) =
1956+ object [fieldName .= omitNulls base ]
1957+ where base = [ "query" .= query
1958+ , "cutoff_frequency" .= cf
1959+ , "low_freq_operator" .= lfo
1960+ , "minimum_should_match" .= msm
1962+ , "analyzer" .= analyzer
1963+ , "disable_coord" .= disableCoord
1964+ , "high_freq_operator" .= hfo ]
1967+instance ToJSON CommonMinimumMatch where
1968+ toJSON (CommonMinimumMatch mm) = toJSON mm
1969+ toJSON (CommonMinimumMatchHighLow (MinimumMatchHighLow lowF highF)) =
1970+ object [ "low_freq" .= lowF
1971+ , "high_freq" .= highF ]
1973+instance ToJSON BoostingQuery where
1974+ toJSON (BoostingQuery bqPositiveQuery bqNegativeQuery bqNegativeBoost) =
1975+ object [ "positive" .= bqPositiveQuery
1976+ , "negative" .= bqNegativeQuery
1977+ , "negative_boost" .= bqNegativeBoost ]
1980+instance ToJSON BoolQuery where
1981+ toJSON (BoolQuery mustM notM shouldM bqMin boost disableCoord) =
1983+ where base = [ "must" .= mustM
1984+ , "must_not" .= notM
1985+ , "should" .= shouldM
1986+ , "minimum_should_match" .= bqMin
1988+ , "disable_coord" .= disableCoord ]
1991+instance ToJSON MatchQuery where
1992+ toJSON (MatchQuery (FieldName fieldName)
1993+ (QueryString mqQueryString) booleanOperator
1994+ zeroTermsQuery cutoffFrequency matchQueryType
1995+ analyzer maxExpansions lenient boost) =
1996+ object [ fieldName .= omitNulls base ]
1997+ where base = [ "query" .= mqQueryString
1998+ , "operator" .= booleanOperator
1999+ , "zero_terms_query" .= zeroTermsQuery
2000+ , "cutoff_frequency" .= cutoffFrequency
2001+ , "type" .= matchQueryType
2002+ , "analyzer" .= analyzer
2003+ , "max_expansions" .= maxExpansions
2004+ , "lenient" .= lenient
2005+ , "boost" .= boost ]
2008+instance ToJSON MultiMatchQuery where
2009+ toJSON (MultiMatchQuery fields (QueryString query) boolOp
2010+ ztQ tb mmqt cf analyzer maxEx lenient) =
2011+ object ["multi_match" .= omitNulls base]
2012+ where base = [ "fields" .= fmap toJSON fields
2014+ , "operator" .= boolOp
2015+ , "zero_terms_query" .= ztQ
2016+ , "tiebreaker" .= tb
2018+ , "cutoff_frequency" .= cf
2019+ , "analyzer" .= analyzer
2020+ , "max_expansions" .= maxEx
2021+ , "lenient" .= lenient ]
2024+instance ToJSON MultiMatchQueryType where
2025+ toJSON MultiMatchBestFields = "best_fields"
2026+ toJSON MultiMatchMostFields = "most_fields"
2027+ toJSON MultiMatchCrossFields = "cross_fields"
2028+ toJSON MultiMatchPhrase = "phrase"
2029+ toJSON MultiMatchPhrasePrefix = "phrase_prefix"
2031+instance ToJSON BooleanOperator where
2032+ toJSON And = String "and"
2033+ toJSON Or = String "or"
2035+instance ToJSON ZeroTermsQuery where
2036+ toJSON ZeroTermsNone = String "none"
2037+ toJSON ZeroTermsAll = String "all"
2039+instance ToJSON MatchQueryType where
2040+ toJSON MatchPhrase = "phrase"
2041+ toJSON MatchPhrasePrefix = "phrase_prefix"
2043+instance ToJSON FieldName where
2044+ toJSON (FieldName fieldName) = String fieldName
2046+instance ToJSON ReplicaCount
2047+instance ToJSON ShardCount
2048+instance ToJSON CutoffFrequency
2049+instance ToJSON Analyzer
2050+instance ToJSON MaxExpansions
2051+instance ToJSON Lenient
2052+instance ToJSON Boost
2053+instance ToJSON Version
2054+instance ToJSON Tiebreaker
2055+instance ToJSON MinimumMatch
2056+instance ToJSON DisableCoord
2057+instance ToJSON PrefixLength
2058+instance ToJSON Fuzziness
2059+instance ToJSON IgnoreTermFrequency
2060+instance ToJSON MaxQueryTerms
2061+instance ToJSON TypeName
2062+instance ToJSON IndexName
2063+instance ToJSON TemplateName
2064+instance ToJSON TemplatePattern
2065+instance ToJSON BoostTerms
2066+instance ToJSON MaxWordLength
2067+instance ToJSON MinWordLength
2068+instance ToJSON MaxDocFrequency
2069+instance ToJSON MinDocFrequency
2070+instance ToJSON PhraseSlop
2071+instance ToJSON StopWord
2072+instance ToJSON QueryPath
2073+instance ToJSON MinimumTermFrequency
2074+instance ToJSON PercentMatch
2075+instance ToJSON MappingName
2076+instance ToJSON DocId
2077+instance ToJSON QueryString
2078+instance ToJSON AllowLeadingWildcard
2079+instance ToJSON LowercaseExpanded
2080+instance ToJSON AnalyzeWildcard
2081+instance ToJSON GeneratePhraseQueries
2082+instance ToJSON Locale
2083+instance ToJSON EnablePositionIncrements
2084+instance FromJSON Version
2085+instance FromJSON IndexName
2086+instance FromJSON MappingName
2087+instance FromJSON DocId
2090+instance FromJSON Status where
2091+ parseJSON (Object v) = Status <$>
2097+ parseJSON _ = empty
2100+instance ToJSON IndexSettings where
2101+ toJSON (IndexSettings s r) = object ["settings" .=
2103+ object ["number_of_shards" .= s, "number_of_replicas" .= r]
2107+instance ToJSON IndexTemplate where
2108+ toJSON (IndexTemplate p s m) = merge
2109+ (object [ "template" .= p
2110+ , "mappings" .= foldl' merge (object []) m
2114+ merge (Object o1) (Object o2) = toJSON $ HM.union o1 o2
2116+ merge _ _ = undefined
2118+instance (FromJSON a) => FromJSON (EsResult a) where
2119+ parseJSON jsonVal@(Object v) = do
2120+ found <- v .:? "found" .!= False
2122+ then parseJSON jsonVal
2123+ else return Nothing
2124+ EsResult <$> v .: "_index" <*>
2128+ parseJSON _ = empty
2130+instance (FromJSON a) => FromJSON (EsResultFound a) where
2131+ parseJSON (Object v) = EsResultFound <$>
2132+ v .: "_version" <*>
2134+ parseJSON _ = empty
2136+instance ToJSON Search where
2137+ toJSON (Search query sFilter sort searchAggs highlight sTrackSortScores sFrom sSize _ sFields sSource) =
2138+ omitNulls [ "query" .= query
2139+ , "filter" .= sFilter
2141+ , "aggregations" .= searchAggs
2142+ , "highlight" .= highlight
2145+ , "track_scores" .= sTrackSortScores
2146+ , "fields" .= sFields
2147+ , "_source" .= sSource]
490instance ToJSON Source where
2150instance ToJSON Source where
491 toJSON NoSource = toJSON False
2151 toJSON NoSource = toJSON False
492 toJSON (SourcePatterns patterns) = toJSON patterns
2152 toJSON (SourcePatterns patterns) = toJSON patterns
493− toJSON (SourceIncludeExclude incl excl) = object [ "includes" .= incl, "excludes" .= excl ]
2153+ toJSON (SourceIncludeExclude incl excl) = object [ "include" .= incl, "exclude" .= excl ]
495−data PatternOrPatterns = PopPattern Pattern
496− | PopPatterns [Pattern] deriving (Eq, Read, Show)
498instance ToJSON PatternOrPatterns where
2155instance ToJSON PatternOrPatterns where
499 toJSON (PopPattern pattern) = toJSON pattern
2156 toJSON (PopPattern pattern) = toJSON pattern
500 toJSON (PopPatterns patterns) = toJSON patterns
2157 toJSON (PopPatterns patterns) = toJSON patterns
502−data Include = Include [Pattern] deriving (Eq, Read, Show)
503−data Exclude = Exclude [Pattern] deriving (Eq, Read, Show)
505instance ToJSON Include where
2159instance ToJSON Include where
506 toJSON (Include patterns) = toJSON patterns
2160 toJSON (Include patterns) = toJSON patterns
508instance ToJSON Exclude where
2162instance ToJSON Exclude where
509 toJSON (Exclude patterns) = toJSON patterns
2163 toJSON (Exclude patterns) = toJSON patterns
511−newtype Pattern = Pattern Text deriving (Eq, Read, Show)
513instance ToJSON Pattern where
2165instance ToJSON Pattern where
514 toJSON (Pattern pattern) = toJSON pattern
2166 toJSON (Pattern pattern) = toJSON pattern
516−data SearchResult a =
2168+
517− SearchResult { took :: Int
2169+instance ToJSON FieldHighlight where
518− , timedOut :: Bool
2170+ toJSON (FieldHighlight (FieldName fName) (Just fSettings)) =
519− , shards :: ShardResult
2171+ object [ fName .= fSettings ]
520− , searchHits :: SearchHits a
2172+ toJSON (FieldHighlight (FieldName fName) Nothing) =
521− , aggregations :: Maybe AggregationResults
2173+ object [ fName .= emptyObject ]
522− , scrollId :: Maybe ScrollId
2174+
523− -- ^ Only one Suggestion request / response per
2175+instance ToJSON Highlights where
524− -- Search is supported.
2176+ toJSON (Highlights global fields) =
525− , suggest :: Maybe NamedSuggestionResponse
2177+ omitNulls (("fields" .= fields)
526− }
2178+ : highlightSettingsPairs global)
527− deriving (Eq, Show)
2179+
2180+instance ToJSON HighlightSettings where
2181+ toJSON hs = omitNulls (highlightSettingsPairs (Just hs))
2183+highlightSettingsPairs :: Maybe HighlightSettings -> [Pair]
2184+highlightSettingsPairs Nothing = []
2185+highlightSettingsPairs (Just (Plain plh)) = plainHighPairs (Just plh)
2186+highlightSettingsPairs (Just (Postings ph)) = postHighPairs (Just ph)
2187+highlightSettingsPairs (Just (FastVector fvh)) = fastVectorHighPairs (Just fvh)
2190+plainHighPairs :: Maybe PlainHighlight -> [Pair]
2191+plainHighPairs Nothing = []
2192+plainHighPairs (Just (PlainHighlight plCom plNonPost)) =
2193+ [ "type" .= String "plain"]
2194+ ++ commonHighlightPairs plCom
2195+ ++ nonPostingsToPairs plNonPost
2197+postHighPairs :: Maybe PostingsHighlight -> [Pair]
2198+postHighPairs Nothing = []
2199+postHighPairs (Just (PostingsHighlight pCom)) =
2200+ ("type" .= String "postings")
2201+ : commonHighlightPairs pCom
2203+fastVectorHighPairs :: Maybe FastVectorHighlight -> [Pair]
2204+fastVectorHighPairs Nothing = []
2205+fastVectorHighPairs (Just
2206+ (FastVectorHighlight fvCom fvNonPostSettings fvBoundChars
2207+ fvBoundMaxScan fvFragOff fvMatchedFields
2209+ [ "type" .= String "fvh"
2210+ , "boundary_chars" .= fvBoundChars
2211+ , "boundary_max_scan" .= fvBoundMaxScan
2212+ , "fragment_offset" .= fvFragOff
2213+ , "matched_fields" .= fvMatchedFields
2214+ , "phraseLimit" .= fvPhraseLim]
2215+ ++ commonHighlightPairs fvCom
2216+ ++ nonPostingsToPairs fvNonPostSettings
2218+commonHighlightPairs :: Maybe CommonHighlight -> [Pair]
2219+commonHighlightPairs Nothing = []
2220+commonHighlightPairs (Just (CommonHighlight chScore chForceSource chTag chEncoder
2221+ chNoMatchSize chHighlightQuery
2222+ chRequireFieldMatch)) =
2223+ [ "order" .= chScore
2224+ , "force_source" .= chForceSource
2225+ , "encoder" .= chEncoder
2226+ , "no_match_size" .= chNoMatchSize
2227+ , "highlight_query" .= chHighlightQuery
2228+ , "require_fieldMatch" .= chRequireFieldMatch]
2229+ ++ highlightTagToPairs chTag
2232+nonPostingsToPairs :: Maybe NonPostings -> [Pair]
2233+nonPostingsToPairs Nothing = []
2234+nonPostingsToPairs (Just (NonPostings npFragSize npNumOfFrags)) =
2235+ [ "fragment_size" .= npFragSize
2236+ , "number_of_fragments" .= npNumOfFrags]
2240+instance ToJSON HighlightEncoder where
2241+ toJSON DefaultEncoder = String "default"
2242+ toJSON HTMLEncoder = String "html"
2244+highlightTagToPairs :: Maybe HighlightTag -> [Pair]
2245+highlightTagToPairs (Just (TagSchema _)) = [ "scheme" .= String "default"]
2246+highlightTagToPairs (Just (CustomTags (pre, post))) = [ "pre_tags" .= pre
2247+ , "post_tags" .= post]
2248+highlightTagToPairs Nothing = []
2250+instance ToJSON SortSpec where
2251+ toJSON (DefaultSortSpec
2252+ (DefaultSort (FieldName dsSortFieldName) dsSortOrder dsIgnoreUnmapped
2253+ dsSortMode dsMissingSort dsNestedFilter)) =
2254+ object [dsSortFieldName .= omitNulls base] where
2255+ base = [ "order" .= dsSortOrder
2256+ , "ignore_unmapped" .= dsIgnoreUnmapped
2257+ , "mode" .= dsSortMode
2258+ , "missing" .= dsMissingSort
2259+ , "nested_filter" .= dsNestedFilter ]
2261+ toJSON (GeoDistanceSortSpec gdsSortOrder (GeoPoint (FieldName field) gdsLatLon) units) =
2262+ object [ "unit" .= units
2263+ , field .= gdsLatLon
2264+ , "order" .= gdsSortOrder ]
2267+instance ToJSON SortOrder where
2268+ toJSON Ascending = String "asc"
2269+ toJSON Descending = String "desc"
2272+instance ToJSON SortMode where
2273+ toJSON SortMin = String "min"
2274+ toJSON SortMax = String "max"
2275+ toJSON SortSum = String "sum"
2276+ toJSON SortAvg = String "avg"
2279+instance ToJSON Missing where
2280+ toJSON LastMissing = String "_last"
2281+ toJSON FirstMissing = String "_first"
2282+ toJSON (CustomMissing txt) = String txt
2285+instance ToJSON ScoreType where
2286+ toJSON ScoreTypeMax = "max"
2287+ toJSON ScoreTypeAvg = "avg"
2288+ toJSON ScoreTypeSum = "sum"
2289+ toJSON ScoreTypeNone = "none"
2292+instance ToJSON Distance where
2293+ toJSON (Distance dCoefficient dUnit) =
2294+ String boltedTogether where
2295+ coefText = showText dCoefficient
2296+ (String unitText) = toJSON dUnit
2297+ boltedTogether = mappend coefText unitText
2300+instance ToJSON DistanceUnit where
2301+ toJSON Miles = String "mi"
2302+ toJSON Yards = String "yd"
2303+ toJSON Feet = String "ft"
2304+ toJSON Inches = String "in"
2305+ toJSON Kilometers = String "km"
2306+ toJSON Meters = String "m"
2307+ toJSON Centimeters = String "cm"
2308+ toJSON Millimeters = String "mm"
2309+ toJSON NauticalMiles = String "nmi"
2312+instance ToJSON DistanceType where
2313+ toJSON Arc = String "arc"
2314+ toJSON SloppyArc = String "sloppy_arc"
2315+ toJSON Plane = String "plane"
2318+instance ToJSON OptimizeBbox where
2319+ toJSON NoOptimizeBbox = String "none"
2320+ toJSON (OptimizeGeoFilterType gft) = toJSON gft
2323+instance ToJSON GeoBoundingBoxConstraint where
2324+ toJSON (GeoBoundingBoxConstraint
2325+ (FieldName gbbcGeoBBField) gbbcConstraintBox cache type') =
2326+ object [gbbcGeoBBField .= gbbcConstraintBox
2327+ , "_cache" .= cache
2331+instance ToJSON GeoFilterType where
2332+ toJSON GeoFilterMemory = String "memory"
2333+ toJSON GeoFilterIndexed = String "indexed"
2336+instance ToJSON GeoBoundingBox where
2337+ toJSON (GeoBoundingBox gbbTopLeft gbbBottomRight) =
2338+ object ["top_left" .= gbbTopLeft
2339+ , "bottom_right" .= gbbBottomRight]
2342+instance ToJSON LatLon where
2343+ toJSON (LatLon lLat lLon) =
2344+ object ["lat" .= lLat
2348+-- index for smaller ranges, fielddata for longer ranges
2349+instance ToJSON RangeExecution where
2350+ toJSON RangeExecutionIndex = "index"
2351+ toJSON RangeExecutionFielddata = "fielddata"
2354+instance ToJSON RegexpFlags where
2355+ toJSON AllRegexpFlags = String "ALL"
2356+ toJSON NoRegexpFlags = String "NONE"
2357+ toJSON (SomeRegexpFlags (h :| fs)) = String $ T.intercalate "|" flagStrs
2358+ where flagStrs = map flagStr . nub $ h:fs
2359+ flagStr AnyString = "ANYSTRING"
2360+ flagStr Automaton = "AUTOMATON"
2361+ flagStr Complement = "COMPLEMENT"
2362+ flagStr Empty = "EMPTY"
2363+ flagStr Intersection = "INTERSECTION"
2364+ flagStr Interval = "INTERVAL"
2366+instance ToJSON Term where
2367+ toJSON (Term field value) = object ["term" .= object
2371+instance ToJSON BoolMatch where
2372+ toJSON (MustMatch term cache) = object ["must" .= term,
2374+ toJSON (MustNotMatch term cache) = object ["must_not" .= term,
2376+ toJSON (ShouldMatch terms cache) = object ["should" .= fmap toJSON terms,
529instance (FromJSON a) => FromJSON (SearchResult a) where
2380instance (FromJSON a) => FromJSON (SearchResult a) where
530 parseJSON (Object v) = SearchResult <$>
2381 parseJSON (Object v) = SearchResult <$>
@@ -533,10 +2384,52 @@ instance (FromJSON a) => FromJSON (SearchResult a) where
533 v .: "_shards" <*>
2384 v .: "_shards" <*>
534 v .: "hits" <*>
2385 v .: "hits" <*>
535 v .:? "aggregations" <*>
2386 v .:? "aggregations" <*>
536− v .:? "_scroll_id" <*>
2387+ v .:? "_scroll_id"
537− v .:? "suggest"
2388+ parseJSON _ = empty
2390+instance (FromJSON a) => FromJSON (SearchHits a) where
2391+ parseJSON (Object v) = SearchHits <$>
2393+ v .: "max_score" <*>
538 parseJSON _ = empty
2395 parseJSON _ = empty
540−newtype ScrollId =
2397+instance (FromJSON a) => FromJSON (Hit a) where
541− ScrollId Text
2398+ parseJSON (Object v) = Hit <$>
542− deriving (Eq, Show, Ord, ToJSON, FromJSON)
2399+ v .: "_index" <*>
2405+ parseJSON _ = empty
2407+instance FromJSON ShardResult where
2408+ parseJSON (Object v) = ShardResult <$>
2410+ v .: "successful" <*>
2412+ parseJSON _ = empty
2415+instance FromJSON DocVersion where
2418+ maybe (fail "DocVersion out of range") return $ mkDocVersion i
2420+instance Bounded DocVersion where
2421+ minBound = DocVersion 1
2422+ maxBound = DocVersion 9200000000000000000 -- 9.2e+18
2424+instance Enum DocVersion where
2426+ | x /= maxBound = DocVersion (succ $ docVersionNumber x)
2427+ | otherwise = succError "DocVersion"
2429+ | x /= minBound = DocVersion (pred $ docVersionNumber x)
2430+ | otherwise = predError "DocVersion"
2432+ fromMaybe (error $ show i ++ " out of DocVersion range") $ mkDocVersion i
2433+ fromEnum = docVersionNumber
2434+ enumFrom = boundedEnumFrom
2435+ enumFromThen = boundedEnumFromThen