git.iko.soy/typeable/bloodhound
on master help sign in
Split out suggest, more cleanup
50d015dChris Allen
bloodhound.cabal+1
@@ -45,6 +45,7 @@ library
45 Database.V5.Bloodhound.Internal.Query
45 Database.V5.Bloodhound.Internal.Query
46 Database.V5.Bloodhound.Internal.Sort
46 Database.V5.Bloodhound.Internal.Sort
47 Database.V5.Bloodhound.Internal.StringlyTyped
47 Database.V5.Bloodhound.Internal.StringlyTyped
48+ Database.V5.Bloodhound.Internal.Suggest
48 Database.V1.Bloodhound
49 Database.V1.Bloodhound
49 Database.V1.Bloodhound.Client
50 Database.V1.Bloodhound.Client
50 Database.V1.Bloodhound.Types
51 Database.V1.Bloodhound.Types
src/Database/V5/Bloodhound/Internal/Aggregation.hs+161
@@ -6,10 +6,12 @@ module Database.V5.Bloodhound.Internal.Aggregation where
6
6
7import Bloodhound.Import
7import Bloodhound.Import
8
8
9+import qualified Data.HashMap.Strict as HM
9import qualified Data.Map.Strict as M
10import qualified Data.Map.Strict as M
10import qualified Data.Text as T
11import qualified Data.Text as T
11
12
12import Database.V5.Bloodhound.Internal.Client
13import Database.V5.Bloodhound.Internal.Client
14+import Database.V5.Bloodhound.Internal.Highlight (HitHighlight)
13import Database.V5.Bloodhound.Internal.Newtypes
15import Database.V5.Bloodhound.Internal.Newtypes
14import Database.V5.Bloodhound.Internal.Query
16import Database.V5.Bloodhound.Internal.Query
15import Database.V5.Bloodhound.Internal.Sort
17import Database.V5.Bloodhound.Internal.Sort
@@ -304,3 +306,162 @@ data DateMathUnit =
304 | DMMinute
306 | DMMinute
305 | DMSecond
307 | DMSecond
306 deriving (Eq, Show)
308 deriving (Eq, Show)
309+
310+data TermsResult = TermsResult
311+ { termKey :: BucketValue
312+ , termsDocCount :: Int
313+ , termsAggs :: Maybe AggregationResults
314+ } deriving (Read, Show)
315+
316+instance FromJSON TermsResult where
317+ parseJSON (Object v) = TermsResult <$>
318+ v .: "key" <*>
319+ v .: "doc_count" <*>
320+ (pure $ getNamedSubAgg v ["key", "doc_count"])
321+ parseJSON _ = mempty
322+
323+instance BucketAggregation TermsResult where
324+ key = termKey
325+ docCount = termsDocCount
326+ aggs = termsAggs
327+
328+data DateHistogramResult = DateHistogramResult
329+ { dateKey :: Int
330+ , dateKeyStr :: Maybe Text
331+ , dateDocCount :: Int
332+ , dateHistogramAggs :: Maybe AggregationResults
333+ } deriving (Show)
334+
335+instance FromJSON DateHistogramResult where
336+ parseJSON (Object v) = DateHistogramResult <$>
337+ v .: "key" <*>
338+ v .:? "key_as_string" <*>
339+ v .: "doc_count" <*>
340+ (pure $ getNamedSubAgg v [ "key"
341+ , "doc_count"
342+ , "key_as_string"
343+ ]
344+ )
345+ parseJSON _ = mempty
346+
347+instance BucketAggregation DateHistogramResult where
348+ key = TextValue . showText . dateKey
349+ docCount = dateDocCount
350+ aggs = dateHistogramAggs
351+
352+data DateRangeResult = DateRangeResult
353+ { dateRangeKey :: Text
354+ , dateRangeFrom :: Maybe UTCTime
355+ , dateRangeFromAsString :: Maybe Text
356+ , dateRangeTo :: Maybe UTCTime
357+ , dateRangeToAsString :: Maybe Text
358+ , dateRangeDocCount :: Int
359+ , dateRangeAggs :: Maybe AggregationResults
360+ } deriving (Eq, Show)
361+
362+instance FromJSON DateRangeResult where
363+ parseJSON = withObject "DateRangeResult" parse
364+ where parse v = DateRangeResult <$>
365+ v .: "key" <*>
366+ (fmap posixMS <$> v .:? "from") <*>
367+ v .:? "from_as_string" <*>
368+ (fmap posixMS <$> v .:? "to") <*>
369+ v .:? "to_as_string" <*>
370+ v .: "doc_count" <*>
371+ (pure $ getNamedSubAgg v [ "key"
372+ , "from"
373+ , "from_as_string"
374+ , "to"
375+ , "to_as_string"
376+ , "doc_count"
377+ ]
378+ )
379+
380+instance BucketAggregation DateRangeResult where
381+ key = TextValue . dateRangeKey
382+ docCount = dateRangeDocCount
383+ aggs = dateRangeAggs
384+
385+toTerms :: Text -> AggregationResults -> Maybe (Bucket TermsResult)
386+toTerms = toAggResult
387+
388+toDateHistogram :: Text -> AggregationResults -> Maybe (Bucket DateHistogramResult)
389+toDateHistogram = toAggResult
390+
391+toMissing :: Text -> AggregationResults -> Maybe MissingResult
392+toMissing = toAggResult
393+
394+toTopHits :: (FromJSON a) => Text -> AggregationResults -> Maybe (TopHitResult a)
395+toTopHits = toAggResult
396+
397+toAggResult :: (FromJSON a) => Text -> AggregationResults -> Maybe a
398+toAggResult t a = M.lookup t a >>= deserialize
399+ where deserialize = parseMaybe parseJSON
400+
401+-- Try to get an AggregationResults when we don't know the
402+-- field name. We filter out the known keys to try to minimize the noise.
403+getNamedSubAgg :: Object -> [Text] -> Maybe AggregationResults
404+getNamedSubAgg o knownKeys = maggRes
405+ where unknownKeys = HM.filterWithKey (\k _ -> k `notElem` knownKeys) o
406+ maggRes
407+ | HM.null unknownKeys = Nothing
408+ | otherwise = Just . M.fromList $ HM.toList unknownKeys
409+
410+data MissingResult = MissingResult
411+ { missingDocCount :: Int
412+ } deriving (Show)
413+
414+instance FromJSON MissingResult where
415+ parseJSON = withObject "MissingResult" parse
416+ where parse v = MissingResult <$> v .: "doc_count"
417+
418+data TopHitResult a = TopHitResult
419+ { tarHits :: (SearchHits a)
420+ } deriving Show
421+
422+instance (FromJSON a) => FromJSON (TopHitResult a) where
423+ parseJSON (Object v) = TopHitResult <$>
424+ v .: "hits"
425+ parseJSON _ = fail "Failure in FromJSON (TopHitResult a)"
426+
427+data SearchHits a =
428+ SearchHits { hitsTotal :: Int
429+ , maxScore :: Score
430+ , hits :: [Hit a] } deriving (Eq, Show)
431+
432+
433+instance (FromJSON a) => FromJSON (SearchHits a) where
434+ parseJSON (Object v) = SearchHits <$>
435+ v .: "total" <*>
436+ v .: "max_score" <*>
437+ v .: "hits"
438+ parseJSON _ = empty
439+
440+instance Semigroup (SearchHits a) where
441+ (SearchHits ta ma ha) <> (SearchHits tb mb hb) =
442+ SearchHits (ta + tb) (max ma mb) (ha <> hb)
443+
444+instance Monoid (SearchHits a) where
445+ mempty = SearchHits 0 Nothing mempty
446+ mappend = (<>)
447+
448+
449+data Hit a =
450+ Hit { hitIndex :: IndexName
451+ , hitType :: MappingName
452+ , hitDocId :: DocId
453+ , hitScore :: Score
454+ , hitSource :: Maybe a
455+ , hitFields :: Maybe HitFields
456+ , hitHighlight :: Maybe HitHighlight } deriving (Eq, Show)
457+
458+instance (FromJSON a) => FromJSON (Hit a) where
459+ parseJSON (Object v) = Hit <$>
460+ v .: "_index" <*>
461+ v .: "_type" <*>
462+ v .: "_id" <*>
463+ v .: "_score" <*>
464+ v .:? "_source" <*>
465+ v .:? "fields" <*>
466+ v .:? "highlight"
467+ parseJSON _ = empty
src/Database/V5/Bloodhound/Internal/Highlight.hs+83−2
@@ -5,8 +5,6 @@ module Database.V5.Bloodhound.Internal.Highlight where
5
5
6import Bloodhound.Import
6import Bloodhound.Import
7
7
8−-- import Data.Aeson
9−-- import Data.Aeson.Types (Parser)
10import qualified Data.Map.Strict as M
8import qualified Data.Map.Strict as M
11
9
12import Database.V5.Bloodhound.Internal.Newtypes
10import Database.V5.Bloodhound.Internal.Newtypes
@@ -19,17 +17,32 @@ data Highlights = Highlights
19 , highlightFields :: [FieldHighlight]
17 , highlightFields :: [FieldHighlight]
20 } deriving (Eq, Show)
18 } deriving (Eq, Show)
21
19
20+instance ToJSON Highlights where
21+ toJSON (Highlights global fields) =
22+ omitNulls (("fields" .= fields)
23+ : highlightSettingsPairs global)
24+
22data FieldHighlight =
25data FieldHighlight =
23 FieldHighlight FieldName (Maybe HighlightSettings)
26 FieldHighlight FieldName (Maybe HighlightSettings)
24 deriving (Eq, Show)
27 deriving (Eq, Show)
25
28
26
29
30+instance ToJSON FieldHighlight where
31+ toJSON (FieldHighlight (FieldName fName) (Just fSettings)) =
32+ object [ fName .= fSettings ]
33+ toJSON (FieldHighlight (FieldName fName) Nothing) =
34+ object [ fName .= emptyObject ]
35+
27data HighlightSettings =
36data HighlightSettings =
28 Plain PlainHighlight
37 Plain PlainHighlight
29 | Postings PostingsHighlight
38 | Postings PostingsHighlight
30 | FastVector FastVectorHighlight
39 | FastVector FastVectorHighlight
31 deriving (Eq, Show)
40 deriving (Eq, Show)
32
41
42+
43+instance ToJSON HighlightSettings where
44+ toJSON hs = omitNulls (highlightSettingsPairs (Just hs))
45+
33data PlainHighlight =
46data PlainHighlight =
34 PlainHighlight { plainCommon :: Maybe CommonHighlight
47 PlainHighlight { plainCommon :: Maybe CommonHighlight
35 , plainNonPost :: Maybe NonPostings }
48 , plainNonPost :: Maybe NonPostings }
@@ -81,3 +94,71 @@ data HighlightTag =
81 -- Only uses more than the first value in the lists if fvh
94 -- Only uses more than the first value in the lists if fvh
82 | CustomTags ([Text], [Text])
95 | CustomTags ([Text], [Text])
83 deriving (Eq, Show)
96 deriving (Eq, Show)
97+
98+highlightSettingsPairs :: Maybe HighlightSettings -> [Pair]
99+highlightSettingsPairs Nothing = []
100+highlightSettingsPairs (Just (Plain plh)) = plainHighPairs (Just plh)
101+highlightSettingsPairs (Just (Postings ph)) = postHighPairs (Just ph)
102+highlightSettingsPairs (Just (FastVector fvh)) = fastVectorHighPairs (Just fvh)
103+
104+
105+plainHighPairs :: Maybe PlainHighlight -> [Pair]
106+plainHighPairs Nothing = []
107+plainHighPairs (Just (PlainHighlight plCom plNonPost)) =
108+ [ "type" .= String "plain"]
109+ ++ commonHighlightPairs plCom
110+ ++ nonPostingsToPairs plNonPost
111+
112+postHighPairs :: Maybe PostingsHighlight -> [Pair]
113+postHighPairs Nothing = []
114+postHighPairs (Just (PostingsHighlight pCom)) =
115+ ("type" .= String "postings")
116+ : commonHighlightPairs pCom
117+
118+fastVectorHighPairs :: Maybe FastVectorHighlight -> [Pair]
119+fastVectorHighPairs Nothing = []
120+fastVectorHighPairs
121+ (Just
122+ (FastVectorHighlight fvCom fvNonPostSettings' fvBoundChars
123+ fvBoundMaxScan fvFragOff fvMatchedFields
124+ fvPhraseLim)) =
125+ [ "type" .= String "fvh"
126+ , "boundary_chars" .= fvBoundChars
127+ , "boundary_max_scan" .= fvBoundMaxScan
128+ , "fragment_offset" .= fvFragOff
129+ , "matched_fields" .= fvMatchedFields
130+ , "phraseLimit" .= fvPhraseLim]
131+ ++ commonHighlightPairs fvCom
132+ ++ nonPostingsToPairs fvNonPostSettings'
133+
134+commonHighlightPairs :: Maybe CommonHighlight -> [Pair]
135+commonHighlightPairs Nothing = []
136+commonHighlightPairs (Just (CommonHighlight chScore chForceSource
137+ chTag chEncoder chNoMatchSize
138+ chHighlightQuery chRequireFieldMatch)) =
139+ [ "order" .= chScore
140+ , "force_source" .= chForceSource
141+ , "encoder" .= chEncoder
142+ , "no_match_size" .= chNoMatchSize
143+ , "highlight_query" .= chHighlightQuery
144+ , "require_fieldMatch" .= chRequireFieldMatch
145+ ]
146+ ++ highlightTagToPairs chTag
147+
148+
149+nonPostingsToPairs :: Maybe NonPostings -> [Pair]
150+nonPostingsToPairs Nothing = []
151+nonPostingsToPairs (Just (NonPostings npFragSize npNumOfFrags)) =
152+ [ "fragment_size" .= npFragSize
153+ , "number_of_fragments" .= npNumOfFrags
154+ ]
155+
156+highlightTagToPairs :: Maybe HighlightTag -> [Pair]
157+highlightTagToPairs (Just (TagSchema _)) =
158+ [ "scheme" .= String "default"
159+ ]
160+highlightTagToPairs (Just (CustomTags (pre, post))) =
161+ [ "pre_tags" .= pre
162+ , "post_tags" .= post
163+ ]
164+highlightTagToPairs Nothing = []
src/Database/V5/Bloodhound/Internal/Suggest.hsnew file+265
@@ -0,0 +1,265 @@
1+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
2+{-# LANGUAGE RecordWildCards #-}
3+{-# LANGUAGE OverloadedStrings #-}
4+
5+module Database.V5.Bloodhound.Internal.Suggest where
6+
7+import Bloodhound.Import
8+
9+import qualified Data.HashMap.Strict as HM
10+
11+import Database.V5.Bloodhound.Internal.Newtypes
12+import Database.V5.Bloodhound.Internal.Query (TemplateQueryInline(..), params)
13+
14+data Suggest = Suggest
15+ { suggestText :: Text
16+ , suggestName :: Text
17+ , suggestType :: SuggestType
18+ } deriving (Eq, Show)
19+
20+instance ToJSON Suggest where
21+ toJSON Suggest{..} =
22+ object [ "text" .= suggestText
23+ , suggestName .= suggestType
24+ ]
25+
26+instance FromJSON Suggest where
27+ parseJSON (Object o) = do
28+ suggestText' <- o .: "text"
29+ let dropTextList =
30+ HM.toList
31+ $ HM.filterWithKey (\x _ -> x /= "text") o
32+ suggestName' <-
33+ case dropTextList of
34+ [(x, _)] -> return x
35+ _ -> fail "error parsing Suggest field name"
36+ suggestType' <- o .: suggestName'
37+ return $ Suggest suggestText' suggestName' suggestType'
38+ parseJSON x = typeMismatch "Suggest" x
39+
40+data SuggestType =
41+ SuggestTypePhraseSuggester PhraseSuggester
42+ deriving (Eq, Show)
43+
44+instance ToJSON SuggestType where
45+ toJSON (SuggestTypePhraseSuggester x) =
46+ object [ "phrase" .= x ]
47+
48+instance FromJSON SuggestType where
49+ parseJSON = withObject "SuggestType" parse
50+ where parse o = phraseSuggester `taggedWith` "phrase"
51+ where taggedWith parser k = parser =<< o .: k
52+ phraseSuggester = pure . SuggestTypePhraseSuggester
53+
54+data PhraseSuggester = PhraseSuggester
55+ { phraseSuggesterField :: FieldName
56+ , phraseSuggesterGramSize :: Maybe Int
57+ , phraseSuggesterRealWordErrorLikelihood :: Maybe Int
58+ , phraseSuggesterConfidence :: Maybe Int
59+ , phraseSuggesterMaxErrors :: Maybe Int
60+ , phraseSuggesterSeparator :: Maybe Text
61+ , phraseSuggesterSize :: Maybe Size
62+ , phraseSuggesterAnalyzer :: Maybe Analyzer
63+ , phraseSuggesterShardSize :: Maybe Int
64+ , phraseSuggesterHighlight :: Maybe PhraseSuggesterHighlighter
65+ , phraseSuggesterCollate :: Maybe PhraseSuggesterCollate
66+ , phraseSuggesterCandidateGenerators :: [DirectGenerators]
67+ } deriving (Eq, Show)
68+
69+instance ToJSON PhraseSuggester where
70+ toJSON PhraseSuggester{..} =
71+ omitNulls [ "field" .= phraseSuggesterField
72+ , "gram_size" .= phraseSuggesterGramSize
73+ , "real_word_error_likelihood" .=
74+ phraseSuggesterRealWordErrorLikelihood
75+ , "confidence" .= phraseSuggesterConfidence
76+ , "max_errors" .= phraseSuggesterMaxErrors
77+ , "separator" .= phraseSuggesterSeparator
78+ , "size" .= phraseSuggesterSize
79+ , "analyzer" .= phraseSuggesterAnalyzer
80+ , "shard_size" .= phraseSuggesterShardSize
81+ , "highlight" .= phraseSuggesterHighlight
82+ , "collate" .= phraseSuggesterCollate
83+ , "direct_generator" .=
84+ phraseSuggesterCandidateGenerators
85+ ]
86+
87+instance FromJSON PhraseSuggester where
88+ parseJSON = withObject "PhraseSuggester" parse
89+ where parse o = PhraseSuggester
90+ <$> o .: "field"
91+ <*> o .:? "gram_size"
92+ <*> o .:? "real_word_error_likelihood"
93+ <*> o .:? "confidence"
94+ <*> o .:? "max_errors"
95+ <*> o .:? "separator"
96+ <*> o .:? "size"
97+ <*> o .:? "analyzer"
98+ <*> o .:? "shard_size"
99+ <*> o .:? "highlight"
100+ <*> o .:? "collate"
101+ <*> o .:? "direct_generator" .!= []
102+
103+mkPhraseSuggester :: FieldName -> PhraseSuggester
104+mkPhraseSuggester fName =
105+ PhraseSuggester fName Nothing Nothing Nothing Nothing Nothing Nothing
106+ Nothing Nothing Nothing Nothing []
107+
108+data PhraseSuggesterHighlighter =
109+ PhraseSuggesterHighlighter { phraseSuggesterHighlighterPreTag :: Text
110+ , phraseSuggesterHighlighterPostTag :: Text
111+ }
112+ deriving (Eq, Show)
113+
114+instance ToJSON PhraseSuggesterHighlighter where
115+ toJSON PhraseSuggesterHighlighter{..} =
116+ object [ "pre_tag" .= phraseSuggesterHighlighterPreTag
117+ , "post_tag" .= phraseSuggesterHighlighterPostTag
118+ ]
119+
120+instance FromJSON PhraseSuggesterHighlighter where
121+ parseJSON = withObject "PhraseSuggesterHighlighter" parse
122+ where parse o = PhraseSuggesterHighlighter
123+ <$> o .: "pre_tag"
124+ <*> o .: "post_tag"
125+
126+data PhraseSuggesterCollate = PhraseSuggesterCollate
127+ { phraseSuggesterCollateTemplateQuery :: TemplateQueryInline
128+ , phraseSuggesterCollatePrune :: Bool
129+ } deriving (Eq, Show)
130+
131+instance ToJSON PhraseSuggesterCollate where
132+ toJSON PhraseSuggesterCollate{..} =
133+ object [ "query" .= object
134+ [ "inline" .= (inline phraseSuggesterCollateTemplateQuery)
135+ ]
136+ , "params" .= (params phraseSuggesterCollateTemplateQuery)
137+ , "prune" .= phraseSuggesterCollatePrune
138+ ]
139+
140+instance FromJSON PhraseSuggesterCollate where
141+ parseJSON (Object o) = do
142+ query' <- o .: "query"
143+ inline' <- query' .: "inline"
144+ params' <- o .: "params"
145+ prune' <- o .:? "prune" .!= False
146+ return $ PhraseSuggesterCollate
147+ (TemplateQueryInline inline' params') prune'
148+ parseJSON x = typeMismatch "PhraseSuggesterCollate" x
149+
150+data SuggestOptions =
151+ SuggestOptions { suggestOptionsText :: Text
152+ , suggestOptionsScore :: Double
153+ , suggestOptionsFreq :: Maybe Int
154+ , suggestOptionsHighlighted :: Maybe Text
155+ }
156+ deriving (Eq, Read, Show)
157+
158+instance FromJSON SuggestOptions where
159+ parseJSON = withObject "SuggestOptions" parse
160+ where
161+ parse o = SuggestOptions
162+ <$> o .: "text"
163+ <*> o .: "score"
164+ <*> o .:? "freq"
165+ <*> o .:? "highlighted"
166+
167+data SuggestResponse =
168+ SuggestResponse { suggestResponseText :: Text
169+ , suggestResponseOffset :: Int
170+ , suggestResponseLength :: Int
171+ , suggestResponseOptions :: [SuggestOptions]
172+ }
173+ deriving (Eq, Read, Show)
174+
175+instance FromJSON SuggestResponse where
176+ parseJSON = withObject "SuggestResponse" parse
177+ where parse o = SuggestResponse
178+ <$> o .: "text"
179+ <*> o .: "offset"
180+ <*> o .: "length"
181+ <*> o .: "options"
182+
183+data NamedSuggestionResponse = NamedSuggestionResponse
184+ { nsrName :: Text
185+ , nsrResponses :: [SuggestResponse]
186+ } deriving (Eq, Read, Show)
187+
188+instance FromJSON NamedSuggestionResponse where
189+ parseJSON (Object o) = do
190+ suggestionName' <- case HM.toList o of
191+ [(x, _)] -> return x
192+ _ -> fail "error parsing NamedSuggestionResponse name"
193+ suggestionResponses' <- o .: suggestionName'
194+ return $ NamedSuggestionResponse suggestionName' suggestionResponses'
195+
196+ parseJSON x = typeMismatch "NamedSuggestionResponse" x
197+
198+data DirectGeneratorSuggestModeTypes = DirectGeneratorSuggestModeMissing
199+ | DirectGeneratorSuggestModePopular
200+ | DirectGeneratorSuggestModeAlways
201+ deriving (Eq, Show)
202+
203+instance ToJSON DirectGeneratorSuggestModeTypes where
204+ toJSON DirectGeneratorSuggestModeMissing = "missing"
205+ toJSON DirectGeneratorSuggestModePopular = "popular"
206+ toJSON DirectGeneratorSuggestModeAlways = "always"
207+
208+instance FromJSON DirectGeneratorSuggestModeTypes where
209+ parseJSON = withText "DirectGeneratorSuggestModeTypes" parse
210+ where
211+ parse "missing" =
212+ pure DirectGeneratorSuggestModeMissing
213+ parse "popular" =
214+ pure DirectGeneratorSuggestModePopular
215+ parse "always" =
216+ pure DirectGeneratorSuggestModeAlways
217+ parse f =
218+ fail ("Unexpected DirectGeneratorSuggestModeTypes: " <> show f)
219+
220+data DirectGenerators = DirectGenerators
221+ { directGeneratorsField :: FieldName
222+ , directGeneratorsSize :: Maybe Int
223+ , directGeneratorSuggestMode :: DirectGeneratorSuggestModeTypes
224+ , directGeneratorMaxEdits :: Maybe Double
225+ , directGeneratorPrefixLength :: Maybe Int
226+ , directGeneratorMinWordLength :: Maybe Int
227+ , directGeneratorMaxInspections :: Maybe Int
228+ , directGeneratorMinDocFreq :: Maybe Double
229+ , directGeneratorMaxTermFreq :: Maybe Double
230+ , directGeneratorPreFilter :: Maybe Text
231+ , directGeneratorPostFilter :: Maybe Text
232+ } deriving (Eq, Show)
233+
234+instance ToJSON DirectGenerators where
235+ toJSON DirectGenerators{..} =
236+ omitNulls [ "field" .= directGeneratorsField
237+ , "size" .= directGeneratorsSize
238+ , "suggest_mode" .= directGeneratorSuggestMode
239+ , "max_edits" .= directGeneratorMaxEdits
240+ , "prefix_length" .= directGeneratorPrefixLength
241+ , "min_word_length" .= directGeneratorMinWordLength
242+ , "max_inspections" .= directGeneratorMaxInspections
243+ , "min_doc_freq" .= directGeneratorMinDocFreq
244+ , "max_term_freq" .= directGeneratorMaxTermFreq
245+ , "pre_filter" .= directGeneratorPreFilter
246+ , "post_filter" .= directGeneratorPostFilter
247+ ]
248+
249+instance FromJSON DirectGenerators where
250+ parseJSON = withObject "DirectGenerators" parse
251+ where parse o = DirectGenerators
252+ <$> o .: "field"
253+ <*> o .:? "size"
254+ <*> o .: "suggest_mode"
255+ <*> o .:? "max_edits"
256+ <*> o .:? "prefix_length"
257+ <*> o .:? "min_word_length"
258+ <*> o .:? "max_inspections"
259+ <*> o .:? "min_doc_freq"
260+ <*> o .:? "max_term_freq"
261+ <*> o .:? "pre_filter"
262+ <*> o .:? "post_filter"
263+
264+mkDirectGenerators :: FieldName -> DirectGenerators
265+mkDirectGenerators fn = DirectGenerators fn Nothing DirectGeneratorSuggestModeMissing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing
src/Database/V5/Bloodhound/Types.hs+21−515
@@ -414,9 +414,6 @@ module Database.V5.Bloodhound.Types
414
414
415import Bloodhound.Import
415import Bloodhound.Import
416
416
417−import qualified Data.HashMap.Strict as HM
418−import qualified Data.Map.Strict as M
419−
420import Database.V5.Bloodhound.Types.Class
417import Database.V5.Bloodhound.Types.Class
421import Database.V5.Bloodhound.Internal.Analysis
418import Database.V5.Bloodhound.Internal.Analysis
422import Database.V5.Bloodhound.Internal.Aggregation
419import Database.V5.Bloodhound.Internal.Aggregation
@@ -425,6 +422,7 @@ import Database.V5.Bloodhound.Internal.Highlight
425import Database.V5.Bloodhound.Internal.Newtypes
422import Database.V5.Bloodhound.Internal.Newtypes
426import Database.V5.Bloodhound.Internal.Query
423import Database.V5.Bloodhound.Internal.Query
427import Database.V5.Bloodhound.Internal.Sort
424import Database.V5.Bloodhound.Internal.Sort
425+import Database.V5.Bloodhound.Internal.Suggest
428
426
429{-| 'unpackId' is a silly convenience function that gets used once.
427{-| 'unpackId' is a silly convenience function that gets used once.
430-}
428-}
@@ -484,14 +482,32 @@ data Source =
484 | SourceIncludeExclude Include Exclude
482 | SourceIncludeExclude Include Exclude
485 deriving (Eq, Show)
483 deriving (Eq, Show)
486
484
485+instance ToJSON Source where
486+ toJSON NoSource = toJSON False
487+ toJSON (SourcePatterns patterns) = toJSON patterns
488+ toJSON (SourceIncludeExclude incl excl) = object [ "includes" .= incl, "excludes" .= excl ]
489+
487data PatternOrPatterns = PopPattern Pattern
490data PatternOrPatterns = PopPattern Pattern
488 | PopPatterns [Pattern] deriving (Eq, Read, Show)
491 | PopPatterns [Pattern] deriving (Eq, Read, Show)
489
492
493+instance ToJSON PatternOrPatterns where
494+ toJSON (PopPattern pattern) = toJSON pattern
495+ toJSON (PopPatterns patterns) = toJSON patterns
496+
490data Include = Include [Pattern] deriving (Eq, Read, Show)
497data Include = Include [Pattern] deriving (Eq, Read, Show)
491data Exclude = Exclude [Pattern] deriving (Eq, Read, Show)
498data Exclude = Exclude [Pattern] deriving (Eq, Read, Show)
492
499
500+instance ToJSON Include where
501+ toJSON (Include patterns) = toJSON patterns
502+
503+instance ToJSON Exclude where
504+ toJSON (Exclude patterns) = toJSON patterns
505+
493newtype Pattern = Pattern Text deriving (Eq, Read, Show)
506newtype Pattern = Pattern Text deriving (Eq, Read, Show)
494
507
508+instance ToJSON Pattern where
509+ toJSON (Pattern pattern) = toJSON pattern
510+
495data SearchResult a =
511data SearchResult a =
496 SearchResult { took :: Int
512 SearchResult { took :: Int
497 , timedOut :: Bool
513 , timedOut :: Bool
@@ -499,7 +515,8 @@ data SearchResult a =
499 , searchHits :: SearchHits a
515 , searchHits :: SearchHits a
500 , aggregations :: Maybe AggregationResults
516 , aggregations :: Maybe AggregationResults
501 , scrollId :: Maybe ScrollId
517 , scrollId :: Maybe ScrollId
502− -- ^ Only one Suggestion request / response per Search is supported.
518+ -- ^ Only one Suggestion request / response per
519+ -- Search is supported.
503 , suggest :: Maybe NamedSuggestionResponse
520 , suggest :: Maybe NamedSuggestionResponse
504 }
521 }
505 deriving (Eq, Show)
522 deriving (Eq, Show)
@@ -518,514 +535,3 @@ instance (FromJSON a) => FromJSON (SearchResult a) where
518newtype ScrollId =
535newtype ScrollId =
519 ScrollId Text
536 ScrollId Text
520 deriving (Eq, Show, Ord, ToJSON, FromJSON)
537 deriving (Eq, Show, Ord, ToJSON, FromJSON)
521−
522−data SearchHits a =
523− SearchHits { hitsTotal :: Int
524− , maxScore :: Score
525− , hits :: [Hit a] } deriving (Eq, Show)
526−
527−
528−instance (FromJSON a) => FromJSON (SearchHits a) where
529− parseJSON (Object v) = SearchHits <$>
530− v .: "total" <*>
531− v .: "max_score" <*>
532− v .: "hits"
533− parseJSON _ = empty
534−
535−instance Semigroup (SearchHits a) where
536− (SearchHits ta ma ha) <> (SearchHits tb mb hb) =
537− SearchHits (ta + tb) (max ma mb) (ha <> hb)
538−
539−instance Monoid (SearchHits a) where
540− mempty = SearchHits 0 Nothing mempty
541− mappend = (<>)
542−
543−data Hit a =
544− Hit { hitIndex :: IndexName
545− , hitType :: MappingName
546− , hitDocId :: DocId
547− , hitScore :: Score
548− , hitSource :: Maybe a
549− , hitFields :: Maybe HitFields
550− , hitHighlight :: Maybe HitHighlight } deriving (Eq, Show)
551−
552−instance (FromJSON a) => FromJSON (Hit a) where
553− parseJSON (Object v) = Hit <$>
554− v .: "_index" <*>
555− v .: "_type" <*>
556− v .: "_id" <*>
557− v .: "_score" <*>
558− v .:? "_source" <*>
559− v .:? "fields" <*>
560− v .:? "highlight"
561− parseJSON _ = empty
562−
563−data MissingResult = MissingResult
564− { missingDocCount :: Int
565− } deriving (Show)
566−
567−instance FromJSON MissingResult where
568− parseJSON = withObject "MissingResult" parse
569− where parse v = MissingResult <$> v .: "doc_count"
570−
571−data TopHitResult a = TopHitResult
572− { tarHits :: (SearchHits a)
573− } deriving Show
574−
575−instance (FromJSON a) => FromJSON (TopHitResult a) where
576− parseJSON (Object v) = TopHitResult <$>
577− v .: "hits"
578− parseJSON _ = fail "Failure in FromJSON (TopHitResult a)"
579−
580−data TermsResult = TermsResult
581− { termKey :: BucketValue
582− , termsDocCount :: Int
583− , termsAggs :: Maybe AggregationResults
584− } deriving (Read, Show)
585−
586−instance FromJSON TermsResult where
587− parseJSON (Object v) = TermsResult <$>
588− v .: "key" <*>
589− v .: "doc_count" <*>
590− (pure $ getNamedSubAgg v ["key", "doc_count"])
591− parseJSON _ = mempty
592−
593−instance BucketAggregation TermsResult where
594− key = termKey
595− docCount = termsDocCount
596− aggs = termsAggs
597−
598−data DateHistogramResult = DateHistogramResult
599− { dateKey :: Int
600− , dateKeyStr :: Maybe Text
601− , dateDocCount :: Int
602− , dateHistogramAggs :: Maybe AggregationResults
603− } deriving (Show)
604−
605−instance FromJSON DateHistogramResult where
606− parseJSON (Object v) = DateHistogramResult <$>
607− v .: "key" <*>
608− v .:? "key_as_string" <*>
609− v .: "doc_count" <*>
610− (pure $ getNamedSubAgg v [ "key"
611− , "doc_count"
612− , "key_as_string"
613− ]
614− )
615− parseJSON _ = mempty
616−
617−instance BucketAggregation DateHistogramResult where
618− key = TextValue . showText . dateKey
619− docCount = dateDocCount
620− aggs = dateHistogramAggs
621−
622−data DateRangeResult = DateRangeResult
623− { dateRangeKey :: Text
624− , dateRangeFrom :: Maybe UTCTime
625− , dateRangeFromAsString :: Maybe Text
626− , dateRangeTo :: Maybe UTCTime
627− , dateRangeToAsString :: Maybe Text
628− , dateRangeDocCount :: Int
629− , dateRangeAggs :: Maybe AggregationResults
630− } deriving (Eq, Show)
631−
632−instance FromJSON DateRangeResult where
633− parseJSON = withObject "DateRangeResult" parse
634− where parse v = DateRangeResult <$>
635− v .: "key" <*>
636− (fmap posixMS <$> v .:? "from") <*>
637− v .:? "from_as_string" <*>
638− (fmap posixMS <$> v .:? "to") <*>
639− v .:? "to_as_string" <*>
640− v .: "doc_count" <*>
641− (pure $ getNamedSubAgg v [ "key"
642− , "from"
643− , "from_as_string"
644− , "to"
645− , "to_as_string"
646− , "doc_count"
647− ]
648− )
649−
650−instance BucketAggregation DateRangeResult where
651− key = TextValue . dateRangeKey
652− docCount = dateRangeDocCount
653− aggs = dateRangeAggs
654−
655−toTerms :: Text -> AggregationResults -> Maybe (Bucket TermsResult)
656−toTerms = toAggResult
657−
658−toDateHistogram :: Text -> AggregationResults -> Maybe (Bucket DateHistogramResult)
659−toDateHistogram = toAggResult
660−
661−toMissing :: Text -> AggregationResults -> Maybe MissingResult
662−toMissing = toAggResult
663−
664−toTopHits :: (FromJSON a) => Text -> AggregationResults -> Maybe (TopHitResult a)
665−toTopHits = toAggResult
666−
667−toAggResult :: (FromJSON a) => Text -> AggregationResults -> Maybe a
668−toAggResult t a = M.lookup t a >>= deserialize
669− where deserialize = parseMaybe parseJSON
670−
671−-- Try to get an AggregationResults when we don't know the
672−-- field name. We filter out the known keys to try to minimize the noise.
673−getNamedSubAgg :: Object -> [Text] -> Maybe AggregationResults
674−getNamedSubAgg o knownKeys = maggRes
675− where unknownKeys = HM.filterWithKey (\k _ -> k `notElem` knownKeys) o
676− maggRes
677− | HM.null unknownKeys = Nothing
678− | otherwise = Just . M.fromList $ HM.toList unknownKeys
679−
680−instance ToJSON Source where
681− toJSON NoSource = toJSON False
682− toJSON (SourcePatterns patterns) = toJSON patterns
683− toJSON (SourceIncludeExclude incl excl) = object [ "includes" .= incl, "excludes" .= excl ]
684−
685−instance ToJSON PatternOrPatterns where
686− toJSON (PopPattern pattern) = toJSON pattern
687− toJSON (PopPatterns patterns) = toJSON patterns
688−
689−instance ToJSON Include where
690− toJSON (Include patterns) = toJSON patterns
691−
692−instance ToJSON Exclude where
693− toJSON (Exclude patterns) = toJSON patterns
694−
695−instance ToJSON Pattern where
696− toJSON (Pattern pattern) = toJSON pattern
697−
698−
699−instance ToJSON FieldHighlight where
700− toJSON (FieldHighlight (FieldName fName) (Just fSettings)) =
701− object [ fName .= fSettings ]
702− toJSON (FieldHighlight (FieldName fName) Nothing) =
703− object [ fName .= emptyObject ]
704−
705−instance ToJSON Highlights where
706− toJSON (Highlights global fields) =
707− omitNulls (("fields" .= fields)
708− : highlightSettingsPairs global)
709−
710−instance ToJSON HighlightSettings where
711− toJSON hs = omitNulls (highlightSettingsPairs (Just hs))
712−
713−highlightSettingsPairs :: Maybe HighlightSettings -> [Pair]
714−highlightSettingsPairs Nothing = []
715−highlightSettingsPairs (Just (Plain plh)) = plainHighPairs (Just plh)
716−highlightSettingsPairs (Just (Postings ph)) = postHighPairs (Just ph)
717−highlightSettingsPairs (Just (FastVector fvh)) = fastVectorHighPairs (Just fvh)
718−
719−
720−plainHighPairs :: Maybe PlainHighlight -> [Pair]
721−plainHighPairs Nothing = []
722−plainHighPairs (Just (PlainHighlight plCom plNonPost)) =
723− [ "type" .= String "plain"]
724− ++ commonHighlightPairs plCom
725− ++ nonPostingsToPairs plNonPost
726−
727−postHighPairs :: Maybe PostingsHighlight -> [Pair]
728−postHighPairs Nothing = []
729−postHighPairs (Just (PostingsHighlight pCom)) =
730− ("type" .= String "postings")
731− : commonHighlightPairs pCom
732−
733−fastVectorHighPairs :: Maybe FastVectorHighlight -> [Pair]
734−fastVectorHighPairs Nothing = []
735−fastVectorHighPairs (Just
736− (FastVectorHighlight fvCom fvNonPostSettings fvBoundChars
737− fvBoundMaxScan fvFragOff fvMatchedFields
738− fvPhraseLim)) =
739− [ "type" .= String "fvh"
740− , "boundary_chars" .= fvBoundChars
741− , "boundary_max_scan" .= fvBoundMaxScan
742− , "fragment_offset" .= fvFragOff
743− , "matched_fields" .= fvMatchedFields
744− , "phraseLimit" .= fvPhraseLim]
745− ++ commonHighlightPairs fvCom
746− ++ nonPostingsToPairs fvNonPostSettings
747−
748−commonHighlightPairs :: Maybe CommonHighlight -> [Pair]
749−commonHighlightPairs Nothing = []
750−commonHighlightPairs (Just (CommonHighlight chScore chForceSource
751− chTag chEncoder chNoMatchSize
752− chHighlightQuery chRequireFieldMatch)) =
753− [ "order" .= chScore
754− , "force_source" .= chForceSource
755− , "encoder" .= chEncoder
756− , "no_match_size" .= chNoMatchSize
757− , "highlight_query" .= chHighlightQuery
758− , "require_fieldMatch" .= chRequireFieldMatch
759− ]
760− ++ highlightTagToPairs chTag
761−
762−
763−nonPostingsToPairs :: Maybe NonPostings -> [Pair]
764−nonPostingsToPairs Nothing = []
765−nonPostingsToPairs (Just (NonPostings npFragSize npNumOfFrags)) =
766− [ "fragment_size" .= npFragSize
767− , "number_of_fragments" .= npNumOfFrags
768− ]
769−
770−highlightTagToPairs :: Maybe HighlightTag -> [Pair]
771−highlightTagToPairs (Just (TagSchema _)) =
772− [ "scheme" .= String "default"
773− ]
774−highlightTagToPairs (Just (CustomTags (pre, post))) =
775− [ "pre_tags" .= pre
776− , "post_tags" .= post
777− ]
778−highlightTagToPairs Nothing = []
779−
780−data Suggest = Suggest
781− { suggestText :: Text
782− , suggestName :: Text
783− , suggestType :: SuggestType
784− } deriving (Eq, Show)
785−
786−instance ToJSON Suggest where
787− toJSON Suggest{..} =
788− object [ "text" .= suggestText
789− , suggestName .= suggestType
790− ]
791−
792−instance FromJSON Suggest where
793− parseJSON (Object o) = do
794− suggestText' <- o .: "text"
795− let dropTextList =
796− HM.toList
797− $ HM.filterWithKey (\x _ -> x /= "text") o
798− suggestName' <-
799− case dropTextList of
800− [(x, _)] -> return x
801− _ -> fail "error parsing Suggest field name"
802− suggestType' <- o .: suggestName'
803− return $ Suggest suggestText' suggestName' suggestType'
804− parseJSON x = typeMismatch "Suggest" x
805−
806−data SuggestType =
807− SuggestTypePhraseSuggester PhraseSuggester
808− deriving (Eq, Show)
809−
810−instance ToJSON SuggestType where
811− toJSON (SuggestTypePhraseSuggester x) =
812− object [ "phrase" .= x ]
813−
814−instance FromJSON SuggestType where
815− parseJSON = withObject "SuggestType" parse
816− where parse o = phraseSuggester `taggedWith` "phrase"
817− where taggedWith parser k = parser =<< o .: k
818− phraseSuggester = pure . SuggestTypePhraseSuggester
819−
820−data PhraseSuggester = PhraseSuggester
821− { phraseSuggesterField :: FieldName
822− , phraseSuggesterGramSize :: Maybe Int
823− , phraseSuggesterRealWordErrorLikelihood :: Maybe Int
824− , phraseSuggesterConfidence :: Maybe Int
825− , phraseSuggesterMaxErrors :: Maybe Int
826− , phraseSuggesterSeparator :: Maybe Text
827− , phraseSuggesterSize :: Maybe Size
828− , phraseSuggesterAnalyzer :: Maybe Analyzer
829− , phraseSuggesterShardSize :: Maybe Int
830− , phraseSuggesterHighlight :: Maybe PhraseSuggesterHighlighter
831− , phraseSuggesterCollate :: Maybe PhraseSuggesterCollate
832− , phraseSuggesterCandidateGenerators :: [DirectGenerators]
833− } deriving (Eq, Show)
834−
835−instance ToJSON PhraseSuggester where
836− toJSON PhraseSuggester{..} =
837− omitNulls [ "field" .= phraseSuggesterField
838− , "gram_size" .= phraseSuggesterGramSize
839− , "real_word_error_likelihood" .=
840− phraseSuggesterRealWordErrorLikelihood
841− , "confidence" .= phraseSuggesterConfidence
842− , "max_errors" .= phraseSuggesterMaxErrors
843− , "separator" .= phraseSuggesterSeparator
844− , "size" .= phraseSuggesterSize
845− , "analyzer" .= phraseSuggesterAnalyzer
846− , "shard_size" .= phraseSuggesterShardSize
847− , "highlight" .= phraseSuggesterHighlight
848− , "collate" .= phraseSuggesterCollate
849− , "direct_generator" .=
850− phraseSuggesterCandidateGenerators
851− ]
852−
853−instance FromJSON PhraseSuggester where
854− parseJSON = withObject "PhraseSuggester" parse
855− where parse o = PhraseSuggester
856− <$> o .: "field"
857− <*> o .:? "gram_size"
858− <*> o .:? "real_word_error_likelihood"
859− <*> o .:? "confidence"
860− <*> o .:? "max_errors"
861− <*> o .:? "separator"
862− <*> o .:? "size"
863− <*> o .:? "analyzer"
864− <*> o .:? "shard_size"
865− <*> o .:? "highlight"
866− <*> o .:? "collate"
867− <*> o .:? "direct_generator" .!= []
868−
869−mkPhraseSuggester :: FieldName -> PhraseSuggester
870−mkPhraseSuggester fName =
871− PhraseSuggester fName Nothing Nothing Nothing Nothing Nothing Nothing
872− Nothing Nothing Nothing Nothing []
873−
874−data PhraseSuggesterHighlighter =
875− PhraseSuggesterHighlighter { phraseSuggesterHighlighterPreTag :: Text
876− , phraseSuggesterHighlighterPostTag :: Text
877− }
878− deriving (Eq, Show)
879−
880−instance ToJSON PhraseSuggesterHighlighter where
881− toJSON PhraseSuggesterHighlighter{..} =
882− object [ "pre_tag" .= phraseSuggesterHighlighterPreTag
883− , "post_tag" .= phraseSuggesterHighlighterPostTag
884− ]
885−
886−instance FromJSON PhraseSuggesterHighlighter where
887− parseJSON = withObject "PhraseSuggesterHighlighter" parse
888− where parse o = PhraseSuggesterHighlighter
889− <$> o .: "pre_tag"
890− <*> o .: "post_tag"
891−
892−data PhraseSuggesterCollate = PhraseSuggesterCollate
893− { phraseSuggesterCollateTemplateQuery :: TemplateQueryInline
894− , phraseSuggesterCollatePrune :: Bool
895− } deriving (Eq, Show)
896−
897−instance ToJSON PhraseSuggesterCollate where
898− toJSON PhraseSuggesterCollate{..} =
899− object [ "query" .= object
900− [ "inline" .= (inline phraseSuggesterCollateTemplateQuery)
901− ]
902− , "params" .= (params phraseSuggesterCollateTemplateQuery)
903− , "prune" .= phraseSuggesterCollatePrune
904− ]
905−
906−instance FromJSON PhraseSuggesterCollate where
907− parseJSON (Object o) = do
908− query' <- o .: "query"
909− inline' <- query' .: "inline"
910− params' <- o .: "params"
911− prune' <- o .:? "prune" .!= False
912− return $ PhraseSuggesterCollate
913− (TemplateQueryInline inline' params') prune'
914− parseJSON x = typeMismatch "PhraseSuggesterCollate" x
915−
916−data SuggestOptions =
917− SuggestOptions { suggestOptionsText :: Text
918− , suggestOptionsScore :: Double
919− , suggestOptionsFreq :: Maybe Int
920− , suggestOptionsHighlighted :: Maybe Text
921− }
922− deriving (Eq, Read, Show)
923−
924−instance FromJSON SuggestOptions where
925− parseJSON = withObject "SuggestOptions" parse
926− where
927− parse o = SuggestOptions
928− <$> o .: "text"
929− <*> o .: "score"
930− <*> o .:? "freq"
931− <*> o .:? "highlighted"
932−
933−data SuggestResponse =
934− SuggestResponse { suggestResponseText :: Text
935− , suggestResponseOffset :: Int
936− , suggestResponseLength :: Int
937− , suggestResponseOptions :: [SuggestOptions]
938− }
939− deriving (Eq, Read, Show)
940−
941−instance FromJSON SuggestResponse where
942− parseJSON = withObject "SuggestResponse" parse
943− where parse o = SuggestResponse
944− <$> o .: "text"
945− <*> o .: "offset"
946− <*> o .: "length"
947− <*> o .: "options"
948−
949−data NamedSuggestionResponse = NamedSuggestionResponse
950− { nsrName :: Text
951− , nsrResponses :: [SuggestResponse]
952− } deriving (Eq, Read, Show)
953−
954−instance FromJSON NamedSuggestionResponse where
955− parseJSON (Object o) = do
956− suggestionName' <- case HM.toList o of
957− [(x, _)] -> return x
958− _ -> fail "error parsing NamedSuggestionResponse name"
959− suggestionResponses' <- o .: suggestionName'
960− return $ NamedSuggestionResponse suggestionName' suggestionResponses'
961−
962− parseJSON x = typeMismatch "NamedSuggestionResponse" x
963−
964−data DirectGeneratorSuggestModeTypes = DirectGeneratorSuggestModeMissing
965− | DirectGeneratorSuggestModePopular
966− | DirectGeneratorSuggestModeAlways
967− deriving (Eq, Show)
968−
969−instance ToJSON DirectGeneratorSuggestModeTypes where
970− toJSON DirectGeneratorSuggestModeMissing = "missing"
971− toJSON DirectGeneratorSuggestModePopular = "popular"
972− toJSON DirectGeneratorSuggestModeAlways = "always"
973−
974−instance FromJSON DirectGeneratorSuggestModeTypes where
975− parseJSON = withText "DirectGeneratorSuggestModeTypes" parse
976− where
977− parse "missing" =
978− pure DirectGeneratorSuggestModeMissing
979− parse "popular" =
980− pure DirectGeneratorSuggestModePopular
981− parse "always" =
982− pure DirectGeneratorSuggestModeAlways
983− parse f =
984− fail ("Unexpected DirectGeneratorSuggestModeTypes: " <> show f)
985−
986−data DirectGenerators = DirectGenerators
987− { directGeneratorsField :: FieldName
988− , directGeneratorsSize :: Maybe Int
989− , directGeneratorSuggestMode :: DirectGeneratorSuggestModeTypes
990− , directGeneratorMaxEdits :: Maybe Double
991− , directGeneratorPrefixLength :: Maybe Int
992− , directGeneratorMinWordLength :: Maybe Int
993− , directGeneratorMaxInspections :: Maybe Int
994− , directGeneratorMinDocFreq :: Maybe Double
995− , directGeneratorMaxTermFreq :: Maybe Double
996− , directGeneratorPreFilter :: Maybe Text
997− , directGeneratorPostFilter :: Maybe Text
998− } deriving (Eq, Show)
999−
1000−instance ToJSON DirectGenerators where
1001− toJSON DirectGenerators{..} =
1002− omitNulls [ "field" .= directGeneratorsField
1003− , "size" .= directGeneratorsSize
1004− , "suggest_mode" .= directGeneratorSuggestMode
1005− , "max_edits" .= directGeneratorMaxEdits
1006− , "prefix_length" .= directGeneratorPrefixLength
1007− , "min_word_length" .= directGeneratorMinWordLength
1008− , "max_inspections" .= directGeneratorMaxInspections
1009− , "min_doc_freq" .= directGeneratorMinDocFreq
1010− , "max_term_freq" .= directGeneratorMaxTermFreq
1011− , "pre_filter" .= directGeneratorPreFilter
1012− , "post_filter" .= directGeneratorPostFilter
1013− ]
1014−
1015−instance FromJSON DirectGenerators where
1016− parseJSON = withObject "DirectGenerators" parse
1017− where parse o = DirectGenerators
1018− <$> o .: "field"
1019− <*> o .:? "size"
1020− <*> o .: "suggest_mode"
1021− <*> o .:? "max_edits"
1022− <*> o .:? "prefix_length"
1023− <*> o .:? "min_word_length"
1024− <*> o .:? "max_inspections"
1025− <*> o .:? "min_doc_freq"
1026− <*> o .:? "max_term_freq"
1027− <*> o .:? "pre_filter"
1028− <*> o .:? "post_filter"
1029−
1030−mkDirectGenerators :: FieldName -> DirectGenerators
1031−mkDirectGenerators fn = DirectGenerators fn Nothing DirectGeneratorSuggestModeMissing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing