{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module Database.Bloodhound.Internal.Sort where
import Bloodhound.Import
import Database.Bloodhound.Internal.Newtypes
import Database.Bloodhound.Internal.Query
data SortMode = SortMin
| SortMax
| SortSum
| SortAvg deriving (SortMode -> SortMode -> Bool
(SortMode -> SortMode -> Bool)
-> (SortMode -> SortMode -> Bool) -> Eq SortMode
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SortMode -> SortMode -> Bool
$c/= :: SortMode -> SortMode -> Bool
== :: SortMode -> SortMode -> Bool
$c== :: SortMode -> SortMode -> Bool
Eq, Int -> SortMode -> ShowS
[SortMode] -> ShowS
SortMode -> String
(Int -> SortMode -> ShowS)
-> (SortMode -> String) -> ([SortMode] -> ShowS) -> Show SortMode
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SortMode] -> ShowS
$cshowList :: [SortMode] -> ShowS
show :: SortMode -> String
$cshow :: SortMode -> String
showsPrec :: Int -> SortMode -> ShowS
$cshowsPrec :: Int -> SortMode -> ShowS
Show)
instance ToJSON SortMode where
toJSON :: SortMode -> Value
toJSON SortMode
SortMin = Text -> Value
String Text
"min"
toJSON SortMode
SortMax = Text -> Value
String Text
"max"
toJSON SortMode
SortSum = Text -> Value
String Text
"sum"
toJSON SortMode
SortAvg = Text -> Value
String Text
"avg"
mkSort :: FieldName -> SortOrder -> DefaultSort
mkSort :: FieldName -> SortOrder -> DefaultSort
mkSort FieldName
fieldName SortOrder
sOrder = FieldName
-> SortOrder
-> Maybe Text
-> Maybe SortMode
-> Maybe Missing
-> Maybe Filter
-> DefaultSort
DefaultSort FieldName
fieldName SortOrder
sOrder Maybe Text
forall a. Maybe a
Nothing Maybe SortMode
forall a. Maybe a
Nothing Maybe Missing
forall a. Maybe a
Nothing Maybe Filter
forall a. Maybe a
Nothing
type Sort = [SortSpec]
data SortSpec =
DefaultSortSpec DefaultSort
| GeoDistanceSortSpec SortOrder GeoPoint DistanceUnit
deriving (SortSpec -> SortSpec -> Bool
(SortSpec -> SortSpec -> Bool)
-> (SortSpec -> SortSpec -> Bool) -> Eq SortSpec
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SortSpec -> SortSpec -> Bool
$c/= :: SortSpec -> SortSpec -> Bool
== :: SortSpec -> SortSpec -> Bool
$c== :: SortSpec -> SortSpec -> Bool
Eq, Int -> SortSpec -> ShowS
[SortSpec] -> ShowS
SortSpec -> String
(Int -> SortSpec -> ShowS)
-> (SortSpec -> String) -> ([SortSpec] -> ShowS) -> Show SortSpec
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SortSpec] -> ShowS
$cshowList :: [SortSpec] -> ShowS
show :: SortSpec -> String
$cshow :: SortSpec -> String
showsPrec :: Int -> SortSpec -> ShowS
$cshowsPrec :: Int -> SortSpec -> ShowS
Show)
instance ToJSON SortSpec where
toJSON :: SortSpec -> Value
toJSON (DefaultSortSpec
(DefaultSort (FieldName Text
dsSortFieldName) SortOrder
dsSortOrder Maybe Text
dsIgnoreUnmapped
Maybe SortMode
dsSortMode Maybe Missing
dsMissingSort Maybe Filter
dsNestedFilter)) =
[Pair] -> Value
object [Text -> Key
fromText Text
dsSortFieldName Key -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= [Pair] -> Value
omitNulls [Pair]
base] where
base :: [Pair]
base = [ Key
"order" Key -> SortOrder -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= SortOrder
dsSortOrder
, Key
"unmapped_type" Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Maybe Text
dsIgnoreUnmapped
, Key
"mode" Key -> Maybe SortMode -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Maybe SortMode
dsSortMode
, Key
"missing" Key -> Maybe Missing -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Maybe Missing
dsMissingSort
, Key
"nested_filter" Key -> Maybe Filter -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Maybe Filter
dsNestedFilter ]
toJSON (GeoDistanceSortSpec SortOrder
gdsSortOrder (GeoPoint (FieldName Text
field) LatLon
gdsLatLon) DistanceUnit
units) =
[Pair] -> Value
object [ Key
"unit" Key -> DistanceUnit -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= DistanceUnit
units
, Text -> Key
fromText Text
field Key -> LatLon -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= LatLon
gdsLatLon
, Key
"order" Key -> SortOrder -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= SortOrder
gdsSortOrder ]
data DefaultSort =
DefaultSort { DefaultSort -> FieldName
sortFieldName :: FieldName
, DefaultSort -> SortOrder
sortOrder :: SortOrder
, DefaultSort -> Maybe Text
ignoreUnmapped :: Maybe Text
, DefaultSort -> Maybe SortMode
sortMode :: Maybe SortMode
, DefaultSort -> Maybe Missing
missingSort :: Maybe Missing
, DefaultSort -> Maybe Filter
nestedFilter :: Maybe Filter } deriving (DefaultSort -> DefaultSort -> Bool
(DefaultSort -> DefaultSort -> Bool)
-> (DefaultSort -> DefaultSort -> Bool) -> Eq DefaultSort
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: DefaultSort -> DefaultSort -> Bool
$c/= :: DefaultSort -> DefaultSort -> Bool
== :: DefaultSort -> DefaultSort -> Bool
$c== :: DefaultSort -> DefaultSort -> Bool
Eq, Int -> DefaultSort -> ShowS
[DefaultSort] -> ShowS
DefaultSort -> String
(Int -> DefaultSort -> ShowS)
-> (DefaultSort -> String)
-> ([DefaultSort] -> ShowS)
-> Show DefaultSort
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [DefaultSort] -> ShowS
$cshowList :: [DefaultSort] -> ShowS
show :: DefaultSort -> String
$cshow :: DefaultSort -> String
showsPrec :: Int -> DefaultSort -> ShowS
$cshowsPrec :: Int -> DefaultSort -> ShowS
Show)
data SortOrder = Ascending
| Descending deriving (SortOrder -> SortOrder -> Bool
(SortOrder -> SortOrder -> Bool)
-> (SortOrder -> SortOrder -> Bool) -> Eq SortOrder
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SortOrder -> SortOrder -> Bool
$c/= :: SortOrder -> SortOrder -> Bool
== :: SortOrder -> SortOrder -> Bool
$c== :: SortOrder -> SortOrder -> Bool
Eq, Int -> SortOrder -> ShowS
[SortOrder] -> ShowS
SortOrder -> String
(Int -> SortOrder -> ShowS)
-> (SortOrder -> String)
-> ([SortOrder] -> ShowS)
-> Show SortOrder
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SortOrder] -> ShowS
$cshowList :: [SortOrder] -> ShowS
show :: SortOrder -> String
$cshow :: SortOrder -> String
showsPrec :: Int -> SortOrder -> ShowS
$cshowsPrec :: Int -> SortOrder -> ShowS
Show)
instance ToJSON SortOrder where
toJSON :: SortOrder -> Value
toJSON SortOrder
Ascending = Text -> Value
String Text
"asc"
toJSON SortOrder
Descending = Text -> Value
String Text
"desc"
data Missing = LastMissing
| FirstMissing
| CustomMissing Text deriving (Missing -> Missing -> Bool
(Missing -> Missing -> Bool)
-> (Missing -> Missing -> Bool) -> Eq Missing
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Missing -> Missing -> Bool
$c/= :: Missing -> Missing -> Bool
== :: Missing -> Missing -> Bool
$c== :: Missing -> Missing -> Bool
Eq, Int -> Missing -> ShowS
[Missing] -> ShowS
Missing -> String
(Int -> Missing -> ShowS)
-> (Missing -> String) -> ([Missing] -> ShowS) -> Show Missing
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Missing] -> ShowS
$cshowList :: [Missing] -> ShowS
show :: Missing -> String
$cshow :: Missing -> String
showsPrec :: Int -> Missing -> ShowS
$cshowsPrec :: Int -> Missing -> ShowS
Show)
instance ToJSON Missing where
toJSON :: Missing -> Value
toJSON Missing
LastMissing = Text -> Value
String Text
"_last"
toJSON Missing
FirstMissing = Text -> Value
String Text
"_first"
toJSON (CustomMissing Text
txt) = Text -> Value
String Text
txt