{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}

module Database.Bloodhound.Internal.Analysis where

import Bloodhound.Import
import qualified Data.Map.Strict as M
import qualified Data.Text as T
import Database.Bloodhound.Internal.Newtypes
import Database.Bloodhound.Internal.StringlyTyped
import GHC.Generics

data Analysis = Analysis
  { Analysis -> Map Text AnalyzerDefinition
analysisAnalyzer :: M.Map Text AnalyzerDefinition,
    Analysis -> Map Text TokenizerDefinition
analysisTokenizer :: M.Map Text TokenizerDefinition,
    Analysis -> Map Text TokenFilterDefinition
analysisTokenFilter :: M.Map Text TokenFilterDefinition,
    Analysis -> Map Text CharFilterDefinition
analysisCharFilter :: M.Map Text CharFilterDefinition
  }
  deriving (Analysis -> Analysis -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Analysis -> Analysis -> Bool
$c/= :: Analysis -> Analysis -> Bool
== :: Analysis -> Analysis -> Bool
$c== :: Analysis -> Analysis -> Bool
Eq, Int -> Analysis -> ShowS
[Analysis] -> ShowS
Analysis -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Analysis] -> ShowS
$cshowList :: [Analysis] -> ShowS
show :: Analysis -> String
$cshow :: Analysis -> String
showsPrec :: Int -> Analysis -> ShowS
$cshowsPrec :: Int -> Analysis -> ShowS
Show, forall x. Rep Analysis x -> Analysis
forall x. Analysis -> Rep Analysis x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Analysis x -> Analysis
$cfrom :: forall x. Analysis -> Rep Analysis x
Generic)

instance ToJSON Analysis where
  toJSON :: Analysis -> Value
toJSON (Analysis Map Text AnalyzerDefinition
analyzer Map Text TokenizerDefinition
tokenizer Map Text TokenFilterDefinition
tokenFilter Map Text CharFilterDefinition
charFilter) =
    [Pair] -> Value
object
      [ Key
"analyzer" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Map Text AnalyzerDefinition
analyzer,
        Key
"tokenizer" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Map Text TokenizerDefinition
tokenizer,
        Key
"filter" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Map Text TokenFilterDefinition
tokenFilter,
        Key
"char_filter" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Map Text CharFilterDefinition
charFilter
      ]

instance FromJSON Analysis where
  parseJSON :: Value -> Parser Analysis
parseJSON = forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"Analysis" forall a b. (a -> b) -> a -> b
$ \Object
m ->
    Map Text AnalyzerDefinition
-> Map Text TokenizerDefinition
-> Map Text TokenFilterDefinition
-> Map Text CharFilterDefinition
-> Analysis
Analysis
      forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
m forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"analyzer"
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
m forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"tokenizer" forall a. Parser (Maybe a) -> a -> Parser a
.!= forall k a. Map k a
M.empty
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
m forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"filter" forall a. Parser (Maybe a) -> a -> Parser a
.!= forall k a. Map k a
M.empty
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
m forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"char_filter" forall a. Parser (Maybe a) -> a -> Parser a
.!= forall k a. Map k a
M.empty

newtype Tokenizer
  = Tokenizer Text
  deriving (Tokenizer -> Tokenizer -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Tokenizer -> Tokenizer -> Bool
$c/= :: Tokenizer -> Tokenizer -> Bool
== :: Tokenizer -> Tokenizer -> Bool
$c== :: Tokenizer -> Tokenizer -> Bool
Eq, Int -> Tokenizer -> ShowS
[Tokenizer] -> ShowS
Tokenizer -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Tokenizer] -> ShowS
$cshowList :: [Tokenizer] -> ShowS
show :: Tokenizer -> String
$cshow :: Tokenizer -> String
showsPrec :: Int -> Tokenizer -> ShowS
$cshowsPrec :: Int -> Tokenizer -> ShowS
Show, forall x. Rep Tokenizer x -> Tokenizer
forall x. Tokenizer -> Rep Tokenizer x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Tokenizer x -> Tokenizer
$cfrom :: forall x. Tokenizer -> Rep Tokenizer x
Generic, [Tokenizer] -> Encoding
[Tokenizer] -> Value
Tokenizer -> Encoding
Tokenizer -> Value
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [Tokenizer] -> Encoding
$ctoEncodingList :: [Tokenizer] -> Encoding
toJSONList :: [Tokenizer] -> Value
$ctoJSONList :: [Tokenizer] -> Value
toEncoding :: Tokenizer -> Encoding
$ctoEncoding :: Tokenizer -> Encoding
toJSON :: Tokenizer -> Value
$ctoJSON :: Tokenizer -> Value
ToJSON, Value -> Parser [Tokenizer]
Value -> Parser Tokenizer
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [Tokenizer]
$cparseJSONList :: Value -> Parser [Tokenizer]
parseJSON :: Value -> Parser Tokenizer
$cparseJSON :: Value -> Parser Tokenizer
FromJSON)

