java - 如何对 mongodb 数组字段进行全文搜索?

标签 java mongodb search full-text-indexing

嗨,我的 Mongo 数据库字段如下:

        "_id" : ObjectId("51d582be7a8d51bd29ca78a3"),
        "Filename" : "sample.pdfe",
        "Title" : null,
        "Author" : null,
        "ContentType" : "application/x-msdownload; format=pe32",
        "url" : "Z:sample.pdf",
        "Keywords" : ["php","java",".net","python"]

下面是我对索引字段进行全文搜索的代码

m = new Mongo("10.0.0.26", 27017);
DB db = m.getDB("soft") ;
DBCollection col = db.getCollection("metadatanew") ;
String collection = col.toString();
DBObject searchCmd = new BasicDBObject();
searchCmd.put("text", collection); 
searchCmd.put("search", name); 

CommandResult commandResult = db.command(searchCmd);

BasicDBList results = (BasicDBList)commandResult.get("results");

for (Iterator <Object> it = results.iterator();it.hasNext();)
{
    BasicDBObject result  = (BasicDBObject) it.next();
    BasicDBObject dbo = (BasicDBObject) result.get("obj");
    System.out.println(dbo.getString("Filename"));
}

我使用以下命令在两个字段上创建文本索引:

db.metadatanew.ensureIndex({'Filename':"text"}, {'Keywords':"text"})

我已经为文件名和关键字创建了文本索引,但我只能对文件名进行全文搜索,我的代码有什么问题吗,请帮助我

最佳答案

ensureIndex 函数的第二个参数是选项。您应该在两个字段上创建一个唯一索引:

db.metadatanew.ensureIndex( { "Keywords" : "text", "Filename" : "text"} )

查看更多:

http://docs.mongodb.org/manual/tutorial/create-text-index-on-multiple-fields/

关于java - 如何对 mongodb 数组字段进行全文搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17473087/

相关文章:

MongoDB测试错误

java 6 -> mongodb maven 依赖错误 : peer not authenticated

mongodb - 由于缺少 'ISODate',使用时间时 Golang + mgo 查询 mongodb 失败

安卓联系人搜索 : Using dialpad keys (alphabets combinations/permutations)

java - 按键升序排序 map

java - 无法从 Eclipse 运行 JavaFx 代码

java - 如何在 WildFly 10 中动态更改 JMS 队列 MDB 池大小

C 如何检查并指向 int 数字中的 '0'

algorithm - 如果树是平衡的,在二叉搜索树中搜索的时间复杂度是多少?

java - 通过类实现接口(interface)创建基于 CGLib 的代理