Pregunta

Hi my Mongo db fields as follows:

        "_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"]

below is my code to do full text search on indexed fields

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"));
}

I have used below command to create Text index on two fields:

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

I have created text index on filename and keywords but i can able to do full text search on filename only, is there any wrong in my code, please help me

¿Fue útil?

Solución

The second parameter on ensureIndex function is options. You should create an unique index on both fields:

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

See more:

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

Otros consejos

Another way to create an unique index on both fields :

BasicDBObject basicDBObject = new BasicDBObject();
basicDBObject.append("Keywords", "text");
basicDBObject.append("Filename", "text");
dbCollection.createIndex(basicDBObject);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top