data AnalyzerDefinition = AnalyzerDefinition
  { AnalyzerDefinition -> Maybe Tokenizer
analyzerDefinitionTokenizer :: Maybe Tokenizer,
    AnalyzerDefinition -> [TokenFilter]
analyzerDefinitionFilter :: [TokenFilter],
    AnalyzerDefinition -> [CharFilter]
analyzerDefinitionCharFilter :: [CharFilter]
  }
  deriving (AnalyzerDefinition -> AnalyzerDefinition -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: AnalyzerDefinition -> AnalyzerDefinition -> Bool
$c/= :: AnalyzerDefinition -> AnalyzerDefinition -> Bool
== :: AnalyzerDefinition -> AnalyzerDefinition -> Bool
$c== :: AnalyzerDefinition -> AnalyzerDefinition -> Bool
Eq, Int -> AnalyzerDefinition -> ShowS
[AnalyzerDefinition] -> ShowS
AnalyzerDefinition -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [AnalyzerDefinition] -> ShowS
$cshowList :: [AnalyzerDefinition] -> ShowS
show :: AnalyzerDefinition -> String
$cshow :: AnalyzerDefinition -> String
showsPrec :: Int -> AnalyzerDefinition -> ShowS
$cshowsPrec :: Int -> AnalyzerDefinition -> ShowS
Show, forall x. Rep AnalyzerDefinition x -> AnalyzerDefinition
forall x. AnalyzerDefinition -> Rep AnalyzerDefinition x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep AnalyzerDefinition x -> AnalyzerDefinition
$cfrom :: forall x. AnalyzerDefinition -> Rep AnalyzerDefinition x
Generic)

instance ToJSON AnalyzerDefinition where
  toJSON :: AnalyzerDefinition -> Value
toJSON (AnalyzerDefinition Maybe Tokenizer
tokenizer [TokenFilter]
tokenFilter [CharFilter]
charFilter) =
    [Pair] -> Value
object forall a b. (a -> b) -> a -> b
$
      forall a. [Maybe a] -> [a]
catMaybes
        [ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Key
"tokenizer" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.=) Maybe Tokenizer
tokenizer,
          forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Key
"filter" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= [TokenFilter]
tokenFilter,
          forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Key
"char_filter" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= [CharFilter]
charFilter
        ]

instance FromJSON AnalyzerDefinition where
  parseJSON :: Value -> Parser AnalyzerDefinition
parseJSON = forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"AnalyzerDefinition" forall a b. (a -> b) -> a -> b
$ \Object
m ->
    Maybe Tokenizer
-> [TokenFilter] -> [CharFilter] -> AnalyzerDefinition
AnalyzerDefinition
      forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
m forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"tokenizer"
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
m forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"filter" forall a. Parser (Maybe a) -> a -> Parser a
.!= []
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
m forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"char_filter" forall a. Parser (Maybe a) -> a -> Parser a
.!= []

-- | Character filters are used to preprocess the stream of characters
--   before it is passed to the tokenizer.
data CharFilterDefinition
  = CharFilterDefinitionMapping (M.Map Text Text)
  | CharFilterDefinitionPatternReplace
      { CharFilterDefinition -> Text
charFilterDefinitionPatternReplacePattern :: Text,
        CharFilterDefinition -> Text
charFilterDefinitionPatternReplaceReplacement :: Text,
        CharFilterDefinition -> Maybe Text
charFilterDefinitionPatternReplaceFlags :: Maybe Text
      }
  deriving (CharFilterDefinition -> CharFilterDefinition -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CharFilterDefinition -> CharFilterDefinition -> Bool
$c/= :: CharFilterDefinition -> CharFilterDefinition -> Bool
== :: CharFilterDefinition -> CharFilterDefinition -> Bool
$c== :: CharFilterDefinition -> CharFilterDefinition -> Bool
Eq, Int -> CharFilterDefinition -> ShowS
[CharFilterDefinition] -> ShowS
CharFilterDefinition -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CharFilterDefinition] -> ShowS
$cshowList :: [CharFilterDefinition] -> ShowS
show :: CharFilterDefinition -> String
$cshow :: CharFilterDefinition -> String
showsPrec :: Int -> CharFilterDefinition -> ShowS
$cshowsPrec :: Int -> CharFilterDefinition -> ShowS
Show)

instance ToJSON CharFilterDefinition where
  toJSON :: CharFilterDefinition -> Value
toJSON (CharFilterDefinitionMapping Map Text Text
ms) =
    [Pair] -> Value
object
      [ Key
"type" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= (Text
"mapping" :: Text),
        Key
"mappings" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= [Text
a forall a. Semigroup a => a -> a -> a
<> Text
" => " forall a. Semigroup a => a -> a -> a
<> Text
b | (Text
a, Text
b) <- forall k a. Map k a -> [(k, a)]
M.toList Map Text Text
ms]
      ]
  toJSON (CharFilterDefinitionPatternReplace Text
pat Text
repl Maybe Text
flags) =
    [Pair] -> Value
object forall a b. (a -> b) -> a -> b
$
      [ Key
"type" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= (Text
"pattern_replace" :: Text),
        Key
"pattern" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Text
pat,
        Key
"replacement" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Text
repl
      ]
        forall a. [a] -> [a] -> [a]
++ forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] (\Text
f -> [Key
"flags" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Text
f]) Maybe Text
flags

instance FromJSON CharFilterDefinition where
  parseJSON :: Value -> Parser CharFilterDefinition
parseJSON = forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"CharFilterDefinition" forall a b. (a -> b) -> a -> b
$ \Object
m -> do
    Text
t <- Object
m forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"type"
    case (Text
t :: Text) of
      Text
