It seems I don't understand how text index in IxSet
should work. Here, I created a unit-test that indexes items by tags
field, and then query items by tag "tag". It should (from my perspective) give me back all 3 items (because they all have tag "tag"), but seems they don't. And I don't understand why.
{-# LANGUAGE DeriveDataTypeable #-}
import Test.HUnit
import Data.IxSet
import Data.List.Split ( splitOn )
import Data.Data ( Data, Typeable )
data TaggedItem = TaggedItem { getTags :: String }
deriving ( Show, Ord, Eq, Data, Typeable )
data Tag = Tag String
deriving ( Show, Ord, Eq, Data, Typeable )
getTagStrings :: TaggedItem -> [Tag]
getTagStrings = map Tag . splitOn "," . getTags
instance Indexable TaggedItem where
empty = ixSet
[ ixFun getTagStrings ]
test3 = TestCase (
assertEqual "ixFun multiple tags test"
3
(size (index @= (Tag "tag"))) )
where items = [ TaggedItem "tag,tag1,tag2"
, TaggedItem "tag,tag1,tag2"
, TaggedItem "tag,tag1,tag2" ]
index = fromList items
tests = TestList [
-- TestLabel "test1" test1,
-- TestLabel "test2" test2,
TestLabel "test3" test3]
main = do
runTestTT tests
Update:
Hmm, quite interesting. I've added field "name" and made at each item name to be different, and now it works.
{-# LANGUAGE DeriveDataTypeable #-}
import Test.HUnit
import Data.IxSet
import Data.List.Split ( splitOn )
import Data.Data ( Data, Typeable )
newtype TaggedItemName = TaggedItemName { unTaggedItemName :: String }
deriving ( Show, Ord, Eq, Data, Typeable )
data TaggedItem = TaggedItem { getName :: TaggedItemName, getTags :: String }
deriving ( Show, Ord, Eq, Data, Typeable )
data Tag = Tag String
deriving ( Show, Ord, Eq, Data, Typeable )
getTagStrings :: TaggedItem -> [Tag]
getTagStrings = map Tag . splitOn "," . getTags
instance Indexable TaggedItem where
empty = ixSet
[ ixFun getTagStrings ]
test3 = TestCase (
assertEqual "ixFun multiple tags test"
3
(size (index @= (Tag "tag"))) )
where items = [ TaggedItem (TaggedItemName "name1") "tag,tag1,tag2"
, TaggedItem (TaggedItemName "name2") "tag,tag1,tag2"
, TaggedItem (TaggedItemName "name3") "tag,tag1,tag2" ]
index = fromList items
tests = TestList [
-- TestLabel "test1" test1,
-- TestLabel "test2" test2,
TestLabel "test3" test3]
main = do
runTestTT tests
Works. So maybe IxSet considered those items as same and merged them somehow (?)
Yes, it seems IxSet merges equal values. I took first example and changed it so that it did:
where items = [ TaggedItem "tag,tag1,tag2"
, TaggedItem "tag,tag3,tag4"
, TaggedItem "tag,tag5,tag6" ]
And now it also works.