簡體   English   中英

如何在mongodb數組字段上進行全文搜索?

[英]how to do full text search on mongodb array fields?

嗨,我的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/

在兩個字段上創建唯一索引的另一種方法:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM