{-# LANGUAGE OverloadedStrings #-}

module Database.Bloodhound.Internal.Versions.Common.Types.Query.CommonTerms
  ( CommonMinimumMatch (..),
    CommonTermsQuery (..),
    MinimumMatchHighLow (..),
  )
where

import Database.Bloodhound.Internal.Utils.Imports
import Database.Bloodhound.Internal.Versions.Common.Types.Newtypes
import Database.Bloodhound.Internal.Versions.Common.Types.Query.Commons
import GHC.Generics

data CommonTermsQuery = CommonTermsQuery
  { CommonTermsQuery -> FieldName
commonField :: FieldName,
    CommonTermsQuery -> QueryString
commonQuery :: QueryString,
    CommonTermsQuery -> CutoffFrequency
commonCutoffFrequency :: CutoffFrequency,
    CommonTermsQuery -> BooleanOperator
commonLowFreqOperator :: BooleanOperator,
    CommonTermsQuery -> BooleanOperator
commonHighFreqOperator :: BooleanOperator,
    CommonTermsQuery -> Maybe CommonMinimumMatch
commonMinimumShouldMatch :: Maybe CommonMinimumMatch,
    CommonTermsQuery -> Maybe Boost
commonBoost :: Maybe Boost,
    CommonTermsQuery -> Maybe Analyzer
commonAnalyzer :: Maybe Analyzer,
    CommonTermsQuery -> Maybe DisableCoord
commonDisableCoord :: Maybe DisableCoord
  }
  deriving stock (CommonTermsQuery -> CommonTermsQuery -> Bool
(CommonTermsQuery -> CommonTermsQuery -> Bool)
-> (CommonTermsQuery -> CommonTermsQuery -> Bool)
-> Eq CommonTermsQuery
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CommonTermsQuery -> CommonTermsQuery -> Bool
== :: CommonTermsQuery -> CommonTermsQuery -> Bool
$c/= :: CommonTermsQuery -> CommonTermsQuery -> Bool
/= :: CommonTermsQuery -> CommonTermsQuery -> Bool
Eq, Int -> CommonTermsQuery -> ShowS
[CommonTermsQuery] -> ShowS
CommonTermsQuery -> String
(Int -> CommonTermsQuery -> ShowS)
-> (CommonTermsQuery -> String)
-> ([CommonTermsQuery] -> ShowS)
-> Show CommonTermsQuery
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CommonTermsQuery -> ShowS
showsPrec :: Int -> CommonTermsQuery -> ShowS
$cshow :: CommonTermsQuery -> String
show :: CommonTermsQuery -> String
$cshowList :: [CommonTermsQuery] -> ShowS
showList :: [CommonTermsQuery] -> ShowS
Show, (forall x. CommonTermsQuery -> Rep CommonTermsQuery x)
-> (forall x. Rep CommonTermsQuery x -> CommonTermsQuery)
-> Generic CommonTermsQuery
forall x. Rep CommonTermsQuery x -> CommonTermsQuery
forall x. CommonTermsQuery -> Rep CommonTermsQuery x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. CommonTermsQuery -> Rep CommonTermsQuery x
from :: forall x. CommonTermsQuery -> Rep CommonTermsQuery x
$cto :: forall x. Rep CommonTermsQuery x -> CommonTermsQuery
to :: forall x. Rep CommonTermsQuery x -> CommonTermsQuery
Generic)

instance ToJSON CommonTermsQuery where
  toJSON :: CommonTermsQuery -> Value
toJSON
    ( CommonTermsQuery
        (FieldName Text
fieldName)
        (QueryString Text
query)
        CutoffFrequency
cf
        BooleanOperator
lfo
        BooleanOperator
hfo
        Maybe CommonMinimumMatch
msm
        Maybe Boost
boost
        Maybe Analyzer
analyzer
        Maybe DisableCoord
disableCoord
      ) =
      [Pair] -> Value
object [Text -> Key
fromText Text
fieldName Key -> Value -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= [Pair] -> Value
omitNulls [Pair]
base]
      where
        base :: [Pair]
base =
          [ Key
"query" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Text
query,
            Key
"cutoff_frequency" Key -> CutoffFrequency -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= CutoffFrequency
cf,
            Key
"low_freq_operator" Key -> BooleanOperator -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= BooleanOperator
lfo,
            Key
"minimum_should_match" Key -> Maybe CommonMinimumMatch -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Maybe CommonMinimumMatch
msm,
            Key
"boost" Key -> Maybe Boost -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Maybe Boost
boost,
            Key
"analyzer" Key -> Maybe Analyzer -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Maybe Analyzer
analyzer,
            Key
"disable_coord" Key -> Maybe DisableCoord -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Maybe DisableCoord
disableCoord,
            Key
"high_freq_operator" Key -> BooleanOperator -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= BooleanOperator
hfo
          ]

instance FromJSON CommonTermsQuery where
  parseJSON :: Value -> Parser CommonTermsQuery
parseJSON = String
-> (Object -> Parser CommonTermsQuery)
-> Value
-> Parser CommonTermsQuery
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"CommonTermsQuery" Object -> Parser CommonTermsQuery
parse
    where
      parse :: Object -> Parser CommonTermsQuery
parse = (FieldName -> Object -> Parser CommonTermsQuery)
-> Object -> Parser CommonTermsQuery
forall (m :: * -> *) a.
(Monad m, MonadFail m) =>
(FieldName -> Object -> m a) -> Object -> m a
fieldTagged ((FieldName -> Object -> Parser CommonTermsQuery)
 -> Object -> Parser CommonTermsQuery)
-> (FieldName -> Object -> Parser CommonTermsQuery)
-> Object
-> Parser CommonTermsQuery
forall a b. (a -> b) -> a -> b
$ \FieldName
fn Object
o ->
        FieldName
-> QueryString
-> CutoffFrequency
-> BooleanOperator
-> BooleanOperator
-> Maybe CommonMinimumMatch
-> Maybe Boost
-> Maybe Analyzer
-> Maybe DisableCoord
-> CommonTermsQuery
CommonTermsQuery FieldName
fn
          (QueryString
 -> CutoffFrequency
 -> BooleanOperator
 -> BooleanOperator
 -> Maybe CommonMinimumMatch
 -> Maybe Boost
 -> Maybe Analyzer
 -> Maybe DisableCoord
 -> CommonTermsQuery)
-> Parser QueryString
-> Parser
     (CutoffFrequency
      -> BooleanOperator
      -> BooleanOperator
      -> Maybe CommonMinimumMatch
      -> Maybe Boost
      -> Maybe Analyzer
      -> Maybe DisableCoord
      -> CommonTermsQuery)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser QueryString
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"query"
          Parser
  (CutoffFrequency
   -> BooleanOperator
   -> BooleanOperator
   -> Maybe CommonMinimumMatch
   -> Maybe Boost
   -> Maybe Analyzer
   -> Maybe DisableCoord
   -> CommonTermsQuery)
-> Parser CutoffFrequency
-> Parser
     (BooleanOperator
      -> BooleanOperator
      -> Maybe CommonMinimumMatch
      -> Maybe Boost
      -> Maybe Analyzer
      -> Maybe DisableCoord
      -> CommonTermsQuery)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser CutoffFrequency
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"cutoff_frequency"
          Parser
  (BooleanOperator
   -> BooleanOperator
   -> Maybe CommonMinimumMatch
   -> Maybe Boost
   -> Maybe Analyzer
   -> Maybe DisableCoord
   -> CommonTermsQuery)
-> Parser BooleanOperator
-> Parser
     (BooleanOperator
      -> Maybe CommonMinimumMatch
      -> Maybe Boost
      -> Maybe Analyzer
      -> Maybe DisableCoord
      -> CommonTermsQuery)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser BooleanOperator
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"low_freq_operator"
          Parser
  (BooleanOperator
   -> Maybe CommonMinimumMatch
   -> Maybe Boost
   -> Maybe Analyzer
   -> Maybe DisableCoord
   -> CommonTermsQuery)
-> Parser BooleanOperator
-> Parser
     (Maybe CommonMinimumMatch
      -> Maybe Boost
      -> Maybe Analyzer
      -> Maybe DisableCoord
      -> CommonTermsQuery)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser BooleanOperator
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"high_freq_operator"
          Parser
  (Maybe CommonMinimumMatch
   -> Maybe Boost
   -> Maybe Analyzer
   -> Maybe DisableCoord
   -> CommonTermsQuery)
-> Parser (Maybe CommonMinimumMatch)
-> Parser
     (Maybe Boost
      -> Maybe Analyzer -> Maybe DisableCoord -> CommonTermsQuery)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser (Maybe CommonMinimumMatch)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"minimum_should_match"
          Parser
  (Maybe Boost
   -> Maybe Analyzer -> Maybe DisableCoord -> CommonTermsQuery)
-> Parser (Maybe Boost)
-> Parser
     (Maybe Analyzer -> Maybe DisableCoord -> CommonTermsQuery)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser (Maybe Boost)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"boost"
          Parser (Maybe Analyzer -> Maybe DisableCoord -> CommonTermsQuery)
-> Parser (Maybe Analyzer)
-> Parser (Maybe DisableCoord -> CommonTermsQuery)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser (Maybe Analyzer)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"analyzer"
          Parser (Maybe DisableCoord -> CommonTermsQuery)
-> Parser (Maybe DisableCoord) -> Parser CommonTermsQuery
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser (Maybe DisableCoord)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"disable_coord"

data CommonMinimumMatch
  = CommonMinimumMatchHighLow MinimumMatchHighLow
  | CommonMinimumMatch MinimumMatch
  deriving stock (CommonMinimumMatch -> CommonMinimumMatch -> Bool
(CommonMinimumMatch -> CommonMinimumMatch -> Bool)
-> (CommonMinimumMatch -> CommonMinimumMatch -> Bool)
-> Eq CommonMinimumMatch
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CommonMinimumMatch -> CommonMinimumMatch -> Bool
== :: CommonMinimumMatch -> CommonMinimumMatch -> Bool
$c/= :: CommonMinimumMatch -> CommonMinimumMatch -> Bool
/= :: CommonMinimumMatch -> CommonMinimumMatch -> Bool
Eq, Int -> CommonMinimumMatch -> ShowS
[CommonMinimumMatch] -> ShowS
CommonMinimumMatch -> String
(Int -> CommonMinimumMatch -> ShowS)
-> (CommonMinimumMatch -> String)
-> ([CommonMinimumMatch] -> ShowS)
-> Show CommonMinimumMatch
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CommonMinimumMatch -> ShowS
showsPrec :: Int -> CommonMinimumMatch -> ShowS
$cshow :: CommonMinimumMatch -> String
show :: CommonMinimumMatch -> String
$cshowList :: [CommonMinimumMatch] -> ShowS
showList :: [CommonMinimumMatch] -> ShowS
Show, (forall x. CommonMinimumMatch -> Rep CommonMinimumMatch x)
-> (forall x. Rep CommonMinimumMatch x -> CommonMinimumMatch)
-> Generic CommonMinimumMatch
forall x. Rep CommonMinimumMatch x -> CommonMinimumMatch
forall x. CommonMinimumMatch -> Rep CommonMinimumMatch x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. CommonMinimumMatch -> Rep CommonMinimumMatch x
from :: forall x. CommonMinimumMatch -> Rep CommonMinimumMatch x
$cto :: forall x. Rep CommonMinimumMatch x -> CommonMinimumMatch
to :: forall x. Rep CommonMinimumMatch x -> CommonMinimumMatch
Generic)

instance ToJSON CommonMinimumMatch where
  toJSON :: CommonMinimumMatch -> Value
toJSON (CommonMinimumMatch MinimumMatch
mm) = MinimumMatch -> Value
forall a. ToJSON a => a -> Value
toJSON MinimumMatch
mm
  toJSON (CommonMinimumMatchHighLow (MinimumMatchHighLow MinimumMatch
lowF MinimumMatch
highF)) =
    [Pair] -> Value
object
      [ Key
"low_freq" Key -> MinimumMatch -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= MinimumMatch
lowF,
        Key
"high_freq" Key -> MinimumMatch -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= MinimumMatch
highF
      ]

instance FromJSON CommonMinimumMatch where
  parseJSON :: Value -> Parser CommonMinimumMatch
parseJSON Value
v =
    Value -> Parser CommonMinimumMatch
parseMinimum Value
v
      Parser CommonMinimumMatch
-> Parser CommonMinimumMatch -> Parser CommonMinimumMatch
forall a. Parser a -> Parser a -> Parser a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Value -> Parser CommonMinimumMatch
parseMinimumHighLow Value
v
    where
      parseMinimum :: Value -> Parser CommonMinimumMatch
parseMinimum = (MinimumMatch -> CommonMinimumMatch)
-> Parser MinimumMatch -> Parser CommonMinimumMatch
forall a b. (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap MinimumMatch -> CommonMinimumMatch
CommonMinimumMatch (Parser MinimumMatch -> Parser CommonMinimumMatch)
-> (Value -> Parser MinimumMatch)
-> Value
-> Parser CommonMinimumMatch
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Value -> Parser MinimumMatch
forall a. FromJSON a => Value -> Parser a
parseJSON
      parseMinimumHighLow :: Value -> Parser CommonMinimumMatch
parseMinimumHighLow =
        (MinimumMatchHighLow -> CommonMinimumMatch)
-> Parser MinimumMatchHighLow -> Parser CommonMinimumMatch
forall a b. (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap MinimumMatchHighLow -> CommonMinimumMatch
CommonMinimumMatchHighLow
          (Parser MinimumMatchHighLow -> Parser CommonMinimumMatch)
-> (Value -> Parser MinimumMatchHighLow)
-> Value
-> Parser CommonMinimumMatch
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String
-> (Object -> Parser MinimumMatchHighLow)
-> Value
-> Parser MinimumMatchHighLow
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject
            String
"CommonMinimumMatchHighLow"
            ( \Object
o ->
                MinimumMatch -> MinimumMatch -> MinimumMatchHighLow
MinimumMatchHighLow
                  (MinimumMatch -> MinimumMatch -> MinimumMatchHighLow)
-> Parser MinimumMatch
-> Parser (MinimumMatch -> MinimumMatchHighLow)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser MinimumMatch
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"low_freq"
                  Parser (MinimumMatch -> MinimumMatchHighLow)
-> Parser MinimumMatch -> Parser MinimumMatchHighLow
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser MinimumMatch
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"high_freq"
            )

data MinimumMatchHighLow = MinimumMatchHighLow
  { MinimumMatchHighLow -> MinimumMatch
lowFreq :: MinimumMatch,
    MinimumMatchHighLow -> MinimumMatch
highFreq :: MinimumMatch
  }
  deriving stock (MinimumMatchHighLow -> MinimumMatchHighLow -> Bool
(MinimumMatchHighLow -> MinimumMatchHighLow -> Bool)
-> (MinimumMatchHighLow -> MinimumMatchHighLow -> Bool)
-> Eq MinimumMatchHighLow
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: MinimumMatchHighLow -> MinimumMatchHighLow -> Bool
== :: MinimumMatchHighLow -> MinimumMatchHighLow -> Bool
$c/= :: MinimumMatchHighLow -> MinimumMatchHighLow -> Bool
/= :: MinimumMatchHighLow -> MinimumMatchHighLow -> Bool
Eq, Int -> MinimumMatchHighLow -> ShowS
[MinimumMatchHighLow] -> ShowS
MinimumMatchHighLow -> String
(Int -> MinimumMatchHighLow -> ShowS)
-> (MinimumMatchHighLow -> String)
-> ([MinimumMatchHighLow] -> ShowS)
-> Show MinimumMatchHighLow
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> MinimumMatchHighLow -> ShowS
showsPrec :: Int -> MinimumMatchHighLow -> ShowS
$cshow :: MinimumMatchHighLow -> String
show :: MinimumMatchHighLow -> String
$cshowList :: [MinimumMatchHighLow] -> ShowS
showList :: [MinimumMatchHighLow] -> ShowS
Show, (forall x. MinimumMatchHighLow -> Rep MinimumMatchHighLow x)
-> (forall x. Rep MinimumMatchHighLow x -> MinimumMatchHighLow)
-> Generic MinimumMatchHighLow
forall x. Rep MinimumMatchHighLow x -> MinimumMatchHighLow
forall x. MinimumMatchHighLow -> Rep MinimumMatchHighLow x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. MinimumMatchHighLow -> Rep MinimumMatchHighLow x
from :: forall x. MinimumMatchHighLow -> Rep MinimumMatchHighLow x
$cto :: forall x. Rep MinimumMatchHighLow x -> MinimumMatchHighLow
to :: forall x. Rep MinimumMatchHighLow x -> MinimumMatchHighLow
Generic)