"mapping" -> Map Text Text -> CharFilterDefinition
CharFilterDefinitionMapping forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k a. Ord k => [(k, a)] -> Map k a
M.fromList forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [(Text, Text)]
ms
        where
          ms :: Parser [(Text, Text)]
ms = Object
m forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"mappings" forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM forall {f :: * -> *}. MonadFail f => Text -> f (Text, Text)
parseMapping
          parseMapping :: Text -> f (Text, Text)
parseMapping Text
kv = case Text -> Text -> [Text]
T.splitOn Text
"=>" Text
kv of
            (Text
k : [Text]
vs) -> forall (f :: * -> *) a. Applicative f => a -> f a
pure (Text -> Text
T.strip Text
k, Text -> Text
T.strip forall a b. (a -> b) -> a -> b
$ [Text] -> Text
T.concat [Text]
vs)
            [Text]
_ -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"mapping is not of the format key => value"
      Text
"pattern_replace" ->
        Text -> Text -> Maybe Text -> CharFilterDefinition
CharFilterDefinitionPatternReplace
          forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
m forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"pattern"
          forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
m forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"replacement"
          forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
m forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"flags"
      Text
_ -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String
"unrecognized character filter type: " forall a. [a] -> [a] -> [a]
++ Text -> String
T.unpack Text
t)

data TokenizerDefinition
  = TokenizerDefinitionNgram Ngram
  | TokenizerDefinitionEdgeNgram Ngram
  deriving (TokenizerDefinition -> TokenizerDefinition -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TokenizerDefinition -> TokenizerDefinition -> Bool
$c/= :: TokenizerDefinition -> TokenizerDefinition -> Bool
== :: TokenizerDefinition -> TokenizerDefinition -> Bool
$c== :: TokenizerDefinition -> TokenizerDefinition -> Bool
Eq, Int -> TokenizerDefinition -> ShowS
[TokenizerDefinition] -> ShowS
TokenizerDefinition -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TokenizerDefinition] -> ShowS
$cshowList :: [TokenizerDefinition] -> ShowS
show :: TokenizerDefinition -> String
$cshow :: TokenizerDefinition -> String
showsPrec :: Int -> TokenizerDefinition -> ShowS
$cshowsPrec :: Int -> TokenizerDefinition -> ShowS
Show, forall x. Rep TokenizerDefinition x -> TokenizerDefinition
forall x. TokenizerDefinition -> Rep TokenizerDefinition x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep TokenizerDefinition x -> TokenizerDefinition
$cfrom :: forall x. TokenizerDefinition -> Rep TokenizerDefinition x
Generic)

instance ToJSON TokenizerDefinition where
  toJSON :: TokenizerDefinition -> Value
toJSON TokenizerDefinition
x =
    case TokenizerDefinition
x of
      TokenizerDefinitionNgram Ngram
ngram ->
        [Pair] -> Value
object ([Key
"type" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= (Text
"ngram" :: Text)] forall a. Semigroup a => a -> a -> a
<> forall {a}. KeyValue a => Ngram -> [a]
ngramToObject Ngram
ngram)
      TokenizerDefinitionEdgeNgram Ngram
ngram ->
        [Pair] -> Value
object ([Key
"type" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= (Text
"edge_ngram" :: Text)] forall a. Semigroup a => a -> a -> a
<> forall {a}. KeyValue a => Ngram -> [a]
ngramToObject Ngram
ngram)
    where
      ngramToObject :: Ngram -> [a]
ngramToObject (Ngram Int
minGram Int
maxGram [TokenChar]
tokenChars) =
        [ Key
"min_gram" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Int
minGram,
          Key
"max_gram" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Int
maxGram,
          Key
"token_chars" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= [TokenChar]
tokenChars
        ]

instance FromJSON TokenizerDefinition where
  parseJSON :: Value -> Parser TokenizerDefinition
parseJSON = forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"TokenizerDefinition" forall a b. (a -> b) -> a -> b
$ \Object
m -> do
    Text
typ <- Object
m forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"type" :: Parser Text
    case Text
typ of
      Text
"ngram" ->
        Ngram -> TokenizerDefinition
TokenizerDefinitionNgram forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object -> Parser Ngram
parseNgram Object
m
      Text
"edge_ngram" ->
        Ngram -> TokenizerDefinition
TokenizerDefinitionEdgeNgram forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object -> Parser Ngram
parseNgram Object
m
      Text
_ -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail forall a b. (a -> b) -> a -> b
$ String
"invalid TokenizerDefinition type: " forall a. Semigroup a => a -> a -> a
<> Text -> String
T.unpack Text
typ
    where
      parseNgram :: Object -> Parser Ngram
parseNgram Object
m =
        Int -> Int -> [TokenChar] -> Ngram
Ngram
          forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap StringlyTypedInt -> Int
unStringlyTypedInt (Object
m forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"min_gram")
          forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap StringlyTypedInt -> Int
unStringlyTypedInt (Object
m forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"max_gram")
          forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
m forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"token_chars"

