@@ -56,6 +56,7 @@ module Database.Bloodhound.Types
56 , Existence(..)
56 , Existence(..)
57 , NullValue(..)
57 , NullValue(..)
58 , IndexSettings(..)
58 , IndexSettings(..)
59 , Server(..)
60 , Server(..)
61 , EsResult(..)
62 , EsResult(..)
@@ -101,6 +102,8 @@ module Database.Bloodhound.Types
101 , RegexpFlag(..)
102 , RegexpFlag(..)
102 , FieldName(..)
103 , FieldName(..)
103 , IndexName(..)
104 , IndexName(..)
106+ , TemplatePattern(..)
104 , MappingName(..)
107 , MappingName(..)
105 , DocId(..)
108 , DocId(..)
106 , CacheName(..)
109 , CacheName(..)
@@ -222,8 +225,9 @@ import Control.Monad.Writer
222import Data.Aeson
225import Data.Aeson
223import Data.Aeson.Types (Pair, emptyObject, parseMaybe)
226import Data.Aeson.Types (Pair, emptyObject, parseMaybe)
224import qualified Data.ByteString.Lazy.Char8 as L
227import qualified Data.ByteString.Lazy.Char8 as L
225−import Data.List (nub)
228+import Data.List (nub, foldl')
226import Data.List.NonEmpty (NonEmpty (..), toList)
229import Data.List.NonEmpty (NonEmpty (..), toList)
230+import qualified Data.HashMap.Strict as HM (union)
227import qualified Data.Map.Strict as M
231import qualified Data.Map.Strict as M
228import Data.Maybe
232import Data.Maybe
229import Data.Text (Text)
233import Data.Text (Text)
@@ -348,6 +352,20 @@ data FieldType = GeoPointType
348data FieldDefinition =
352data FieldDefinition =
349 FieldDefinition { fieldType :: FieldType } deriving (Eq, Show)
353 FieldDefinition { fieldType :: FieldType } deriving (Eq, Show)
355+{-| An 'IndexTemplate' defines a template that will automatically be
356+ applied to new indices created. The templates include both
357+ 'IndexSettings' and mappings, and a simple 'TemplatePattern' that
358+ controls if the template will be applied to the index created.
359+ Specify mappings as follows: @[toJSON TweetMapping, ...]@
361+ https://www.elastic.co/guide/en/elasticsearch/reference/1.7/indices-templates.html
364+ IndexTemplate { templatePattern :: TemplatePattern
365+ , templateSettings :: Maybe IndexSettings
366+ , templateMappings :: [Value]
351data MappingField =
369data MappingField =
352 MappingField { mappingFieldName :: FieldName
370 MappingField { mappingFieldName :: FieldName
353 , fieldDefinition :: FieldDefinition } deriving (Eq, Show)
371 , fieldDefinition :: FieldDefinition } deriving (Eq, Show)
@@ -546,6 +564,14 @@ newtype Server = Server Text deriving (Eq, Show)
547newtype IndexName = IndexName Text deriving (Eq, Generic, Show)
565newtype IndexName = IndexName Text deriving (Eq, Generic, Show)
567+{-| 'TemplateName' is used to describe which template to query/create/delete
569+newtype TemplateName = TemplateName Text deriving (Eq, Show, Generic)
571+{-| 'TemplatePattern' represents a pattern which is matched against index names
573+newtype TemplatePattern = TemplatePattern Text deriving (Eq, Show, Generic)
549{-| 'MappingName' is part of mappings which are how ES describes and schematizes
575{-| 'MappingName' is part of mappings which are how ES describes and schematizes
550 the data in the indices.
576 the data in the indices.
@@ -1953,6 +1979,8 @@ instance ToJSON IgnoreTermFrequency
1953instance ToJSON MaxQueryTerms
1979instance ToJSON MaxQueryTerms
1954instance ToJSON TypeName
1980instance ToJSON TypeName
1955instance ToJSON IndexName
1981instance ToJSON IndexName
1982+instance ToJSON TemplateName
1983+instance ToJSON TemplatePattern
1956instance ToJSON BoostTerms
1984instance ToJSON BoostTerms
1957instance ToJSON MaxWordLength
1985instance ToJSON MaxWordLength
1958instance ToJSON MinWordLength
1986instance ToJSON MinWordLength
@@ -1991,6 +2019,16 @@ instance FromJSON Status where
1991instance ToJSON IndexSettings where
2019instance ToJSON IndexSettings where
1992 toJSON (IndexSettings s r) = object ["settings" .= object ["shards" .= s, "replicas" .= r]]
2020 toJSON (IndexSettings s r) = object ["settings" .= object ["shards" .= s, "replicas" .= r]]
2022+instance ToJSON IndexTemplate where
2023+ toJSON (IndexTemplate p s m) =
2024+ objectNoNulls [ "template" .= p
2026+ , "mappings" .= foldl' merge (object []) m
2029+ objectNoNulls = object . filter (\(_, v) -> v /= Null)
2030+ merge (Object o1) (Object o2) = toJSON $ HM.union o1 o2
2031+ merge _ _ = undefined
1995instance (FromJSON a) => FromJSON (EsResult a) where
2033instance (FromJSON a) => FromJSON (EsResult a) where
1996 parseJSON (Object v) = EsResult <$>
2034 parseJSON (Object v) = EsResult <$>