通过Haskell的mongodb:创建一个文本搜索索引 [英] mongodb via Haskell: creating a text-search index

查看:87
本文介绍了通过Haskell的mongodb:创建一个文本搜索索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了通过文本搜索mongo数据库中的项目,我需要创建一个索引,按照

In order to search items in a mongo database by text, I need to create an index, as per the documentation.

下面的代码创建了这个索引:

The code below creates this index:

index is Index {iColl = "note", iKey = [ text: 1], iName = "text_1", iUnique = False, iDropDups = False}

为什么代码会出现此错误?

Why does the code give this error?

*** Exception: expected "results" in [ ok: 0.0, errmsg: "no text index for: db.note"]

更新
我使用以下更新的代码获得相同的错误。我改变了我现在使用 createIndex

         let order = [(fieldToText TextField) =: (1 :: Int32)]
              docIndex =  index (docTypeToText docType) order
          actionResult <- run pipe dbName $ createIndex docIndex
          case actionResult of
            Left failure -> do putStrLn $ show failure
                               return []
            Right () -> do
              putStrLn $ "index is " ++ show docIndex
              run pipe dbName $ ensureIndex docIndex
              mDoc <- run pipe dbName $ runCommand
                [pack "text" =: (docTypeToText docType),
                  pack "search" =: (pack $ unwords keywords),
                    pack "filter" =: (selector $ selection query)]
              case mDoc of
                Left failure -> do putStrLn $ show failure
                                   return []
                Right doc -> let Array results = valueAt (pack "results") doc
                                 ds = [d | Doc d <- results]
                             in return ds

UPDATE 请参阅下面的注释。

推荐答案

我的猜测是:在开发Haskell驱动程序时,MongoDB根本不支持full-文本索引。因此,使用当前版本的驱动程序创建text索引是不可能的。

My guess is: at the time Haskell driver was developed, MongoDB simply didn't support full-text indexing. So, it's impossible to create a "text" index with the current version of the driver.

更新1 :第二个想法是,您可以创建一个将文档插入到system.indexes中的text code>,就像 done a>在驱动程序中。

Update 1: on second thought, you can probably create a "text" index upserting a document into "system.indexes", like it's done in the driver.

更新2 :使用 createIndex ,因为MongoDB 不允许text传递为 iKey 。这是一种似乎正在工作的方法:

Update 2: using createIndex won't help, because MongoDB doesn't allow passing "text" as iKey. Here's an approach which seems to be working:

createTextIndex :: Collection -> String -> [Label] -> Action IO ()
createTextIndex col name keys = do
    db <- thisDatabase
    let doc = [ "ns"   =: db <.> col
              , "key"  =: [key =: ("text" :: String) | key <- keys]
              , "name" =: name
              ]
    insert_ "system.indexes" doc

search :: Collection -> String -> Document -> Action IO Document
search col term filter = runCommand [ "text"   =: col
                                    , "search" =: term
                                    , "filter" =: filter
                                    ]

main :: IO ()
main = do
    pipe <- runIOE $ connect host
    res  <- access pipe master "test" $ do
        createTextIndex "foo" "foo-index" ["bar"]
        search "foo" "some-keyword" []
    print res
  where
    host = Host "localhost" $ UnixSocket "/tmp/mongodb-27017.sock"

这篇关于通过Haskell的mongodb:创建一个文本搜索索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