@@ -2,23 +2,23 @@
22
33-------------------------------------------------------------------------------
44-- |
5−-- Module : Database.Bloodhound.Types
5+-- Module : Database.Bloodhound.Client
66-- Copyright : (C) 2014 Chris Allen
77-- License : BSD-style (see the file LICENSE)
88-- Maintainer : Chris Allen <cma@bitemyapp.com
99-- Stability : provisional
10−-- Portability : DeriveGeneric, RecordWildCards
10+-- Portability : OverloadedStrings
11+--
12+-- Client side functions for talking to Elasticsearch servers.
13+--
14+-- $setup
1115--
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
17−-- queries.
1816-------------------------------------------------------------------------------
1917
2018module Database.Bloodhound.Client
21− ( createIndex
19+ (
20+ -- module Database.Bloodhound.Client
21+ createIndex
2222 , deleteIndex
2323 , indexExists
2424 , openIndex
@@ -68,10 +68,18 @@ import Database.Bloodhound.Types
6868-- >>> let testIndex = IndexName "twitter"
6969-- >>> let testMapping = MappingName "tweet"
7070-- >>> let defaultIndexSettings = IndexSettings (ShardCount 3) (ReplicaCount 2)
71+-- >>> data TweetMapping = TweetMapping deriving (Eq, Show)
7172-- >>> _ <- deleteIndex testServer testIndex
73+-- >>> _ <- deleteMapping testServer testIndex testMapping
74+-- >>> :{
75+--instance ToJSON TweetMapping where
76+-- toJSON TweetMapping =
77+-- object ["tweet" .=
78+-- object ["properties" .=
79+-- object ["location" .=
80+-- object ["type" .= ("geo_point" :: Text)]]]]
81+-- :}
7282
73−-- no trailing slashes in servers, library handles building the path.
74−-- defaultIndexSettings is exported by Database.Bloodhound as well
7583
7684mkShardCount :: Int -> Maybe ShardCount
7785mkShardCount n
@@ -125,15 +133,27 @@ getStatus (Server server) = do
125133
126134-- | createIndex will create an index given a 'Server',
127135-- 'IndexSettings', and an 'IndexName'
128−-- >>> response <- createIndex testServer defaultIndexSettings testIndex
136+--
137+-- >>> response <- createIndex testServer defaultIndexSettings (IndexName "didimakeanindex")
129138-- >>> respIsTwoHunna response
130139-- True
140+-- >>> indexExists testServer (IndexName "didimakeanindex")
141+-- True
131142createIndex :: Server -> IndexSettings -> IndexName -> IO Reply
132143createIndex (Server server) indexSettings (IndexName indexName) =
133144 put url body
134145 where url = joinPath [server, indexName]
135146 body = Just $ encode indexSettings
136147
148+-- | deleteIndex will delete an index given a 'Server',
149+-- and an 'IndexName'
150+--
151+-- >>> response <- createIndex testServer defaultIndexSettings (IndexName "didimakeanindex")
152+-- >>> response <- deleteIndex testServer (IndexName "didimakeanindex")
153+-- >>> respIsTwoHunna response
154+-- True
155+-- >>> indexExists testServer testIndex
156+-- False
137157deleteIndex :: Server -> IndexName -> IO Reply
138158deleteIndex (Server server) (IndexName indexName) =
139159 delete $ joinPath [server, indexName]
@@ -152,6 +172,13 @@ indexExists (Server server) (IndexName indexName) = do
152172 return exists
153173 where url = joinPath [server, indexName]
154174
175+-- | 'refreshIndex' will force a refresh on an index. You must
176+-- do this if you want to read what you wrote.
177+--
178+-- >>> response <- createIndex testServer defaultIndexSettings testIndex
179+-- >>> response <- refreshIndex testServer testIndex
180+-- >>> respIsTwoHunna response
181+-- True
155182refreshIndex :: Server -> IndexName -> IO Reply
156183refreshIndex (Server server) (IndexName indexName) =
157184 post url Nothing
@@ -174,9 +201,13 @@ openIndex = openOrCloseIndexes OpenIndex
174201closeIndex :: Server -> IndexName -> IO Reply
175202closeIndex = openOrCloseIndexes CloseIndex
176203
177−{-| putMapping is an HTTP PUT and has upsert semantics. Mappings are schemas
178− for documents in indexes.
179−-}
204+-- | 'putMapping' is an HTTP PUT and has upsert semantics. Mappings are schemas
205+-- for documents in indexes.
206+--
207+-- >>> response <- createIndex testServer defaultIndexSettings testIndex
208+-- >>> resp <- putMapping testServer testIndex testMapping TweetMapping
209+-- >>> respIsTwoHunna resp
210+-- True
180211putMapping :: ToJSON a => Server -> IndexName
181212 -> MappingName -> a -> IO Reply
182213putMapping (Server server) (IndexName indexName) (MappingName mappingName) mapping =
@@ -184,6 +215,16 @@ putMapping (Server server) (IndexName indexName) (MappingName mappingName) mappi
184215 where url = joinPath [server, indexName, mappingName, "_mapping"]
185216 body = Just $ encode mapping
186217
218+-- | 'deleteMapping' is an HTTP DELETE and deletes a mapping for a given index.
219+-- Mappings are schemas for documents in indexes.
220+--
221+-- >>> response <- createIndex testServer defaultIndexSettings testIndex
222+-- >>> resp <- putMapping testServer testIndex testMapping TweetMapping
223+-- >>> respIsTwoHunna resp
224+-- True
225+-- >>> resp <- deleteMapping testServer testIndex testMapping
226+-- >>> respIsTwoHunna resp
227+-- True
187228deleteMapping :: Server -> IndexName -> MappingName -> IO Reply
188229deleteMapping (Server server) (IndexName indexName)
189230 (MappingName mappingName) =