@@ -2,23 +2,23 @@
3-------------------------------------------------------------------------------
3-------------------------------------------------------------------------------
5−-- Module : Database.Bloodhound.Types
5+-- Module : Database.Bloodhound.Client
6-- Copyright : (C) 2014 Chris Allen
6-- Copyright : (C) 2014 Chris Allen
7-- License : BSD-style (see the file LICENSE)
7-- License : BSD-style (see the file LICENSE)
8-- Maintainer : Chris Allen <cma@bitemyapp.com
8-- Maintainer : Chris Allen <cma@bitemyapp.com
9-- Stability : provisional
9-- Stability : provisional
10−-- Portability : DeriveGeneric, RecordWildCards
10+-- Portability : OverloadedStrings
12+-- Client side functions for talking to Elasticsearch servers.
12−-- Data types for describing actions and data structures performed to interact
13−-- with Elasticsearch. The two main buckets your queries against Elasticsearch
14−-- will fall into are 'Query's and 'Filter's. 'Filter's are more like
15−-- traditional database constraints and often have preferable performance
16−-- properties. 'Query's support human-written textual queries, such as fuzzy
18-------------------------------------------------------------------------------
16-------------------------------------------------------------------------------
20module Database.Bloodhound.Client
18module Database.Bloodhound.Client
20+ -- module Database.Bloodhound.Client
21− ( createIndex
21+ createIndex
22 , deleteIndex
22 , deleteIndex
23 , indexExists
23 , indexExists
24 , openIndex
24 , openIndex
@@ -68,10 +68,18 @@ import Database.Bloodhound.Types
68-- >>> let testIndex = IndexName "twitter"
68-- >>> let testIndex = IndexName "twitter"
69-- >>> let testMapping = MappingName "tweet"
69-- >>> let testMapping = MappingName "tweet"
70-- >>> let defaultIndexSettings = IndexSettings (ShardCount 3) (ReplicaCount 2)
70-- >>> let defaultIndexSettings = IndexSettings (ShardCount 3) (ReplicaCount 2)
71+-- >>> data TweetMapping = TweetMapping deriving (Eq, Show)
71-- >>> _ <- deleteIndex testServer testIndex
72-- >>> _ <- deleteIndex testServer testIndex
73+-- >>> _ <- deleteMapping testServer testIndex testMapping
75+--instance ToJSON TweetMapping where
76+-- toJSON TweetMapping =
78+-- object ["properties" .=
79+-- object ["location" .=
80+-- object ["type" .= ("geo_point" :: Text)]]]]
73−-- no trailing slashes in servers, library handles building the path.
74−-- defaultIndexSettings is exported by Database.Bloodhound as well
76mkShardCount :: Int -> Maybe ShardCount
84mkShardCount :: Int -> Maybe ShardCount
77mkShardCount n
85mkShardCount n
@@ -125,15 +133,27 @@ getStatus (Server server) = do
126-- | createIndex will create an index given a 'Server',
134-- | createIndex will create an index given a 'Server',
127-- 'IndexSettings', and an 'IndexName'
135-- 'IndexSettings', and an 'IndexName'
128−-- >>> response <- createIndex testServer defaultIndexSettings testIndex
137+-- >>> response <- createIndex testServer defaultIndexSettings (IndexName "didimakeanindex")
129-- >>> respIsTwoHunna response
138-- >>> respIsTwoHunna response
140+-- >>> indexExists testServer (IndexName "didimakeanindex")
131createIndex :: Server -> IndexSettings -> IndexName -> IO Reply
142createIndex :: Server -> IndexSettings -> IndexName -> IO Reply
132createIndex (Server server) indexSettings (IndexName indexName) =
143createIndex (Server server) indexSettings (IndexName indexName) =
133 put url body
144 put url body
134 where url = joinPath [server, indexName]
145 where url = joinPath [server, indexName]
135 body = Just $ encode indexSettings
146 body = Just $ encode indexSettings
148+-- | deleteIndex will delete an index given a 'Server',
149+-- and an 'IndexName'
151+-- >>> response <- createIndex testServer defaultIndexSettings (IndexName "didimakeanindex")
152+-- >>> response <- deleteIndex testServer (IndexName "didimakeanindex")
153+-- >>> respIsTwoHunna response
155+-- >>> indexExists testServer testIndex
137deleteIndex :: Server -> IndexName -> IO Reply
157deleteIndex :: Server -> IndexName -> IO Reply
138deleteIndex (Server server) (IndexName indexName) =
158deleteIndex (Server server) (IndexName indexName) =
139 delete $ joinPath [server, indexName]
159 delete $ joinPath [server, indexName]
@@ -152,6 +172,13 @@ indexExists (Server server) (IndexName indexName) = do
152 return exists
172 return exists
153 where url = joinPath [server, indexName]
173 where url = joinPath [server, indexName]
175+-- | 'refreshIndex' will force a refresh on an index. You must
176+-- do this if you want to read what you wrote.
178+-- >>> response <- createIndex testServer defaultIndexSettings testIndex
179+-- >>> response <- refreshIndex testServer testIndex
180+-- >>> respIsTwoHunna response
155refreshIndex :: Server -> IndexName -> IO Reply
182refreshIndex :: Server -> IndexName -> IO Reply
156refreshIndex (Server server) (IndexName indexName) =
183refreshIndex (Server server) (IndexName indexName) =
157 post url Nothing
184 post url Nothing
@@ -174,9 +201,13 @@ openIndex = openOrCloseIndexes OpenIndex
174closeIndex :: Server -> IndexName -> IO Reply
201closeIndex :: Server -> IndexName -> IO Reply
175closeIndex = openOrCloseIndexes CloseIndex
202closeIndex = openOrCloseIndexes CloseIndex
177−{-| putMapping is an HTTP PUT and has upsert semantics. Mappings are schemas
204+-- | 'putMapping' is an HTTP PUT and has upsert semantics. Mappings are schemas
178− for documents in indexes.
205+-- for documents in indexes.
207+-- >>> response <- createIndex testServer defaultIndexSettings testIndex
208+-- >>> resp <- putMapping testServer testIndex testMapping TweetMapping
209+-- >>> respIsTwoHunna resp
180putMapping :: ToJSON a => Server -> IndexName
211putMapping :: ToJSON a => Server -> IndexName
181 -> MappingName -> a -> IO Reply
212 -> MappingName -> a -> IO Reply
182putMapping (Server server) (IndexName indexName) (MappingName mappingName) mapping =
213putMapping (Server server) (IndexName indexName) (MappingName mappingName) mapping =
@@ -184,6 +215,16 @@ putMapping (Server server) (IndexName indexName) (MappingName mappingName) mappi
184 where url = joinPath [server, indexName, mappingName, "_mapping"]
215 where url = joinPath [server, indexName, mappingName, "_mapping"]
185 body = Just $ encode mapping
216 body = Just $ encode mapping
218+-- | 'deleteMapping' is an HTTP DELETE and deletes a mapping for a given index.
219+-- Mappings are schemas for documents in indexes.
221+-- >>> response <- createIndex testServer defaultIndexSettings testIndex
222+-- >>> resp <- putMapping testServer testIndex testMapping TweetMapping
223+-- >>> respIsTwoHunna resp
225+-- >>> resp <- deleteMapping testServer testIndex testMapping
226+-- >>> respIsTwoHunna resp
187deleteMapping :: Server -> IndexName -> MappingName -> IO Reply
228deleteMapping :: Server -> IndexName -> MappingName -> IO Reply
188deleteMapping (Server server) (IndexName indexName)
229deleteMapping (Server server) (IndexName indexName)
189 (MappingName mappingName) =
230 (MappingName mappingName) =