【问题标题】:how to do full text search on mongodb array fields?如何对mongodb数组字段进行全文搜索?
【发布时间】:2013-07-04 14:42:13
【问题描述】:

嗨,我的 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"})

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

【问题讨论】:

    标签: java mongodb search full-text-indexing


    【解决方案1】:

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

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

    查看更多:

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

    【讨论】:

      【解决方案2】:

      另一种在两个字段上创建唯一索引的方法:

      BasicDBObject basicDBObject = new BasicDBObject();
      basicDBObject.append("Keywords", "text");
      basicDBObject.append("Filename", "text");
      dbCollection.createIndex(basicDBObject);
      

      【讨论】:

        猜你喜欢
        • 2015-06-06
        • 2014-10-15
        • 1970-01-01
        • 1970-01-01
        • 2018-01-13
        • 1970-01-01
        • 1970-01-01
        • 2015-09-21
        • 2015-11-21
        相关资源
        最近更新 更多