data Ngram = Ngram
  { Ngram -> Int
ngramMinGram :: Int,
    Ngram -> Int
ngramMaxGram :: Int,
    Ngram -> [TokenChar]
ngramTokenChars :: [TokenChar]
  }
  deriving (Ngram -> Ngram -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Ngram -> Ngram -> Bool
$c/= :: Ngram -> Ngram -> Bool
== :: Ngram -> Ngram -> Bool
$c== :: Ngram -> Ngram -> Bool
Eq, Int -> Ngram -> ShowS
[Ngram] -> ShowS
Ngram -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Ngram] -> ShowS
$cshowList :: [Ngram] -> ShowS
show :: Ngram -> String
$cshow :: Ngram -> String
showsPrec :: Int -> Ngram -> ShowS
$cshowsPrec :: Int -> Ngram -> ShowS
Show, forall x. Rep Ngram x -> Ngram
forall x. Ngram -> Rep Ngram x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Ngram x -> Ngram
$cfrom :: forall x. Ngram -> Rep Ngram x
Generic)

data TokenChar
  = TokenLetter
  | TokenDigit
  | TokenWhitespace
  | TokenPunctuation
  | TokenSymbol
  deriving (TokenChar -> TokenChar -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TokenChar -> TokenChar -> Bool
$c/= :: TokenChar -> TokenChar -> Bool
== :: TokenChar -> TokenChar -> Bool
$c== :: TokenChar -> TokenChar -> Bool
Eq, Int -> TokenChar -> ShowS
[TokenChar] -> ShowS
TokenChar -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TokenChar] -> ShowS
$cshowList :: [TokenChar] -> ShowS
show :: TokenChar -> String
$cshow :: TokenChar -> String
showsPrec :: Int -> TokenChar -> ShowS
$cshowsPrec :: Int -> TokenChar -> ShowS
Show, forall x. Rep TokenChar x -> TokenChar
forall x. TokenChar -> Rep TokenChar x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep TokenChar x -> TokenChar
$cfrom :: forall x. TokenChar -> Rep TokenChar x
Generic)

instance ToJSON TokenChar where
  toJSON :: TokenChar -> Value
toJSON TokenChar
t = Text -> Value
String forall a b. (a -> b) -> a -> b
$ case TokenChar
t of
    TokenChar
TokenLetter -> Text
"letter"
    TokenChar
TokenDigit -> Text
"digit"
    TokenChar
TokenWhitespace -> Text
"whitespace"
    TokenChar
TokenPunctuation -> Text
"punctuation"
    TokenChar
TokenSymbol -> Text
"symbol"

instance FromJSON TokenChar where
  parseJSON :: Value -> Parser TokenChar
parseJSON = forall a. String -> (Text -> Parser a) -> Value -> Parser a
withText String
"TokenChar" forall a b. (a -> b) -> a -> b
$ \Text
t -> case Text
t of
    Text
"letter" -> forall (m :: * -> *) a. Monad m => a -> m a
return TokenChar
TokenLetter
    Text
"digit" -> forall (m :: * -> *) a. Monad m => a -> m a
return TokenChar
TokenDigit
    Text
"whitespace" -> forall (m :: * -> *) a. Monad m => a -> m a
return TokenChar
TokenWhitespace
    Text
"punctuation" -> forall (m :: * -> *) a. Monad m => a -> m a
return TokenChar
TokenPunctuation
    Text
"symbol" -> forall (m :: * -> *) a. Monad m => a -> m a
return TokenChar
TokenSymbol
    Text
_ -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"invalid TokenChar"

-- | Token filters are used to create custom analyzers.
data TokenFilterDefinition
  = TokenFilterDefinitionLowercase (Maybe Language)
  | TokenFilterDefinitionUppercase (Maybe Language)
  | TokenFilterDefinitionApostrophe
  | TokenFilterDefinitionReverse
  | TokenFilterDefinitionSnowball Language
  | TokenFilterDefinitionShingle Shingle
  | TokenFilterDefinitionStemmer Language
  | TokenFilterDefinitionStop (Either Language [StopWord])
  | TokenFilterDefinitionEdgeNgram NgramFilter (Maybe EdgeNgramFilterSide)
  | TokenFilterDefinitionNgram NgramFilter
  | TokenFilterTruncate Int
  deriving (TokenFilterDefinition -> TokenFilterDefinition -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TokenFilterDefinition -> TokenFilterDefinition -> Bool
$c/= :: TokenFilterDefinition -> TokenFilterDefinition -> Bool
== :: TokenFilterDefinition -> TokenFilterDefinition -> Bool
$c== :: TokenFilterDefinition -> TokenFilterDefinition -> Bool
Eq, Int -> TokenFilterDefinition -> ShowS
[TokenFilterDefinition] -> ShowS
TokenFilterDefinition -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TokenFilterDefinition] -> ShowS
$cshowList :: [TokenFilterDefinition] -> ShowS
show :: TokenFilterDefinition -> String
$cshow :: TokenFilterDefinition -> String
showsPrec :: Int -> TokenFilterDefinition -> ShowS
$cshowsPrec :: Int -> TokenFilterDefinition -> ShowS
Show, forall x. Rep TokenFilterDefinition x -> TokenFilterDefinition
forall x. TokenFilterDefinition -> Rep TokenFilterDefinition x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep TokenFilterDefinition x -> TokenFilterDefinition
$cfrom :: forall x. TokenFilterDefinition -> Rep TokenFilterDefinition x
Generic)

instance ToJSON TokenFilterDefinition where
  toJSON :: TokenFilterDefinition -> Value
toJSON TokenFilterDefinition
x = case TokenFilterDefinition
x of
    TokenFilterDefinitionLowercase Maybe Language
mlang ->
      [Pair] -> Value
