似乎我不明白IxSet
的文本索引应该如何工作。在这里,我创建了一个单元测试,它按tags
字段索引项目,然后按标记"tag"查询项目。它应该(从我的角度来看)把我所有的3件物品都还给我(因为它们都有标签"标签"),但似乎他们没有。我不明白为什么。
{-# 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
更新:嗯,很有趣。我添加了字段"name",并使每个项目的名称不同,现在它可以工作了。
{-# 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
。因此,IxSet可能认为这些项是相同的,并以某种方式合并它们(?)
是的,IxSet似乎合并了相等的值。我以第一个例子为例,并将其更改为:
where items = [ TaggedItem "tag,tag1,tag2"
, TaggedItem "tag,tag3,tag4"
, TaggedItem "tag,tag5,tag6" ]
现在也可以了