git.iko.soy/typeable/bloodhound
on 977d2e8 help sign in
541 lines 16 kB history raw
1{-# LANGUAGE FlexibleContexts #-}
2{-# LANGUAGE FlexibleInstances #-}
3{-# LANGUAGE GeneralizedNewtypeDeriving #-}
4{-# LANGUAGE MultiParamTypeClasses #-}
5{-# LANGUAGE NamedFieldPuns #-}
6{-# LANGUAGE OverloadedStrings #-}
7{-# LANGUAGE RecordWildCards #-}
8{-# LANGUAGE UndecidableInstances #-}
10-------------------------------------------------------------------------------
11-- |
12-- Module : Database.Bloodhound.Types
13-- Copyright : (C) 2014, 2018 Chris Allen
14-- License : BSD-style (see the file LICENSE)
15-- Maintainer : Chris Allen <cma@bitemyapp.com
16-- Stability : provisional
17-- Portability : RecordWildCards
18--
19-- Data types for describing actions and data structures performed to interact
20-- with Elasticsearch. The two main buckets your queries against Elasticsearch
21-- will fall into are 'Query's and 'Filter's. 'Filter's are more like
22-- traditional database constraints and often have preferable performance
23-- properties. 'Query's support human-written textual queries, such as fuzzy
24-- queries.
25-------------------------------------------------------------------------------
29module Database.Bloodhound.Types
30 ( defaultCache
31 , defaultIndexSettings
32 , defaultIndexDocumentSettings
33 , mkSort
34 , showText
35 , unpackId
36 , mkMatchQuery
37 , mkMultiMatchQuery
38 , mkBoolQuery
39 , mkRangeQuery
40 , mkQueryStringQuery
41 , mkAggregations
42 , mkTermsAggregation
43 , mkTermsScriptAggregation
44 , mkDateHistogram
45 , mkCardinalityAggregation
46 , mkDocVersion
47 , mkStatsAggregation
48 , mkExtendedStatsAggregation
49 , docVersionNumber
50 , toMissing
51 , toTerms
52 , toDateHistogram
53 , toTopHits
54 , omitNulls
55 , BH(..)
56 , runBH
57 , BHEnv
58 , bhServer
59 , bhManager
60 , bhRequestHook
61 , mkBHEnv
62 , MonadBH(..)
63 , Version(..)
64 , VersionNumber(..)
65 , MaybeNA(..)
66 , BuildHash(..)
67 , Status(..)
68 , Existence(..)
69 , NullValue(..)
70 , IndexSettings(..)
71 , UpdatableIndexSetting(..)
72 , IndexSettingsSummary(..)
73 , AllocationPolicy(..)
74 , Compression(..)
75 , ReplicaBounds(..)
76 , Bytes(..)
77 , gigabytes
78 , megabytes
79 , kilobytes
80 , FSType(..)
81 , InitialShardCount(..)
82 , NodeAttrFilter(..)
83 , NodeAttrName(..)
84 , CompoundFormat(..)
85 , IndexTemplate(..)
86 , Server(..)
87 , Reply
88 , EsResult(..)
89 , EsResultFound(..)
90 , EsError(..)
91 , EsProtocolException(..)
92 , IndexAlias(..)
93 , IndexAliasName(..)
94 , IndexAliasAction(..)
95 , IndexAliasCreate(..)
96 , IndexAliasSummary(..)
97 , IndexAliasesSummary(..)
98 , AliasRouting(..)
99 , SearchAliasRouting(..)
100 , IndexAliasRouting(..)
101 , RoutingValue(..)
102 , DocVersion
103 , ExternalDocVersion(..)
104 , VersionControl(..)
105 , DocumentParent(..)
106 , IndexDocumentSettings(..)
107 , Query(..)
108 , Search(..)
109 , SearchType(..)
110 , SearchResult(..)
111 , ScrollId(..)
112 , SearchHits(..)
113 , TrackSortScores
114 , From(..)
115 , Size(..)
116 , Source(..)
117 , PatternOrPatterns(..)
118 , Include(..)
119 , Exclude(..)
120 , Pattern(..)
121 , ShardResult(..)
122 , Hit(..)
123 , HitFields(..)
124 , Filter(..)
125 , BoolMatch(..)
126 , Term(..)
127 , GeoPoint(..)
128 , GeoBoundingBoxConstraint(..)
129 , GeoBoundingBox(..)
130 , GeoFilterType(..)
131 , Distance(..)
132 , DistanceUnit(..)
133 , DistanceType(..)
134 , DistanceRange(..)
135 , OptimizeBbox(..)
136 , LatLon(..)
137 , RangeValue(..)
138 , RangeExecution(..)
139 , LessThan(..)
140 , LessThanEq(..)
141 , GreaterThan(..)
142 , GreaterThanEq(..)
143 , LessThanD(..)
144 , LessThanEqD(..)
145 , GreaterThanD(..)
146 , GreaterThanEqD(..)
147 , Regexp(..)
148 , RegexpFlags(..)
149 , RegexpFlag(..)
150 , FieldName(..)
151 , ScriptFields(..)
152 , ScriptFieldName
153 , ScriptFieldValue
154 , Script(..)
155 , ScriptLanguage(..)
156 , ScriptInline(..)
157 , ScriptId(..)
158 , ScriptParams(..)
159 , ScriptParamName
160 , ScriptParamValue
161 , IndexName(..)
162 , IndexSelection(..)
163 , NodeSelection(..)
164 , NodeSelector(..)
165 , ForceMergeIndexSettings(..)
166 , defaultForceMergeIndexSettings
167 , TemplateName(..)
168 , TemplatePattern(..)
169 , MappingName(..)
170 , DocId(..)
171 , CacheName(..)
172 , CacheKey(..)
173 , BulkOperation(..)
174 , ReplicaCount(..)
175 , ShardCount(..)
176 , Sort
177 , SortMode(..)
178 , SortOrder(..)
179 , SortSpec(..)
180 , DefaultSort(..)
181 , Missing(..)
182 , OpenCloseIndex(..)
183 , Method
184 , Boost(..)
185 , MatchQuery(..)
186 , MultiMatchQuery(..)
187 , BoolQuery(..)
188 , BoostingQuery(..)
189 , CommonTermsQuery(..)
190 , FunctionScoreQuery(..)
191 , BoostMode(..)
192 , ScoreMode(..)
193 , FunctionScoreFunctions(..)
194 , ComponentFunctionScoreFunction(..)
195 , FunctionScoreFunction(..)
196 , Weight(..)
197 , Seed(..)
198 , FieldValueFactor(..)
199 , Factor(..)
200 , FactorModifier(..)
201 , FactorMissingFieldValue(..)
202 , DisMaxQuery(..)
203 , FuzzyLikeThisQuery(..)
204 , FuzzyLikeFieldQuery(..)
205 , FuzzyQuery(..)
206 , HasChildQuery(..)
207 , HasParentQuery(..)
208 , IndicesQuery(..)
209 , MoreLikeThisQuery(..)
210 , MoreLikeThisFieldQuery(..)
211 , NestedQuery(..)
212 , PrefixQuery(..)
213 , QueryStringQuery(..)
214 , SimpleQueryStringQuery(..)
215 , RangeQuery(..)
216 , RegexpQuery(..)
217 , QueryString(..)
218 , TemplateQueryInline(..)
219 , TemplateQueryKeyValuePairs(..)
220 , WildcardQuery(..)
221 , BooleanOperator(..)
222 , ZeroTermsQuery(..)
223 , CutoffFrequency(..)
224 , Analyzer(..)
225 , Tokenizer(..)
226 , TokenFilter(..)
227 , CharFilter(..)
228 , MaxExpansions(..)
229 , Lenient(..)
230 , MatchQueryType(..)
231 , MultiMatchQueryType(..)
232 , Tiebreaker(..)
233 , MinimumMatch(..)
234 , DisableCoord(..)
235 , CommonMinimumMatch(..)
236 , MinimumMatchHighLow(..)
237 , PrefixLength(..)
238 , Fuzziness(..)
239 , IgnoreTermFrequency(..)
240 , MaxQueryTerms(..)
241 , ScoreType(..)
242 , Score
243 , Cache
244 , TypeName(..)
245 , BoostTerms(..)
246 , MaxWordLength(..)
247 , MinWordLength(..)
248 , MaxDocFrequency(..)
249 , MinDocFrequency(..)
250 , PhraseSlop(..)
251 , StopWord(..)
252 , QueryPath(..)
253 , MinimumTermFrequency(..)
254 , PercentMatch(..)
255 , FieldDefinition(..)
256 , MappingField(..)
257 , Mapping(..)
258 , AllowLeadingWildcard(..)
259 , LowercaseExpanded(..)
260 , GeneratePhraseQueries(..)
261 , Locale(..)
262 , AnalyzeWildcard(..)
263 , EnablePositionIncrements(..)
264 , SimpleQueryFlag(..)
265 , FieldOrFields(..)
266 , Monoid(..)
267 , ToJSON(..)
268 , Interval(..)
269 , TimeInterval(..)
270 , ExecutionHint(..)
271 , CollectionMode(..)
272 , TermOrder(..)
273 , TermInclusion(..)
274 , SnapshotRepoSelection(..)
275 , GenericSnapshotRepo(..)
276 , SnapshotRepo(..)
277 , SnapshotRepoConversionError(..)
278 , SnapshotRepoType(..)
279 , GenericSnapshotRepoSettings(..)
280 , SnapshotRepoUpdateSettings(..)
281 , defaultSnapshotRepoUpdateSettings
282 , SnapshotRepoName(..)
283 , SnapshotRepoPattern(..)
284 , SnapshotVerification(..)
285 , SnapshotNodeVerification(..)
286 , FullNodeId(..)
287 , NodeName(..)
288 , ClusterName(..)
289 , NodesInfo(..)
290 , NodesStats(..)
291 , NodeStats(..)
292 , NodeBreakersStats(..)
293 , NodeBreakerStats(..)
294 , NodeHTTPStats(..)
295 , NodeTransportStats(..)
296 , NodeFSStats(..)
297 , NodeDataPathStats(..)
298 , NodeFSTotalStats(..)
299 , NodeNetworkStats(..)
300 , NodeThreadPoolsStats(..)
301 , NodeThreadPoolStats(..)
302 , NodeJVMStats(..)
303 , JVMBufferPoolStats(..)
304 , JVMGCStats(..)
305 , JVMPoolStats(..)
306 , NodeProcessStats(..)
307 , NodeOSStats(..)
308 , LoadAvgs(..)
309 , NodeIndicesStats(..)
310 , EsAddress(..)
311 , PluginName(..)
312 , NodeInfo(..)
313 , NodePluginInfo(..)
314 , NodeHTTPInfo(..)
315 , NodeTransportInfo(..)
316 , BoundTransportAddress(..)
317 , NodeNetworkInfo(..)
318 , MacAddress(..)
319 , NetworkInterfaceName(..)
320 , NodeNetworkInterface(..)
321 , NodeThreadPoolsInfo(..)
322 , NodeThreadPoolInfo(..)
323 , ThreadPoolSize(..)
324 , ThreadPoolType(..)
325 , NodeJVMInfo(..)
326 , JVMMemoryPool(..)
327 , JVMGCCollector(..)
328 , JVMMemoryInfo(..)
329 , PID(..)
330 , NodeOSInfo(..)
331 , CPUInfo(..)
332 , NodeProcessInfo(..)
333 , FsSnapshotRepo(..)
334 , SnapshotCreateSettings(..)
335 , defaultSnapshotCreateSettings
336 , SnapshotSelection(..)
337 , SnapshotPattern(..)
338 , SnapshotInfo(..)
339 , SnapshotShardFailure(..)
340 , ShardId(..)
341 , SnapshotName(..)
342 , SnapshotState(..)
343 , SnapshotRestoreSettings(..)
344 , defaultSnapshotRestoreSettings
345 , RestoreRenamePattern(..)
346 , RestoreRenameToken(..)
347 , RRGroupRefNum
348 , rrGroupRefNum
349 , mkRRGroupRefNum
350 , RestoreIndexSettings(..)
351 , Suggest(..)
352 , SuggestType(..)
353 , PhraseSuggester(..)
354 , PhraseSuggesterHighlighter(..)
355 , PhraseSuggesterCollate(..)
356 , mkPhraseSuggester
357 , SuggestOptions(..)
358 , SuggestResponse(..)
359 , NamedSuggestionResponse(..)
360 , DirectGenerators(..)
361 , mkDirectGenerators
362 , DirectGeneratorSuggestModeTypes (..)
364 , Aggregation(..)
365 , Aggregations
366 , AggregationResults
367 , BucketValue(..)
368 , Bucket(..)
369 , BucketAggregation(..)
370 , TermsAggregation(..)
371 , MissingAggregation(..)
372 , ValueCountAggregation(..)
373 , FilterAggregation(..)
374 , CardinalityAggregation(..)
375 , DateHistogramAggregation(..)
376 , DateRangeAggregation(..)
377 , DateRangeAggRange(..)
378 , DateMathExpr(..)
379 , DateMathAnchor(..)
380 , DateMathModifier(..)
381 , DateMathUnit(..)
382 , TopHitsAggregation(..)
383 , StatisticsAggregation(..)
384 , SearchAfterKey
386 , Highlights(..)
387 , FieldHighlight(..)
388 , HighlightSettings(..)
389 , PlainHighlight(..)
390 , PostingsHighlight(..)
391 , FastVectorHighlight(..)
392 , CommonHighlight(..)
393 , NonPostings(..)
394 , HighlightEncoder(..)
395 , HighlightTag(..)
396 , HitHighlight
398 , MissingResult(..)
399 , TermsResult(..)
400 , DateHistogramResult(..)
401 , DateRangeResult(..)
402 , TopHitResult(..)
404 , EsUsername(..)
405 , EsPassword(..)
407 , Analysis(..)
408 , AnalyzerDefinition(..)
409 , TokenizerDefinition(..)
410 , TokenFilterDefinition(..)
411 , CharFilterDefinition(..)
412 , Ngram(..)
413 , TokenChar(..)
414 , Shingle(..)
415 , Language(..)
416 ) where
418import Bloodhound.Import
420import Database.Bloodhound.Internal.Aggregation
421import Database.Bloodhound.Internal.Analysis
422import Database.Bloodhound.Internal.Client
423import Database.Bloodhound.Internal.Highlight
424import Database.Bloodhound.Internal.Newtypes
425import Database.Bloodhound.Internal.Query
426import Database.Bloodhound.Internal.Sort
427import Database.Bloodhound.Internal.Suggest
429{-| 'unpackId' is a silly convenience function that gets used once.
430-}
431unpackId :: DocId -> Text
432unpackId (DocId docId) = docId
434type TrackSortScores = Bool
436data Search = Search { queryBody :: Maybe Query
437 , filterBody :: Maybe Filter
438 , sortBody :: Maybe Sort
439 , aggBody :: Maybe Aggregations
440 , highlight :: Maybe Highlights
441 -- default False
442 , trackSortScores :: TrackSortScores
443 , from :: From
444 , size :: Size
445 , searchType :: SearchType
446 , searchAfterKey :: Maybe SearchAfterKey
447 , fields :: Maybe [FieldName]
448 , scriptFields :: Maybe ScriptFields
449 , source :: Maybe Source
450 , suggestBody :: Maybe Suggest -- ^ Only one Suggestion request / response per Search is supported.
451 } deriving (Eq, Show)
454instance ToJSON Search where
455 toJSON (Search mquery sFilter sort searchAggs
456 highlight sTrackSortScores sFrom sSize _ sAfter sFields
457 sScriptFields sSource sSuggest) =
458 omitNulls [ "query" .= query'
459 , "sort" .= sort
460 , "aggregations" .= searchAggs
461 , "highlight" .= highlight
462 , "from" .= sFrom
463 , "size" .= sSize
464 , "track_scores" .= sTrackSortScores
465 , "search_after" .= sAfter
466 , "fields" .= sFields
467 , "script_fields" .= sScriptFields
468 , "_source" .= sSource
469 , "suggest" .= sSuggest]
471 where query' = case sFilter of
472 Nothing -> mquery
473 Just x ->
474 Just
475 . QueryBoolQuery
476 $ mkBoolQuery (maybeToList mquery)
477 [x] [] []
479data SearchType = SearchTypeQueryThenFetch
480 | SearchTypeDfsQueryThenFetch
481 deriving (Eq, Show)
483data Source =
484 NoSource
485 | SourcePatterns PatternOrPatterns
486 | SourceIncludeExclude Include Exclude
487 deriving (Eq, Show)
489instance ToJSON Source where
490 toJSON NoSource = toJSON False
491 toJSON (SourcePatterns patterns) = toJSON patterns
492 toJSON (SourceIncludeExclude incl excl) = object [ "includes" .= incl, "excludes" .= excl ]
494data PatternOrPatterns = PopPattern Pattern
495 | PopPatterns [Pattern] deriving (Eq, Read, Show)
497instance ToJSON PatternOrPatterns where
498 toJSON (PopPattern pattern) = toJSON pattern
499 toJSON (PopPatterns patterns) = toJSON patterns
501data Include = Include [Pattern] deriving (Eq, Read, Show)
502data Exclude = Exclude [Pattern] deriving (Eq, Read, Show)
504instance ToJSON Include where
505 toJSON (Include patterns) = toJSON patterns
507instance ToJSON Exclude where
508 toJSON (Exclude patterns) = toJSON patterns
510newtype Pattern = Pattern Text deriving (Eq, Read, Show)
512instance ToJSON Pattern where
513 toJSON (Pattern pattern) = toJSON pattern
515data SearchResult a =
516 SearchResult { took :: Int
517 , timedOut :: Bool
518 , shards :: ShardResult
519 , searchHits :: SearchHits a
520 , aggregations :: Maybe AggregationResults
521 , scrollId :: Maybe ScrollId
522 -- ^ Only one Suggestion request / response per
523 -- Search is supported.
524 , suggest :: Maybe NamedSuggestionResponse
525 }
526 deriving (Eq, Show)
528instance (FromJSON a) => FromJSON (SearchResult a) where
529 parseJSON (Object v) = SearchResult <$>
530 v .: "took" <*>
531 v .: "timed_out" <*>
532 v .: "_shards" <*>
533 v .: "hits" <*>
534 v .:? "aggregations" <*>
535 v .:? "_scroll_id" <*>
536 v .:? "suggest"
537 parseJSON _ = empty
539newtype ScrollId =
540 ScrollId Text
541 deriving (Eq, Show, Ord, ToJSON, FromJSON)