0

Index for array of (key, value) pairs can be done using dot operator and even Search is possible for this.

Could any one please tell me how to :

"Index" and "Search" basic array type in MongoDB using Java. Suppose if I want to search "Hard Working" in all the documents in the collection.

For example :

"key_strengths" : [ 
                "Self Motivated", 
                "Innovative", 
                "Hard Working"
              ]

I referred this: http://docs.mongodb.org/manual/core/index-multikey/#index-type-multikey But still I could not do Index and Search for the above example.

    BasicDBObject index = new BasicDBObject();
        index.put("key_strengths" , 1);

     collection.ensureIndex( index, "TextIndex");

The above Java code created an index for "key_strengths".

        BasicDBObject search = new BasicDBObject("$search", "\"Hard Working\""); 
        BasicDBObject textSearch = new BasicDBObject("$text", search);

        DBCursor cursor = users.find(textSearch);

The above is the code which I used for text search. This did not give out any search result (There is no error also). Question is :

1. Is it not possible for an array type to be searched using full text search in MongoDB??

2. If it is possible what changes should I do for the above Java code? Please help me out.

Thank you.

1 Answer 1

0

Here is what you can do to see if the index has an effect. Look at the explain output if the index was used:

> db.test.count()
  3
> db.test.find({key_strengths:"Hard Working"}).explain()
{
        "cursor" : "BasicCursor",
        "n" : 2,
        "nscannedObjects" : 3,
        "nscannedObjectsAllPlans" : 3,
        ...
}
>

You see that all three documents had to be scanned, because there is no index (BasicCursor). We create an index.

> db.test.ensureIndex({key_strengths:1})

And now the query should hit the index:

> db.test.find({key_strengths:"Hard Working"}).explain()
{
        "cursor" : "BtreeCursor key_strengths_1",
        "isMultiKey" : true,
        "n" : 2,
        "nscannedObjects" : 2,
        "nscanned" : 2,
        ...
        "indexBounds" : {
                "key_strengths" : [
                        [
                                "Hard Working",
                                "Hard Working"
                        ]
                ]
        },
        ...

}
>

As you see only two documents now were examined because there exist index entries for those documents (key_strengths_1).

To perform search you can actually use regexp:

db.test.find({key_strengths:/Hard Working/i})

In java something like this:

BasicDBObject query = new BasicDBObject();
query.put("key_strengths",  java.util.regex.Pattern.compile("Hard Working", java.util.regex.Pattern.CASE_INSENSITIVE));
users.find(query);

Text search

You can also enable text search on all the fields of documents in your collection (in Java):

collection.createIndex(new BasicDBObject("$**","text"),new BasicDBObject("name","TextIndex"));

Searching:

BasicDBObject query = new BasicDBObject();
query.put("$text",new BasicDBObject("$search","Hard Working"));
DBCursor cursor = users.find(query);
Sign up to request clarification or add additional context in comments.

11 Comments

I tried as u suggested and it worked as u have mentioned.. Thank you. But I have another problem.
Now I have posted a question
Yes I deleted my answer.
#naimdjon I have tried with regex. It worked. The problem I'm facing is search_keyword should be searched in all the indexes created for a particular collection. This didn't work. BasicDBObject search = new BasicDBObject("$search", java.util.regex.Pattern.compile("java", java.util.regex.Pattern.CASE_INSENSITIVE)); BasicDBObject textSearch = new BasicDBObject("$text", search);
@user3805045 updated the answer, look at the bottom for an update.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.