git.iko.soy/typeable/bloodhound
on master help sign in
positive validation of sorting
commit1de5b91a86c3efbc9d3a5f55b01a5583dbc67bb9 authorChris Allen <cma@bitemyapp.com> dateTue 15 Apr 2014, 03:10 (13 years ago) parent2480dcb
4 files changed+41−2
Database/Bloodhound.hs+1 Database/Bloodhound/Types.hs+5 README.org+8−2 tests/tests.hs+27
Database/Bloodhound.hs+1
@@ -21,6 +21,7 @@ module Database.Bloodhound
2121 , rangeToKV
2222 , showText
2323 , unpackId
24+ , mkSort
2425 , Version(..)
2526 , Status(..)
2627 , Existence(..)
Database/Bloodhound/Types.hs+5
@@ -6,6 +6,7 @@ module Database.Bloodhound.Types
66 , halfRangeToKV
77 , maybeJson
88 , maybeJsonF
9+ , mkSort
910 , rangeToKV
1011 , showText
1112 , unpackId
@@ -162,6 +163,10 @@ data SortMode = SortMin
162163 | SortMax
163164 | SortSum
164165 | SortAvg deriving (Eq, Show)
166+
167+mkSort :: FieldName -> SortOrder -> DefaultSort
168+mkSort fieldName sortOrder = DefaultSort fieldName sortOrder False Nothing Nothing Nothing
169+
165170type Cache = Bool -- caching on/off
166171defaultCache = False
167172
README.org+8−2
@@ -27,7 +27,7 @@ Bloodhound is alpha at the moment. The library works fine, but I don't want to m
2727-- the multi-line examples into your ghci REPL.
2828
2929:set -XDeriveGeneric
30−import Database.Bloodhound.Client
30+import Database.Bloodhound
3131import Data.Aeson
3232import Data.Either (Either(..))
3333import Data.Maybe (fromJust)
@@ -43,7 +43,7 @@ let testServer = (Server "http://localhost:9200")
4343let testIndex = IndexName "twitter"
4444let testMapping = MappingName "tweet"
4545
46−-- defaultIndexSettings is exported by Database.Bloodhound.Client as well.
46+-- defaultIndexSettings is exported by Database.Bloodhound as well
4747let defaultIndexSettings = IndexSettings (ShardCount 3) (ReplicaCount 2)
4848
4949-- createIndex returns IO Reply
@@ -281,6 +281,12 @@ Right [Hit {hitIndex = IndexName "twitter"
281281
282282#+END_SRC
283283
284+*** Sorting
285+
286+#+BEGIN_SRC haskell
287+
288+#+END_SRC
289+
284290*** Filtering
285291
286292**** And, Not, and Or filters
tests/tests.hs+27
@@ -56,6 +56,14 @@ exampleTweet = Tweet { user = "bitemyapp"
5656 , age = 10000
5757 , location = Location 40.12 (-71.34) }
5858
59+otherTweet = Tweet { user = "notmyapp"
60+ , postDate = UTCTime
61+ (ModifiedJulianDay 55000)
62+ (secondsToDiffTime 11)
63+ , message = "Use haskell!"
64+ , age = 1000
65+ , location = Location 40.12 (-71.34) }
66+
5967insertData :: IO ()
6068insertData = do
6169 let encoded = encode exampleTweet
@@ -66,6 +74,12 @@ insertData = do
6674 _ <- refreshIndex testServer testIndex
6775 return ()
6876
77+insertOther :: IO ()
78+insertOther = do
79+ docCreated <- indexDocument testServer testIndex testMapping otherTweet (DocId "2")
80+ _ <- refreshIndex testServer testIndex
81+ return ()
82+
6983searchTweet :: Search -> IO (Either String Tweet)
7084searchTweet search = do
7185 reply <- searchByIndex testServer testIndex search
@@ -132,6 +146,19 @@ main = hspec $ do
132146 myTweet <- searchTweet search
133147 myTweet `shouldBe` Right exampleTweet
134148
149+ describe "sorting" $ do
150+ it "returns documents in the right order" $ do
151+ _ <- insertData
152+ _ <- insertOther
153+ let sortSpec = DefaultSortSpec $ mkSort (FieldName "age") Ascending
154+ let search = Search Nothing
155+ (Just IdentityFilter) (Just [sortSpec])
156+ False 0 10
157+ reply <- searchByIndex testServer testIndex search
158+ let result = eitherDecode (responseBody reply) :: Either String (SearchResult Tweet)
159+ let myTweet = fmap (hitSource . head . hits . searchHits) result
160+ myTweet `shouldBe` Right otherTweet
161+
135162 describe "filtering API" $ do
136163 it "returns document for composed boolmatch and identity" $ do
137164 _ <- insertData