object forall a b. (a -> b) -> a -> b
$
        forall a. [Maybe a] -> [a]
catMaybes
          [ forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Key
"type" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= (Text
"lowercase" :: Text),
            forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\Language
lang -> Key
"language" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Language -> Text
languageToText Language
lang) Maybe Language
mlang
          ]
    TokenFilterDefinitionUppercase Maybe Language
mlang ->
      [Pair] -> Value
object forall a b. (a -> b) -> a -> b
$
        forall a. [Maybe a] -> [a]
catMaybes
          [ forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Key
"type" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= (Text
"uppercase" :: Text),
            forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\Language
lang -> Key
"language" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Language -> Text
languageToText Language
lang) Maybe Language
mlang
          ]
    TokenFilterDefinition
TokenFilterDefinitionApostrophe ->
      [Pair] -> Value
object
        [ Key
"type" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= (Text
"apostrophe" :: Text)
        ]
    TokenFilterDefinition
TokenFilterDefinitionReverse ->
      [Pair] -> Value
object
        [ Key
"type" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= (Text
"reverse" :: Text)
        ]
    TokenFilterDefinitionSnowball Language
lang ->
      [Pair] -> Value
object
        [ Key
"type" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= (Text
"snowball" :: Text),
          Key
"language" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Language -> Text
languageToText Language
lang
        ]
    TokenFilterDefinitionShingle Shingle
s ->
      [Pair] -> Value
object
        [ Key
"type" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= (Text
"shingle" :: Text),
          Key
"max_shingle_size" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Shingle -> Int
shingleMaxSize Shingle
s,
          Key
"min_shingle_size" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Shingle -> Int
shingleMinSize Shingle
s,
          Key
"output_unigrams" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Shingle -> Bool
shingleOutputUnigrams Shingle
s,
          Key
"output_unigrams_if_no_shingles" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Shingle -> Bool
shingleOutputUnigramsIfNoShingles Shingle
s,
          Key
"token_separator" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Shingle -> Text
shingleTokenSeparator Shingle
s,
          Key
"filler_token" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Shingle -> Text
shingleFillerToken Shingle
s
        ]
    TokenFilterDefinitionStemmer Language
lang ->
      [Pair] -> Value
object
        [ Key
"type" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= (Text
"stemmer" :: Text),
          Key
"language" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Language -> Text
languageToText Language
lang
        ]
    TokenFilterDefinitionStop Either Language [StopWord]
stop ->
      [Pair] -> Value
object
        [ Key
"type" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= (Text
"stop" :: Text),
          Key
"stopwords" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= case Either Language [StopWord]
stop of
            Left Language
lang -> Text -> Value
String forall a b. (a -> b) -> a -> b
$ Text
"_" forall a. Semigroup a => a -> a -> a
<> Language -> Text
languageToText Language
lang forall a. Semigroup a => a -> a -> a
<> Text
"_"
            Right [StopWord]
stops -> forall a. ToJSON a => a -> Value
toJSON [StopWord]
stops
        ]
    TokenFilterDefinitionEdgeNgram NgramFilter
ngram Maybe EdgeNgramFilterSide
side ->
      [Pair] -> Value
object
        ( [ Key
"type" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= (Text
"edge_ngram" :: Text),
            Key
"side" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Maybe EdgeNgramFilterSide
side
          ]
            forall a. Semigroup a => a -> a -> a
<> NgramFilter -> [Pair]
ngramFilterToPairs NgramFilter
ngram
        )
    TokenFilterDefinitionNgram NgramFilter
ngram ->
      [Pair] -> Value
object
        ( [Key
"type" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= (Text
"ngram" :: Text)]
            forall a. Semigroup a => a -> a -> a
<> NgramFilter -> [Pair]
ngramFilterToPairs NgramFilter
ngram
        )
    TokenFilterTruncate Int
n ->
      [Pair] -> Value
object
        [ Key
"type" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= (Text
"truncate" :: Text),
          Key
"length" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Int
n
        ]

instance FromJSON TokenFilterDefinition where
  parseJSON :: Value -> Parser TokenFilterDefinition
parseJSON = forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"TokenFilterDefinition" forall a b. (a -> b) -> a -> b
$ \Object
m -> do
    Text
t <- Object
m forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"type"
    case (Text
t :: Text) of
      Text
"reverse" -> forall (m :: * -> *) a. Monad m => a -> m a
return TokenFilterDefinition
TokenFilterDefinitionReverse
      Text
"apostrophe" -> forall (m :: * -> *) a. Monad m => a -> m a
return TokenFilterDefinition
TokenFilterDefinitionApostrophe
      Text
"lowercase" ->
        Maybe Language -> TokenFilterDefinition
TokenFilterDefinitionLowercase
          forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
m forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"language"
      Text
"uppercase" ->
        Maybe Language -> TokenFilterDefinition
TokenFilterDefinitionUppercase
          forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
m forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"language"
      Text
"snowball" ->
        Language -> TokenFilterDefinition
TokenFilterDefinitionSnowball
          forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
m forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"language"
      Text
"shingle" ->
        forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Shingle -> TokenFilterDefinition
TokenFilterDefinitionShingle forall a b. (a -> b) -> a -> b
$
          Int -> Int -> Bool -> Bool -> Text -> Text -> Shingle
Shingle
            forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap) StringlyTypedInt -> Int
