【问题标题】:mongodb via Haskell: creating a text-search indexmongodb 通过 Haskell:创建文本搜索索引
【发布时间】:2013-12-26 03:39:15
【问题描述】:

为了通过文本在 mongo 数据库中搜索项目,我需要根据 documentation 创建一个索引。

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

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

为什么代码会出现这个错误?

*** 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

更新见下方评论。

【问题讨论】:

    标签: mongodb haskell


    【解决方案1】:

    我的猜测是:在开发 Haskell 驱动程序时,MongoDB 根本不支持全文索引。因此,当前版本的驱动无法创建"text" 索引。

    更新 1:再想一想,您或许可以创建一个 "text" 索引,将文档插入到 "system.indexes",就像驱动程序中的 done 一样。

    更新 2:使用 createIndex 无济于事,因为 MongoDB doesn't allow"text" 传递为 iKey。这是一种似乎有效的方法:

    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"
    

    【讨论】:

    • 你知道这个异常可能指的是什么吗? text 字段包含在 obj 字段中。 *** Exception: expected "text" in [ score: 0.75, obj: [ _id: 52befd9e88458526ad000000, created: 2013-12-28 16:34:38.598 UTC, text: "Test note"]] 有三个地方我使用了文字“text”,当我将它们每个更改为“obj.text”时,错误仍然存​​在,除了我在文本搜索命令中更改“text”时,在在这种情况下,错误是no such cmd: obj.text。我更改“文本”的另外两个地方是doc 键和createTextIndex 中的值。
    • 您应该使用!? 运算符来获取嵌套字段,即。 e. `医生!? “obj.text”。
    • 我应该在哪里使用这个操作符?
    猜你喜欢
    • 1970-01-01
    • 2018-09-27
    • 1970-01-01
    • 2015-11-08
    • 2014-01-11
    • 2011-03-20
    • 1970-01-01
    • 2016-12-12
    • 2018-10-26
    相关资源
    最近更新 更多