@@ -196,6 +196,8 @@ module Database.V5.Bloodhound.Types
196196 , RangeQuery(..)
197197 , RegexpQuery(..)
198198 , QueryString(..)
199+ , TemplateQueryInline(..)
200+ , TemplateQueryKeyValuePairs(..)
199201 , BooleanOperator(..)
200202 , ZeroTermsQuery(..)
201203 , CutoffFrequency(..)
@@ -1211,6 +1213,7 @@ data Query =
12111213 | QueryRegexpQuery RegexpQuery
12121214 | QueryExistsQuery FieldName
12131215 | QueryMatchNoneQuery
1216+ | QueryTemplateQueryInline TemplateQueryInline
12141217 deriving (Eq, Read, Show, Generic, Typeable)
12151218
12161219-- | As of Elastic 2.0, 'Filters' are just 'Queries' housed in a Bool Query, and
@@ -1622,6 +1625,38 @@ data DistanceRange =
16221625 DistanceRange { distanceFrom :: Distance
16231626 , distanceTo :: Distance } deriving (Eq, Read, Show, Generic, Typeable)
16241627
1628+type TemplateQueryKey = Text
1629+type TemplateQueryValue = Text
1630+
1631+newtype TemplateQueryKeyValuePairs = TemplateQueryKeyValuePairs (HM.HashMap TemplateQueryKey TemplateQueryValue)
1632+ deriving (Eq, Read, Show, Generic)
1633+
1634+instance ToJSON TemplateQueryKeyValuePairs where
1635+ toJSON (TemplateQueryKeyValuePairs x) = Object $ HM.map toJSON x
1636+
1637+instance FromJSON TemplateQueryKeyValuePairs where
1638+ parseJSON (Object o) = pure . TemplateQueryKeyValuePairs $ HM.mapMaybe getValue o
1639+ where getValue (String x) = Just x
1640+ getValue _ = Nothing
1641+ parseJSON _ = fail "error parsing TemplateQueryKeyValuePairs"
1642+
1643+data TemplateQueryInline =
1644+ TemplateQueryInline { inline :: Query
1645+ , params :: TemplateQueryKeyValuePairs
1646+ }
1647+ deriving (Eq, Read, Show, Generic, Typeable)
1648+
1649+instance ToJSON TemplateQueryInline where
1650+ toJSON TemplateQueryInline{..} = object [ "inline" .= inline
1651+ , "params" .= params
1652+ ]
1653+
1654+instance FromJSON TemplateQueryInline where
1655+ parseJSON = withObject "TemplateQueryInline" parse
1656+ where parse o = TemplateQueryInline
1657+ <$> o .: "inline"
1658+ <*> o .: "params"
1659+
16251660data SearchResult a =
16261661 SearchResult { took :: Int
16271662 , timedOut :: Bool
@@ -2220,6 +2255,9 @@ instance ToJSON Query where
22202255 toJSON QueryMatchNoneQuery =
22212256 object ["match_none" .= object []]
22222257
2258+ toJSON (QueryTemplateQueryInline templateQuery) =
2259+ object [ "template" .= templateQuery ]
2260+
22232261instance FromJSON Query where
22242262 parseJSON v = withObject "Query" parse v
22252263 where parse o = termQuery `taggedWith` "term"
@@ -2247,6 +2285,7 @@ instance FromJSON Query where
22472285 <|> queryRangeQuery `taggedWith` "range"
22482286 <|> queryRegexpQuery `taggedWith` "regexp"
22492287 <|> querySimpleQueryStringQuery `taggedWith` "simple_query_string"
2288+ <|> queryTemplateQueryInline `taggedWith` "template"
22502289 where taggedWith parser k = parser =<< o .: k
22512290 termQuery = fieldTagged $ \(FieldName fn) o ->
22522291 TermQuery <$> (Term fn <$> o .: "value") <*> o .:? "boost"
@@ -2284,6 +2323,7 @@ instance FromJSON Query where
22842323 queryRegexpQuery = pure . QueryRegexpQuery
22852324 querySimpleQueryStringQuery = pure . QuerySimpleQueryStringQuery
22862325 queryExistsQuery o = QueryExistsQuery <$> o .: "field"
2326+ queryTemplateQueryInline = pure . QueryTemplateQueryInline
22872327
22882328
22892329omitNulls :: [(Text, Value)] -> Value