1{-# LANGUAGE DeriveGeneric #-}
2{-# LANGUAGE OverloadedStrings #-}
8-------------------------------------------------------------------------------
9import Control.Monad.IO.Class (liftIO)
10import Data.Aeson (FromJSON (..), defaultOptions,
11 genericParseJSON, genericToJSON,
13import Data.List.NonEmpty (NonEmpty (..))
14import Data.Text (Text)
15import Data.Time.Calendar (Day (..))
16import Data.Time.Clock (UTCTime (..), secondsToDiffTime)
17import qualified Data.Vector as V
18import Database.V5.Bloodhound
19import GHC.Generics (Generic)
20import Network.HTTP.Client (defaultManagerSettings)
21-------------------------------------------------------------------------------
24data TweetMapping = TweetMapping deriving (Eq, Show)
26instance ToJSON TweetMapping where
30 object ["location" .= object ["type" .= ("geo_point" :: Text)]]
32-------------------------------------------------------------------------------
39 } deriving (Eq, Generic, Show)
42-------------------------------------------------------------------------------
47 , postDate = UTCTime (ModifiedJulianDay 55000) (secondsToDiffTime 10)
48 , message = "Use haskell!"
53 loc = LatLon {lat = 40.12, lon = -71.3}
55instance ToJSON Tweet where
56 toJSON = genericToJSON defaultOptions
57instance FromJSON Tweet where
58 parseJSON = genericParseJSON defaultOptions
64 _ <- createIndex indexSettings testIndex
65 True <- indexExists testIndex
66 _ <- putMapping testIndex testMapping TweetMapping
69 resp <- indexDocument testIndex testMapping defaultIndexDocumentSettings exampleTweet (DocId "1")
71 -- Response {responseStatus = Status {statusCode = 201, statusMessage = "Created"}, responseVersion = HTTP/1.1, responseHeaders = [("Content-Type","application/json; charset=UTF-8"),("Content-Length","74")], responseBody = "{\"_index\":\"twitter\",\"_type\":\"tweet\",\"_id\":\"1\",\"_version\":1,\"created\":true}", responseCookieJar = CJ {expose = []}, responseClose' = ResponseClose}
74 let stream = V.fromList [BulkIndex testIndex testMapping (DocId "2") (toJSON exampleTweet)]
76 -- Bulk loads require an index refresh before new data is loaded.
77 _ <- refreshIndex testIndex
79 -- set up some aliases
80 let aliasName = IndexName "twitter-alias"
81 let iAlias = IndexAlias testIndex (IndexAliasName aliasName)
82 let aliasRouting = Nothing
83 let aliasFiltering = Nothing
84 let aliasCreate = IndexAliasCreate aliasRouting aliasFiltering
85 _ <- updateIndexAliases (AddAlias iAlias aliasCreate :| [])
86 True <- indexExists aliasName
88 -- create a template so that if we just write into an index named tweet-2017-01-02, for instance, the index will be automatically created with the given mapping. This is a great idea for any ongoing indices because it makes them much easier to manage and rotate.
89 let idxTpl = IndexTemplate (TemplatePattern "tweet-*") (Just (IndexSettings (ShardCount 1) (ReplicaCount 1))) [toJSON TweetMapping]
90 let templateName = TemplateName "tweet-tpl"
91 _ <- putTemplate idxTpl templateName
92 True <- templateExists templateName
96 let query = TermQuery (Term "user" "bitemyapp") boost
97 let search = mkSearch (Just query) boost
98 _ <- searchByType testIndex testMapping search
101 _ <- deleteTemplate templateName
102 _ <- deleteIndex testIndex
103 False <- indexExists testIndex
107 testServer = Server "http://localhost:9200"
108 runBH' = withBH defaultManagerSettings testServer
109 testIndex = IndexName "twitter"
110 testMapping = MappingName "tweet"
111 indexSettings = IndexSettings (ShardCount 1) (ReplicaCount 0)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list