addAttrCond cond "rakka:isFeed STREQ yes"
setOrder cond "@uri STRA"
result <- searchPages sto cond
- return (map srPageName result)
+ return (map hpPageName $ srPages result)
mkFeedURIStr :: URI -> PageName -> String
}
+defaultResultsPerPage :: Int
+defaultResultsPerPage = 20
+
+
{-
<searchResult query="foo bar baz"
from="0"
= do params <- getQueryForm
let query = UTF8.decodeString $ fromMaybe "" $ lookup "q" params
- from = read $ fromMaybe "0" $ lookup "from" params
- to = read $ fromMaybe "20" $ lookup "to" params
+ from = fromMaybe 0
+ $ fmap read $ lookup "from" params
+ to = fromMaybe defaultResultsPerPage
+ $ fmap read $ lookup "to" params
cond <- liftIO $ mkCond query from to
result <- searchPages (envStorage env) cond
- let to' = min (from + length result) to
+ let to' = min (from + length (srPages result)) to
runIdempotentA $ proc ()
-> do tree <- ( eelem "/"
+= sattr "query" query
+= sattr "from" (show from)
+= sattr "to" (show to')
- += sattr "total" (show $ length result)
- += ( constL result
+ += sattr "total" (show $ srTotal result)
+ += ( constL (srPages result)
>>>
mkPageElem
)
setMax cond (to - from)
return cond
- mkPageElem :: ArrowXml a => a SearchResult XmlTree
+ mkPageElem :: ArrowXml a => a HitPage XmlTree
mkPageElem = ( eelem "page"
- += attr "name" (arr srPageName >>> mkText)
- += ( arrL srSnippet
+ += attr "name" (arr hpPageName >>> mkText)
+ += ( arrL hpSnippet
>>>
mkSnippetTree
)
-- re-export from Rakka.Storage.Types
, SearchResult(..)
+ , HitPage(..)
, SnippetFragment(..)
, mkStorage -- private
getDirContentsA = arrIO2 . getDirContents
-searchPages :: MonadIO m => Storage -> Condition -> m [SearchResult]
+searchPages :: MonadIO m => Storage -> Condition -> m SearchResult
searchPages sto cond
= liftIO $
do var <- newEmptyTMVarIO
mapM_ (updateIndex index repos mkDraft newRev) (S.toList pages)
-searchIndex :: Database -> Condition -> IO [SearchResult]
+searchIndex :: Database -> Condition -> IO SearchResult
searchIndex index cond
= do (ids, hint) <- searchDatabase' index cond
- mapM (fromId $ map fst hint) ids
+ let (total, words) = parseHint hint
+ pages <- mapM (fromId words) ids
+ return SearchResult {
+ srTotal = total
+ , srPages = pages
+ }
where
- fromId :: [String] -> DocumentID -> IO SearchResult
+ parseHint :: [(String, Int)] -> (Int, [String])
+ parseHint xs
+ = let total = fromJust $ lookup "" xs
+ words = filter (/= "") $ map fst xs
+ in
+ (total, words)
+
+ fromId :: [String] -> DocumentID -> IO HitPage
fromId words docId
= do uri <- getDocURI index docId
rev <- getDocAttr index docId "rakka:revision"
do doc <- getDocument index docId [NoAttributes, NoKeywords]
sn <- makeSnippet doc words 300 80 80
return (trim (== Boundary) $ map toFragment sn)
- return SearchResult {
- srPageName = decodePageName $ uriPath uri
- , srPageRev = rev
- , srSnippet = snippet
+ return HitPage {
+ hpPageName = decodePageName $ uriPath uri
+ , hpPageRev = rev
+ , hpSnippet = snippet
}
toFragment :: Either String (String, String) -> SnippetFragment
( Storage(..)
, IndexReq(..)
, SearchResult(..)
+ , HitPage(..)
, SnippetFragment(..)
)
where
data IndexReq
= RebuildIndex
| SyncIndex
- | SearchIndex !Condition !(TMVar [SearchResult])
+ | SearchIndex !Condition !(TMVar SearchResult)
data SearchResult
= SearchResult {
- srPageName :: !PageName
- , srPageRev :: !RevNum
- , srSnippet :: [SnippetFragment]
+ srTotal :: !Int
+ , srPages :: ![HitPage]
+ }
+ deriving (Show, Eq)
+
+
+data HitPage
+ = HitPage {
+ hpPageName :: !PageName
+ , hpPageRev :: !RevNum
+ , hpSnippet :: [SnippetFragment]
}
deriving (Show, Eq)
setMax cond items
result <- searchPages sto cond
- pages <- mapM (getPageBySR sto) result
+ pages <- mapM (getPageByHP sto) (srPages result)
mkPageList showSummary pages
}
where
- getPageBySR :: Storage -> SearchResult -> IO Page
- getPageBySR sto sr
- = getPage sto (srPageName sr) (Just (srPageRev sr)) >>= return . fromJust
+ getPageByHP :: Storage -> HitPage -> IO Page
+ getPageByHP sto hp
+ = getPage sto (hpPageName hp) (Just (hpPageRev hp)) >>= return . fromJust
mkPageList :: Bool -> [Page] -> IO BlockElement
mkPageList showSummary pages