通过Haskell创建mongodb:创建文本搜索索引
为了通过文本搜索mongo数据库中的项目,我需要创建一个索引,如下所示 下面的代码创建此索引:通过Haskell创建mongodb:创建文本搜索索引,mongodb,haskell,Mongodb,Haskell,为了通过文本搜索mongo数据库中的项目,我需要创建一个索引,如下所示 下面的代码创建此索引: 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
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
let order=[(fieldToText TextField)=:(1::Int32)]
docIndex=索引(docTypeToText docType)顺序
actionResult do putStrLn$显示失败
返回[]
对()->do
putStrLn$“索引为”++显示文档索引
运行管道dbName$ensureIndex docIndex
mDoc do putStrLn$显示失败
返回[]
右单据->让数组结果=valueAt(打包“结果”)单据
ds=[d | Doc d我的猜测是:在开发Haskell驱动程序时,MongoDB根本不支持全文索引。因此,用当前版本的驱动程序创建“text”
索引是不可能的
更新1:仔细想想,您可能可以创建一个“文本”
索引,将文档上传到“system.index”
,就像它在驱动程序中一样
更新2:使用createIndex
不会有帮助,因为MongoDB将文本作为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"
createTextIndex::Collection->String->[Label]->操作IO()
createTextIndex col name keys=do
db文档->操作IO文档
搜索列术语筛选器=运行命令[“文本”=:列
,“搜索”=:术语
,“过滤器”=:过滤器
]
main::IO()
main=do
管道您知道此异常可能指的是什么吗?text
字段包含在obj
字段中。***异常:在[score:0.75,obj:[\u id:52befd9e88458526ad000000,created:2013-12-28 16:34:38.598 UTC,text:“Test note”]]中预期为“text”
我使用文字“text”有三处,当我将它们分别更改为“obj.text”时,错误保持不变,但在文本搜索命令中更改“text”时除外,在这种情况下,错误为无此类cmd:obj.text
。其他两个位置我更改了“text”是createTextIndex
中的doc
键和值。您应该使用运算符获取嵌套字段,即`doc!?“obj.text”。我应该在哪里使用此运算符?