Java - MongoDB Compund索引文本搜索

时间:2014-08-07 05:11:44

标签: java mongodb

我在Eclipse中使用MongoDB最新版本2.12 Java Driver。然而,即使在搜索SO和Google之后,我也无法解决我的问题。 这是我的代码片段:

mongoClient_m = new MongoClient();
db_m = mongoClient_m.getDB("atlascompare");
coll_m = db_m.getCollection("CollRMOB");

    BasicDBObject doc = new BasicDBObject("ErrorName", "RMOB RULE_INFO")
                    .append("ErrorCause", "Database Out Of Sync")
                    .append("Resolution", "Try clearing your cache, or check database ver")
                    .append("TechnicalDat", new BasicDBObject("Java Null Pointer", 203).append("Java File Not Found Exception", 102)); 
    coll_m.insert(doc);
                    doc = new BasicDBObject("ErrorName", "ERROR X_2609")
                    .append("ErrorCause", "CAUSE X_2609")
                    .append("Resolution", "PERFORM X_2609")
                    .append("TechnicalDat", new BasicDBObject("Java X_2609_1", 203).append("Java X_2609_2", 102)); 
    coll_m.insert(doc);
                    doc = new BasicDBObject("ErrorName", "ERROR Y_2609")
                    .append("ErrorCause", "CAUSE Y_2609")
                    .append("Resolution", "PERFORM Y_2609")
                    .append("TechnicalDat", new BasicDBObject("Java Y_2609_1", 203).append("Java Y_2609_2", 102)); 
    coll_m.insert(doc);

以下是我打印文档时的输出:

{ "_id" : { "$oid" : "53e306577bdc3279309da4e3"} , "ErrorName" : "RMOB RULE_INFO" , "ErrorCause" : "Database Out Of Sync" , "Resolution" : "Try clearing your cache, or check database ver" , "TechnicalDat" : { "Java Null Pointer" : 203 , "Java File Not Found Exception" : 102}}
{ "_id" : { "$oid" : "53e306577bdc3279309da4e4"} , "ErrorName" : "ERROR X_2609" , "ErrorCause" : "CAUSE X_2609" , "Resolution" : "PERFORM X_2609" , "TechnicalDat" : { "Java X_2609_1" : 203 , "Java X_2609_2" : 102}}
{ "_id" : { "$oid" : "53e306577bdc3279309da4e5"} , "ErrorName" : "ERROR Y_2609" , "ErrorCause" : "CAUSE Y_2609" , "Resolution" : "PERFORM Y_2609" , "TechnicalDat" : { "Java Y_2609_1" : 203 , "Java Y_2609_2" : 102}}

然而,简单的文本搜索不显示任何输出!!

BasicDBObject textSearch = new BasicDBObject("$text", "ERROR X_2609");
BasicDBObject search = new BasicDBObject("$search", textSearch);
System.out.println(coll_m.find(search));

打印以下行:

Cursor id=0, ns=atlascompare.CollRMOB, query={ "$search" : { "$text" : "ERROR X_2609"}}, numIterated=0, readPreference=primary

有人可以指导我做错了吗?!

编辑1:此外,我确保索引已经完成。这是打印索引时的输出:

Indexes present ----------------------------
{ "v" : 1 , "key" : { "_id" : 1} , "name" : "_id_" , "ns" : "atlascompare.CollRMOB"}
{ "v" : 1 , "key" : { "_fts" : "text" , "_ftsx" : 1} , "name" : "$**_text" , "ns" : "atlascompare.CollRMOB" , "weights" : { "$**" : 1} , "default_language" : "english" , "language_override" : "language" , "textIndexVersion" : 2}

1 个答案:

答案 0 :(得分:0)

我认为您的查询不正确。

查询应如下所示:(在MongoDB Shell中)

db.CollRMOB.find( { $text : { $search : "ERROR X_2609" } } );

所以在你的Java代码中应该是这样的:

BasicDBObject textSearch = new BasicDBObject("$search", "ERROR X_2609"); BasicDBObject search = new BasicDBObject("$text", textSearch); // the print the documents System.out.println(coll_m.find(search).toArray() );

拖轮