unStringlyTypedInt (Object
m forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"max_shingle_size") forall a. Parser (Maybe a) -> a -> Parser a
.!= Int
2
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap) StringlyTypedInt -> Int
unStringlyTypedInt (Object
m forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"min_shingle_size") forall a. Parser (Maybe a) -> a -> Parser a
.!= Int
2
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap) StringlyTypedBool -> Bool
unStringlyTypedBool (Object
m forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"output_unigrams") forall a. Parser (Maybe a) -> a -> Parser a
.!= Bool
True
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap) StringlyTypedBool -> Bool
unStringlyTypedBool (Object
m forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"output_unigrams_if_no_shingles") forall a. Parser (Maybe a) -> a -> Parser a
.!= Bool
False
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
m forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"token_separator" forall a. Parser (Maybe a) -> a -> Parser a
.!= Text
" "
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
m forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"filler_token" forall a. Parser (Maybe a) -> a -> Parser a
.!= Text
"_"
      Text
"stemmer" ->
        Language -> TokenFilterDefinition
TokenFilterDefinitionStemmer
          forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
m forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"language"
      Text
"stop" -> do
        Value
stop <- Object
m forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"stopwords"
        Either Language [StopWord]
stop' <- case Value
stop of
          String Text
lang ->
            forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. a -> Either a b
Left
              forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. FromJSON a => Value -> Parser a
parseJSON
              forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Value
String
              forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Text -> Text
T.drop Int
1
              forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Text -> Text
T.dropEnd Int
1
              forall a b. (a -> b) -> a -> b
$ Text
lang
          Value
_ -> forall a b. b -> Either a b
Right forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. FromJSON a => Value -> Parser a
parseJSON Value
stop
        forall (m :: * -> *) a. Monad m => a -> m a
return (Either Language [StopWord] -> TokenFilterDefinition
TokenFilterDefinitionStop Either Language [StopWord]
stop')
      Text
"edge_ngram" ->
        NgramFilter -> Maybe EdgeNgramFilterSide -> TokenFilterDefinition
TokenFilterDefinitionEdgeNgram
          forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object -> Parser NgramFilter
ngramFilterFromJSONObject Object
m
          forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
m forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"side"
      Text
"ngram" -> NgramFilter -> TokenFilterDefinition
TokenFilterDefinitionNgram forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object -> Parser NgramFilter
ngramFilterFromJSONObject Object
m
      Text
"truncate" -> Int -> TokenFilterDefinition
TokenFilterTruncate forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
m forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"length" forall a. Parser (Maybe a) -> a -> Parser a
.!= Int
10
      Text
_ -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String
"unrecognized token filter type: " forall a. [a] -> [a] -> [a]
++ Text -> String
T.unpack Text
t)

data NgramFilter = NgramFilter
  { NgramFilter -> Int
ngramFilterMinGram :: Int,
    NgramFilter -> Int
ngramFilterMaxGram :: Int
  }
  deriving (NgramFilter -> NgramFilter -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: NgramFilter -> NgramFilter -> Bool
$c/= :: NgramFilter -> NgramFilter -> Bool
== :: NgramFilter -> NgramFilter -> Bool
$c== :: NgramFilter -> NgramFilter -> Bool
Eq, Int -> NgramFilter -> ShowS
[NgramFilter] -> ShowS
NgramFilter -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [NgramFilter] -> ShowS
$cshowList :: [NgramFilter] -> ShowS
show :: NgramFilter -> String
$cshow :: NgramFilter -> String
showsPrec :: Int -> NgramFilter -> ShowS
$cshowsPrec :: Int -> NgramFilter -> ShowS
Show, forall x. Rep NgramFilter x -> NgramFilter
forall x. NgramFilter -> Rep NgramFilter x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep NgramFilter x -> NgramFilter
$cfrom :: forall x. NgramFilter -> Rep NgramFilter x
Generic)

ngramFilterToPairs :: NgramFilter -> [Pair]
ngramFilterToPairs :: NgramFilter -> [Pair]
ngramFilterToPairs (NgramFilter Int
minGram Int
maxGram) =
  [Key
"min_gram" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Int
minGram, Key
"max_gram" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Int
maxGram]

ngramFilterFromJSONObject :: Object -> Parser NgramFilter
ngramFilterFromJSONObject :: Object -> Parser NgramFilter
ngramFilterFromJSONObject Object
o =
  Int -> Int -> NgramFilter
NgramFilter
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"min_gram" forall a. Parser (Maybe a) -> a -> Parser a
.!= Int
1
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"max_gram" forall a. Parser (Maybe a) -> a -> Parser a
.!= Int
2

data EdgeNgramFilterSide
  = EdgeNgramFilterSideFront
  | EdgeNgramFilterSideBack
  deriving (EdgeNgramFilterSide -> EdgeNgramFilterSide -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: EdgeNgramFilterSide -> EdgeNgramFilterSide -> Bool
$c/= :: EdgeNgramFilterSide -> EdgeNgramFilterSide -> Bool
== :: EdgeNgramFilterSide -> EdgeNgramFilterSide -> Bool
$c== :: EdgeNgramFilterSide -> EdgeNgramFilterSide -> Bool
Eq, Int -> EdgeNgramFilterSide -> ShowS
[EdgeNgramFilterSide] -> ShowS
EdgeNgramFilterSide -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [EdgeNgramFilterSide] -> ShowS
$cshowList :: [EdgeNgramFilterSide] -> ShowS
show :: EdgeNgramFilterSide -> String
$cshow :: EdgeNgramFilterSide -> String
showsPrec :: Int -> EdgeNgramFilterSide -> ShowS
$cshowsPrec :: Int -> EdgeNgramFilterSide -> ShowS
Show, forall x. Rep EdgeNgramFilterSide x -> EdgeNgramFilterSide
forall x. EdgeNgramFilterSide -> Rep EdgeNgramFilterSide x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep EdgeNgramFilterSide x -> EdgeNgramFilterSide
$cfrom :: forall x. EdgeNgramFilterSide -> Rep EdgeNgramFilterSide x
Generic)

