git.iko.soy/typeable/bloodhound
on master help sign in
add 'Suggest' types with to/from JSON instances and tests
commita6d11141030c9cbbf438c957623ef97378335bda authorJosh Berman <berman.josh@gmail.com> dateTue 18 Apr 2017, 21:41 (9 years ago) parentfc6fe99
2 files changed+140−2
src/Database/V5/Bloodhound/Types.hs+127−2 tests/V5/tests.hs+13
src/Database/V5/Bloodhound/Types.hs+127−2
@@ -325,6 +325,11 @@ module Database.V5.Bloodhound.Types
325325 , rrGroupRefNum
326326 , mkRRGroupRefNum
327327 , RestoreIndexSettings(..)
328+ , Suggest(..)
329+ , SuggestType(..)
330+ , PhraseSuggester(..)
331+ , PhraseSuggesterHighlighter(..)
332+ , PhraseSuggesterCollate(..)
328333
329334 , Aggregation(..)
330335 , Aggregations
@@ -380,7 +385,8 @@ import Control.Monad.Writer (MonadWriter)
380385import Data.Aeson
381386import Data.Aeson.Types (Pair, Parser,
382387 emptyObject,
383− parseEither, parseMaybe)
388+ parseEither, parseMaybe,
389+ typeMismatch)
384390import qualified Data.ByteString.Lazy.Char8 as L
385391import Data.Char
386392import Data.Hashable (Hashable)
@@ -1097,7 +1103,7 @@ unpackId (DocId docId) = docId
10971103
10981104type TrackSortScores = Bool
10991105newtype From = From Int deriving (Eq, Read, Show, Generic, ToJSON)
1100−newtype Size = Size Int deriving (Eq, Read, Show, Generic, ToJSON)
1106+newtype Size = Size Int deriving (Eq, Read, Show, Generic, ToJSON, FromJSON)
11011107
11021108data Search = Search { queryBody :: Maybe Query
11031109 , filterBody :: Maybe Filter
@@ -5095,3 +5101,122 @@ newtype MaybeNA a = MaybeNA { unMaybeNA :: Maybe a }
50955101instance FromJSON a => FromJSON (MaybeNA a) where
50965102 parseJSON (String "NA") = pure $ MaybeNA Nothing
50975103 parseJSON o = MaybeNA . Just <$> parseJSON o
5104+
5105+data Suggest = Suggest { suggestText :: Text
5106+ , suggestName :: Text
5107+ , suggestType :: SuggestType
5108+ }
5109+ deriving (Show, Generic)
5110+
5111+instance ToJSON Suggest where
5112+ toJSON Suggest{..} = object [ "text" .= suggestText
5113+ , suggestName .= suggestType
5114+ ]
5115+
5116+instance FromJSON Suggest where
5117+ parseJSON (Object o) = do
5118+ suggestText' <- o .: "text"
5119+ let dropTextList = HM.toList $ HM.filterWithKey (\x _ -> x /= "text") o
5120+ suggestName' <- case dropTextList of
5121+ [(x, _)] -> return x
5122+ _ -> fail "error parsing Suggest field name"
5123+ suggestType' <- o .: suggestName'
5124+ return $ Suggest suggestText' suggestName' suggestType'
5125+ parseJSON x = typeMismatch "Suggest" x
5126+
5127+data SuggestType = SuggestTypePhraseSuggester PhraseSuggester
5128+ deriving (Show, Generic)
5129+
5130+instance ToJSON SuggestType where
5131+ toJSON (SuggestTypePhraseSuggester x) = object ["phrase" .= x]
5132+
5133+instance FromJSON SuggestType where
5134+ parseJSON = withObject "SuggestType" parse
5135+ where parse o = phraseSuggester `taggedWith` "phrase"
5136+ where taggedWith parser k = parser =<< o .: k
5137+ phraseSuggester = pure . SuggestTypePhraseSuggester
5138+
5139+data PhraseSuggester =
5140+ PhraseSuggester { phraseSuggesterField :: FieldName
5141+ , phraseSuggesterGramSize :: Maybe Int
5142+ , phraseSuggesterRealWordErrorLikelihood :: Maybe Int
5143+ , phraseSuggesterConfidence :: Maybe Int
5144+ , phraseSuggesterMaxErrors :: Maybe Int
5145+ , phraseSuggesterSeparator :: Maybe Text
5146+ , phraseSuggesterSize :: Maybe Size
5147+ , phraseSuggesterAnalyzer :: Maybe Analyzer
5148+ , phraseSuggesterShardSize :: Maybe Int
5149+ , phraseSuggesterHighlight :: Maybe PhraseSuggesterHighlighter
5150+ , phraseSuggesterCollate :: Maybe PhraseSuggesterCollate
5151+ }
5152+ deriving (Show, Generic)
5153+
5154+instance ToJSON PhraseSuggester where
5155+ toJSON PhraseSuggester{..} = omitNulls [ "field" .= phraseSuggesterField
5156+ , "gram_size" .= phraseSuggesterGramSize
5157+ , "real_word_error_likelihood" .= phraseSuggesterRealWordErrorLikelihood
5158+ , "confidence" .= phraseSuggesterConfidence
5159+ , "max_errors" .= phraseSuggesterMaxErrors
5160+ , "separator" .= phraseSuggesterSeparator
5161+ , "size" .= phraseSuggesterSize
5162+ , "analyzer" .= phraseSuggesterAnalyzer
5163+ , "shard_size" .= phraseSuggesterShardSize
5164+ , "highlight" .= phraseSuggesterHighlight
5165+ , "collate" .= phraseSuggesterCollate
5166+ ]
5167+
5168+instance FromJSON PhraseSuggester where
5169+ parseJSON = withObject "PhraseSuggester" parse
5170+ where parse o = PhraseSuggester
5171+ <$> o .: "field"
5172+ <*> o .:? "gram_size"
5173+ <*> o .:? "real_word_error_likelihood"
5174+ <*> o .:? "confidence"
5175+ <*> o .:? "max_errors"
5176+ <*> o .:? "separator"
5177+ <*> o .:? "size"
5178+ <*> o .:? "analyzer"
5179+ <*> o .:? "shard_size"
5180+ <*> o .:? "highlight"
5181+ <*> o .:? "collate"
5182+
5183+data PhraseSuggesterHighlighter =
5184+ PhraseSuggesterHighlighter { phraseSuggesterHighlighterPreTag :: Text
5185+ , phraseSuggesterHighlighterPostTag :: Text
5186+ }
5187+ deriving (Show, Generic)
5188+
5189+instance ToJSON PhraseSuggesterHighlighter where
5190+ toJSON PhraseSuggesterHighlighter{..} =
5191+ object [ "pre_tag" .= phraseSuggesterHighlighterPreTag
5192+ , "post_tag" .= phraseSuggesterHighlighterPostTag
5193+ ]
5194+
5195+instance FromJSON PhraseSuggesterHighlighter where
5196+ parseJSON = withObject "PhraseSuggesterHighlighter" parse
5197+ where parse o = PhraseSuggesterHighlighter
5198+ <$> o .: "pre_tag"
5199+ <*> o .: "post_tag"
5200+
5201+data PhraseSuggesterCollate =
5202+ PhraseSuggesterCollate { phraseSuggesterCollateTemplateQuery :: TemplateQueryInline
5203+ , phraseSuggesterCollatePrune :: Bool
5204+ }
5205+ deriving (Show, Generic)
5206+
5207+instance ToJSON PhraseSuggesterCollate where
5208+ toJSON PhraseSuggesterCollate{..} = object [ "query" .= object
5209+ [ "inline" .= (inline phraseSuggesterCollateTemplateQuery)
5210+ ]
5211+ , "params" .= (params phraseSuggesterCollateTemplateQuery)
5212+ , "prune" .= phraseSuggesterCollatePrune
5213+ ]
5214+
5215+instance FromJSON PhraseSuggesterCollate where
5216+ parseJSON (Object o) = do
5217+ query' <- o .: "query"
5218+ inline' <- query' .: "inline"
5219+ params' <- o .: "params"
5220+ prune' <- o .:? "prune" .!= False
5221+ return $ PhraseSuggesterCollate (TemplateQueryInline inline' params') prune'
5222+ parseJSON x = typeMismatch "PhraseSuggesterCollate" x
tests/V5/tests.hs+13
@@ -610,6 +610,12 @@ instance ApproxEq BuildHash
610610instance ApproxEq TemplateQueryKeyValuePairs where
611611 (=~) = (==)
612612instance ApproxEq TemplateQueryInline
613+instance ApproxEq Size
614+instance ApproxEq PhraseSuggesterHighlighter
615+instance ApproxEq PhraseSuggesterCollate
616+instance ApproxEq PhraseSuggester
617+instance ApproxEq SuggestType
618+instance ApproxEq Suggest
613619
614620-- | Due to the way nodeattrfilters get serialized here, they may come
615621-- out in a different order, but they are morally equivalent
@@ -907,6 +913,12 @@ instance Arbitrary CompoundFormat where arbitrary = sopArbitrary; shrink = gener
907913instance Arbitrary FsSnapshotRepo where arbitrary = sopArbitrary; shrink = genericShrink
908914instance Arbitrary SnapshotRepoName where arbitrary = sopArbitrary; shrink = genericShrink
909915instance Arbitrary TemplateQueryInline where arbitrary = sopArbitrary; shrink = genericShrink
916+instance Arbitrary PhraseSuggesterCollate where arbitrary = sopArbitrary; shrink = genericShrink
917+instance Arbitrary PhraseSuggesterHighlighter where arbitrary = sopArbitrary; shrink = genericShrink
918+instance Arbitrary Size where arbitrary = sopArbitrary; shrink = genericShrink
919+instance Arbitrary PhraseSuggester where arbitrary = sopArbitrary; shrink = genericShrink
920+instance Arbitrary SuggestType where arbitrary = sopArbitrary; shrink = genericShrink
921+instance Arbitrary Suggest where arbitrary = sopArbitrary; shrink = genericShrink
910922
911923newtype UpdatableIndexSetting' = UpdatableIndexSetting' UpdatableIndexSetting
912924 deriving (Show, Eq, ToJSON, FromJSON, ApproxEq, Typeable)
@@ -1673,6 +1685,7 @@ main = hspec $ do
16731685 propJSON (Proxy :: Proxy FSType)
16741686 propJSON (Proxy :: Proxy CompoundFormat)
16751687 propJSON (Proxy :: Proxy TemplateQueryInline)
1688+ propJSON (Proxy :: Proxy Suggest)
16761689
16771690-- Temporary solution for lacking of generic derivation of Arbitrary
16781691-- We use generics-sop, as it's much more concise than directly using GHC.Generics