Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding support for filtering by field

  • Loading branch information...
commit b7a6df203eebff14be47fb8effa6e9c2074ea98a 1 parent 2e9d23b
@zmoazeni authored
Showing with 19 additions and 11 deletions.
  1. +7 −3 src/Storage.hs
  2. +12 −8 src/Web.hs
View
10 src/Storage.hs
@@ -17,6 +17,7 @@ import qualified Data.Binary as Bin
import Data.Binary (Binary, Get, decode, encode)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Lazy as BL (ByteString)
+import Data.Text.Lazy hiding (map, empty, null)
import Data.Text.Lazy.Encoding (decodeUtf8)
import Data.Time.Clock.POSIX
import Codec.Digest.SHA
@@ -102,11 +103,14 @@ flushIterator stageDB gramDB iter = iterFirst iter >> iterValid iter >>= flush'
iterValid iter >>= flush'
| otherwise = yield
-search :: DB -> Gram -> IO A.Value
-search db gram = do
+search :: DB -> Text -> [Text] -> IO A.Value
+search db query fields = do
+ let gram = Gram (toStrict query)
maybeValue <- get db [] (encode' gram)
case maybeValue of
Just binaryIndexes -> do let indexes = decode' binaryIndexes :: [Index]
- return . A.Array . V.fromList . nub $ map indexId indexes
+ filtered = [x | x <- indexes, indexField x `elem` (map toStrict fields)]
+ xs = if null fields then indexes else filtered
+ return . A.Array . V.fromList . nub $ map indexId xs
Nothing -> return $ A.Array (empty)
View
20 src/Web.hs
@@ -1,6 +1,6 @@
{-# LANGUAGE OverloadedStrings #-}
-module Web where
+module Web (run) where
import Web.Scotty
import Storage
@@ -8,6 +8,7 @@ import Parser
import Data.Text.Lazy
import Data.Text.Lazy.Encoding
import Database.LevelDB (DB)
+import Control.Monad
import Control.Monad.IO.Class
import Network.HTTP.Types
import Data.Aeson
@@ -15,14 +16,14 @@ import Data.Aeson
run :: String -> (DB, DB) -> IO ()
run port (gramDB, stageDB) = scotty (read port) $ do
get "/grams" $ do
- grams' <- fetchGrams
+ grams' <- fetchGrams gramDB
text . decodeUtf8 . encode . toJSON $ grams'
header "Content-Type" "application/json"
get "/search" $ do
- q <- param "q"
- let gram = Gram (toStrict q)
- results <- liftIO $ search gramDB gram
+ query <- param "q"
+ fields <- liftM toFields $ param "f" `rescue` (\_ -> return (pack ""))
+ results <- liftIO $ search gramDB query fields
text . decodeUtf8 . encode $ results
header "Content-Type" "application/json"
@@ -31,6 +32,9 @@ run port (gramDB, stageDB) = scotty (read port) $ do
liftIO $ queueAction stageDB IndexCreate b
status status201
- where
- fetchGrams :: ActionM [Gram]
- fetchGrams = liftIO (grams gramDB)
+fetchGrams :: DB -> ActionM [Gram]
+fetchGrams db = liftIO (grams db)
+
+toFields :: Text -> [Text]
+toFields "" = []
+toFields combined = splitOn "," combined
Please sign in to comment.
Something went wrong with that request. Please try again.