instance ToJSON EdgeNgramFilterSide where
  toJSON :: EdgeNgramFilterSide -> Value
toJSON EdgeNgramFilterSide
EdgeNgramFilterSideFront = Text -> Value
String Text
"front"
  toJSON EdgeNgramFilterSide
EdgeNgramFilterSideBack = Text -> Value
String Text
"back"

instance FromJSON EdgeNgramFilterSide where
  parseJSON :: Value -> Parser EdgeNgramFilterSide
parseJSON = forall a. String -> (Text -> Parser a) -> Value -> Parser a
withText String
"EdgeNgramFilterSide" forall a b. (a -> b) -> a -> b
$ \Text
t ->
    case Text
t of
      Text
"front" -> forall (f :: * -> *) a. Applicative f => a -> f a
pure EdgeNgramFilterSide
EdgeNgramFilterSideFront
      Text
"back" -> forall (f :: * -> *) a. Applicative f => a -> f a
pure EdgeNgramFilterSide
EdgeNgramFilterSideBack
      Text
_ -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail forall a b. (a -> b) -> a -> b
$ String
"EdgeNgramFilterSide can only be 'front' or 'back', found: " forall a. Semigroup a => a -> a -> a
<> Text -> String
T.unpack Text
t

-- | The set of languages that can be passed to various analyzers,
--   filters, etc. in Elasticsearch. Most data types in this module
--   that have a 'Language' field are actually only actually to
--   handle a subset of these languages. Consult the official
--   Elasticsearch documentation to see what is actually supported.
data Language
  = Arabic
  | Armenian
  | Basque
  | Bengali
  | Brazilian
  | Bulgarian
  | Catalan
  | Cjk
  | Czech
  | Danish
  | Dutch
  | English
  | Finnish
  | French
  | Galician
  | German
  | German2
  | Greek
  | Hindi
  | Hungarian
  | Indonesian
  | Irish
  | Italian
  | Kp
  | Latvian
  | Lithuanian
  | Lovins
  | Norwegian
  | Persian
  | Porter
  | Portuguese
  | Romanian
  | Russian
  | Sorani
  | Spanish
  | Swedish
  | Thai
  | Turkish
  deriving (Language -> Language -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Language -> Language -> Bool
$c/= :: Language -> Language -> Bool
== :: Language -> Language -> Bool
$c== :: Language -> Language -> Bool
Eq, Int -> Language -> ShowS
[Language] -> ShowS
Language -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Language] -> ShowS
$cshowList :: [Language] -> ShowS
show :: Language -> String
$cshow :: Language -> String
showsPrec :: Int -> Language -> ShowS
$cshowsPrec :: Int -> Language -> ShowS
Show, forall x. Rep Language x -> Language
forall x. Language -> Rep Language x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Language x -> Language
$cfrom :: forall x. Language -> Rep Language x
Generic)

instance ToJSON Language where
  toJSON :: Language -> Value
toJSON = Text -> Value
String forall b c a. (b -> c) -> (a -> b) -> a -> c
. Language -> Text
languageToText

instance FromJSON Language where
  parseJSON :: Value -> Parser Language
parseJSON = forall a. String -> (Text -> Parser a) -> Value -> Parser a
withText String
"Language" forall a b. (a -> b) -> a -> b
$ \Text
t -> case Text -> Maybe Language
languageFromText Text
t of
    Maybe Language
Nothing -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"not a supported Elasticsearch language"
    Just Language
lang -> forall (m :: * -> *) a. Monad m => a -> m a
return Language
lang

languageToText :: Language -> Text
languageToText :: Language -> Text
languageToText Language
x = case Language
x of
  Language
Arabic -> Text
"arabic"
  Language
Armenian -> Text
"armenian"
  Language
Basque -> Text
"basque"
  Language
Bengali -> Text
"bengali"
  Language
Brazilian -> Text
"brazilian"
  Language
Bulgarian -> Text
"bulgarian"
  Language
Catalan -> Text
"catalan"
  Language
Cjk -> Text
"cjk"
  Language
Czech -> Text
"czech"
  Language
Danish -> Text
"danish"
  Language
Dutch -> Text
"dutch"
  Language
English -> Text
"english"
  Language
Finnish -> Text
"finnish"
  Language
French -> Text
"french"
  Language
Galician -> Text
"galician"
  Language
German -> Text
"german"
  Language
German2 -> Text
"german2"
  Language
Greek -> Text
"greek"
  Language
Hindi -> Text
"hindi"
  Language
Hungarian -> Text
"hungarian"
  Language
Indonesian -> Text
"indonesian"
  Language
Irish -> Text
"irish"
  Language
