@@ -21,6 +21,7 @@ import Data.Aeson.TH (deriveJSON)
21import qualified Data.ByteString.Lazy.Char8 as L
21import qualified Data.ByteString.Lazy.Char8 as L
22import Data.List (intercalate)
22import Data.List (intercalate)
23import Data.Maybe (fromMaybe)
23import Data.Maybe (fromMaybe)
24import Data.Text (Text)
25import Data.Text (Text)
25import Data.Time.Clock (UTCTime)
26import Data.Time.Clock (UTCTime)
26import GHC.Generics (Generic)
27import GHC.Generics (Generic)
@@ -53,33 +54,8 @@ instance (FromJSON a, ToJSON a) => FromJSON (Status a) where
53 v .: "tagline"
54 v .: "tagline"
54 parseJSON _ = empty
55 parseJSON _ = empty
56−-- instance ToJSON (Status a)
57−-- instance FromJSON (Status a)
59−-- bloo <- (liftM responseBody $ parseUrl "http://localhost:9200/" >>= \r -> withManager (httpLbs r))
62−-- rootPath = rollUp $ ["/"]
64−-- -- Kinda hate this.
65−-- pathFromType :: String -> String -> String
66−-- pathFromType index docType = "/" ++ index ++ docType
68−-- -- Kinda hate this too.
69−-- rollUp :: [String] -> String
70−-- rollUp = Prelude.concat
72main = simpleHttp "http://localhost:9200/events/event/_search?q=hostname:localhost&size=1" >>= L.putStrLn
57main = simpleHttp "http://localhost:9200/events/event/_search?q=hostname:localhost&size=1" >>= L.putStrLn
74−-- data Response = Response { blah :: Text } deriving (Show)
76−-- indexDocument :: ToJSON a => a -> IO Response
77−-- indexDocument doc = ToJSON a
79−-- nomenclature is because you're dropping the type baggage. All of this is compile-time erased.
80−-- newtype ShardCount = ShardCount { unShardCount :: Int } deriving (Show, Generic)
81−-- newtype ReplicaCount = ReplicaCount { unReplicaCount :: Int } deriving (Show, Generic)
83newtype ShardCount = ShardCount Int deriving (Show, Generic)
59newtype ShardCount = ShardCount Int deriving (Show, Generic)
84newtype ReplicaCount = ReplicaCount Int deriving (Show, Generic)
60newtype ReplicaCount = ReplicaCount Int deriving (Show, Generic)
@@ -95,8 +71,6 @@ mkReplicaCount n
95 | n > 1000 = Nothing -- ...
71 | n > 1000 = Nothing -- ...
96 | otherwise = Just (ReplicaCount n)
72 | otherwise = Just (ReplicaCount n)
98−-- IndexSettings <$> mkShardCount 10 <*> mkReplicaCount 20
100data IndexSettings =
74data IndexSettings =
101 IndexSettings { indexShards :: ShardCount
75 IndexSettings { indexShards :: ShardCount
102 , indexReplicas :: ReplicaCount } deriving (Show)
76 , indexReplicas :: ReplicaCount } deriving (Show)
@@ -113,20 +87,6 @@ data Strategy = RoundRobinStrat | RandomStrat | HeadStrat deriving (Show)
114newtype Server = Server String deriving (Show)
88newtype Server = Server String deriving (Show)
116−-- data Errors = IndexAlreadyExistsError | GenericError deriving (Show)
118−-- data ElasticsearchError = ElasticsearchError
120−-- , error :: Text } deriving (Show, Generic)
122−-- instance FromJSON ElasticsearchError
124−-- errorForResponse resp = do
125−-- let (sts, err) = decode (responseBody resp) :: ElasticsearchError
126−-- if T.isInfixOf "IndexAlreadyExistsException" error
127−-- then IndexAlreadyExistsError
130type Reply = Network.HTTP.Conduit.Response L.ByteString
90type Reply = Network.HTTP.Conduit.Response L.ByteString
131type Method = NHTM.Method
91type Method = NHTM.Method
@@ -242,17 +202,17 @@ data EsResult a = EsResult { _index :: Text
242 , _type :: Text
202 , _type :: Text
243 , _id :: Text
203 , _id :: Text
244 , _version :: Int
204 , _version :: Int
245− , found :: Bool
205+ , found :: Maybe Bool
246 , _source :: a } deriving (Eq, Show)
206 , _source :: a } deriving (Eq, Show)
248instance (FromJSON a, ToJSON a) => FromJSON (EsResult a) where
208instance (FromJSON a, ToJSON a) => FromJSON (EsResult a) where
249 parseJSON (Object v) = EsResult <$>
209 parseJSON (Object v) = EsResult <$>
250− v .: "_index" <*>
210+ v .: "_index" <*>
251− v .: "_type" <*>
211+ v .: "_type" <*>
252− v .: "_id" <*>
212+ v .: "_id" <*>
253− v .: "_version" <*>
213+ v .: "_version" <*>
254− v .: "found" <*>
214+ v .:? "found" <*>
255− v .: "_source"
215+ v .: "_source"
256 parseJSON _ = empty
216 parseJSON _ = empty
258getDocument :: Server -> IndexName -> MappingName
218getDocument :: Server -> IndexName -> MappingName
@@ -270,25 +230,6 @@ documentExists (Server server) indexName mappingName docId = do
270 return exists where
230 return exists where
271 url = joinPath [server, indexName, mappingName, docId]
231 url = joinPath [server, indexName, mappingName, docId]
273−-- data Analyzer = AStandardAnalyzer StandardAnalyzer
274−-- | SimpleAnalyzer -- just "simple"
275−-- | WhitespaceAnalyzer
279−-- | LanguageAnalyzer
280−-- | SnowballAnalyzer
283−-- data StandardAnalyzer =
285−-- { stopwords :: Maybe [Text] -- default []
286−-- , max_token_length :: Maybe Int -- default 255
289−-- DefaultField -> "default_field": ""
290−-- Fields -> "fields": []
292type QueryString = Text
233type QueryString = Text
293-- status:active
234-- status:active
294-- author:"John Smith"
235-- author:"John Smith"
@@ -345,14 +286,33 @@ queryStringQuery query = emptyQueryStringQuery { query = query }
345type FieldName = Text
286type FieldName = Text
347type Cache = Bool -- caching on/off
288type Cache = Bool -- caching on/off
348data Filter = AndFilter [Filter] Cache
291data Filter = AndFilter [Filter] Cache
349 | OrFilter [Filter] Cache
292 | OrFilter [Filter] Cache
350 | BoolFilter BoolMatch Cache
294 | BoolFilter BoolMatch Cache
351 | ExistsFilter FieldName -- always cached
295 | ExistsFilter FieldName -- always cached
352 | GeoBoundingBoxFilter GeoBoundingBoxConstraint GeoFilterType Cache
296 | GeoBoundingBoxFilter GeoBoundingBoxConstraint GeoFilterType Cache
353 | GeoDistanceFilter GeoConstraint Distance Cache
297 | GeoDistanceFilter GeoConstraint Distance Cache
354 deriving (Show)
298 deriving (Show)
300+class Monoid a => Seminearring a where
302+ (<||>) :: a -> a -> a
303+ (<&&>) :: a -> a -> a
309+instance Monoid Filter where
310+ mempty = IdentityFilter
311+ mappend a b = AndFilter [a, b] defaultCache
313+instance Seminearring Filter where
314+ a <||> b = OrFilter [a, b] defaultCache
356instance ToJSON Filter where
316instance ToJSON Filter where
357 toJSON (AndFilter filters cache) =
317 toJSON (AndFilter filters cache) =
358 object ["and" .= fmap toJSON filters
318 object ["and" .= fmap toJSON filters
@@ -360,6 +320,8 @@ instance ToJSON Filter where
360 toJSON (OrFilter filters cache) =
320 toJSON (OrFilter filters cache) =
361 object ["or" .= fmap toJSON filters
321 object ["or" .= fmap toJSON filters
362 , "_cache" .= cache]
322 , "_cache" .= cache]
323+ toJSON (IdentityFilter) =
324+ object ["match_all" .= object []]
363 toJSON (ExistsFilter fieldName) =
325 toJSON (ExistsFilter fieldName) =
364 object ["exists" .= object
326 object ["exists" .= object
365 ["field" .= fieldName]]
327 ["field" .= fieldName]]
@@ -367,7 +329,6 @@ instance ToJSON Filter where
367 object ["bool" .= toJSON boolMatch
329 object ["bool" .= toJSON boolMatch
368 , "_cache" .= cache]
330 , "_cache" .= cache]
371data Term = Term { termField :: Text
332data Term = Term { termField :: Text
372 , termValue :: Text } deriving (Show)
333 , termValue :: Text } deriving (Show)
@@ -446,25 +407,3 @@ data ShardResults =
446 ShardResults { shardTotal :: Int
407 ShardResults { shardTotal :: Int
447 , shardsSuccessful :: Int
408 , shardsSuccessful :: Int
448 , shardsFailed :: Int } deriving (Show)
409 , shardsFailed :: Int } deriving (Show)
450−-- This is turning into a fractal of horror
451−-- data Fuzziness = FDateFuzziness DateFuzziness |
453−-- data Query = Query { query :: () } deriving (Show)
454−-- search :: Server -> IndexName -> Maybe MappingName
455−-- -> Query -> IO Reply
458−-- getStatus :: String -> IO (Maybe (Status Version))
459−-- getStatus server = do
460−-- request <- parseUrl $ server ++ rootPath
461−-- response <- withManager $ httpLbs request
462−-- return $ (decode $ responseBody response)
464−-- mkServer -- (Maybe Server) for URL parsing?
466−-- data Cluster = Cluster { targets :: [Server]
467−-- , strategy :: Strategy } deriving (Show)
469−-- target :: Cluster -> Server
470−-- target (Cluster targets HeadStrat) = head targets