Italian -> Text
"italian"
  Language
Kp -> Text
"kp"
  Language
Latvian -> Text
"latvian"
  Language
Lithuanian -> Text
"lithuanian"
  Language
Lovins -> Text
"lovins"
  Language
Norwegian -> Text
"norwegian"
  Language
Persian -> Text
"persian"
  Language
Porter -> Text
"porter"
  Language
Portuguese -> Text
"portuguese"
  Language
Romanian -> Text
"romanian"
  Language
Russian -> Text
"russian"
  Language
Sorani -> Text
"sorani"
  Language
Spanish -> Text
"spanish"
  Language
Swedish -> Text
"swedish"
  Language
Thai -> Text
"thai"
  Language
Turkish -> Text
"turkish"

languageFromText :: Text -> Maybe Language
languageFromText :: Text -> Maybe Language
languageFromText Text
x = case Text
x of
  Text
"arabic" -> forall a. a -> Maybe a
Just Language
Arabic
  Text
"armenian" -> forall a. a -> Maybe a
Just Language
Armenian
  Text
"basque" -> forall a. a -> Maybe a
Just Language
Basque
  Text
"bengali" -> forall a. a -> Maybe a
Just Language
Bengali
  Text
"brazilian" -> forall a. a -> Maybe a
Just Language
Brazilian
  Text
"bulgarian" -> forall a. a -> Maybe a
Just Language
Bulgarian
  Text
"catalan" -> forall a. a -> Maybe a
Just Language
Catalan
  Text
"cjk" -> forall a. a -> Maybe a
Just Language
Cjk
  Text
"czech" -> forall a. a -> Maybe a
Just Language
Czech
  Text
"danish" -> forall a. a -> Maybe a
Just Language
Danish
  Text
"dutch" -> forall a. a -> Maybe a
Just Language
Dutch
  Text
"english" -> forall a. a -> Maybe a
Just Language
English
  Text
"finnish" -> forall a. a -> Maybe a
Just Language
Finnish
  Text
"french" -> forall a. a -> Maybe a
Just Language
French
  Text
"galician" -> forall a. a -> Maybe a
Just Language
Galician
  Text
"german" -> forall a. a -> Maybe a
Just Language
German
  Text
"german2" -> forall a. a -> Maybe a
Just Language
German2
  Text
"greek" -> forall a. a -> Maybe a
Just Language
Greek
  Text
"hindi" -> forall a. a -> Maybe a
Just Language
Hindi
  Text
"hungarian" -> forall a. a -> Maybe a
Just Language
Hungarian
  Text
"indonesian" -> forall a. a -> Maybe a
Just Language
Indonesian
  Text
"irish" -> forall a. a -> Maybe a
Just Language
Irish
  Text
"italian" -> forall a. a -> Maybe a
Just Language
Italian
  Text
"kp" -> forall a. a -> Maybe a
Just Language
Kp
  Text
"latvian" -> forall a. a -> Maybe a
Just Language
Latvian
  Text
"lithuanian" -> forall a. a -> Maybe a
Just Language
Lithuanian
  Text
"lovins" -> forall a. a -> Maybe a
Just Language
Lovins
  Text
"norwegian" -> forall a. a -> Maybe a
Just Language
Norwegian
  Text
"persian" -> forall a. a -> Maybe a
Just Language
Persian
  Text
"porter" -> forall a. a -> Maybe a
Just Language
Porter
  Text
"portuguese" -> forall a. a -> Maybe a
Just Language
Portuguese
  Text
"romanian" -> forall a. a -> Maybe a
Just Language
Romanian
  Text
"russian" -> forall a. a -> Maybe a
Just Language
Russian
  Text
"sorani" -> forall a. a -> Maybe a
Just Language
Sorani
  Text
"spanish" -> forall a. a -> Maybe a
Just Language
Spanish
  Text
"swedish" -> forall a. a -> Maybe a
Just Language
Swedish
  Text
"thai" -> forall a. a -> Maybe a
Just Language
Thai
  Text
"turkish" -> forall a. a -> Maybe a
Just Language
Turkish
  Text
_ -> forall a. Maybe a
Nothing

data Shingle = Shingle
  { Shingle -> Int
shingleMaxSize :: Int,
    Shingle -> Int
shingleMinSize :: Int,
    Shingle -> Bool
shingleOutputUnigrams :: Bool,
    Shingle -> Bool
shingleOutputUnigramsIfNoShingles :: Bool,
    Shingle -> Text
shingleTokenSeparator :: Text,
    Shingle -> Text
shingleFillerToken :: Text
  }
  deriving (Shingle -> Shingle -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Shingle -> Shingle -> Bool
$c/= :: Shingle -> Shingle -> Bool
== :: Shingle -> Shingle -> Bool
$c== :: Shingle -> Shingle -> Bool
Eq, Int -> Shingle -> ShowS
[Shingle] -> ShowS
Shingle -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Shingle] -> ShowS
$cshowList :: [Shingle] -> ShowS
show :: Shingle -> String
$cshow :: Shingle -> String
showsPrec :: Int -> Shingle -> ShowS
$cshowsPrec :: Int -> Shingle -> ShowS
Show, forall x. Rep Shingle x -> Shingle
forall x. Shingle -> Rep Shingle x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Shingle x -> Shingle
$cfrom :: forall x. Shingle -> Rep Shingle x
Generic)