0

I am using MongoDB with Java Driver (http://tinyurl.com/dyjxz8k). In my application I want it to be possible to give results that contains a substring of the users search-term. The method looks like this:

*searchlabel = the name of a field *searchTerm = the users searchword

 private void dbSearch(String searchlabel, String searchTerm){
    if(searchTerm != null && (searchTerm.length() > 0)){

        DBCollection coll = db.getCollection("MediaCollection");
        BasicDBObject query = new BasicDBObject(searchlabel, searchTerm);
        DBCursor cursor = coll.find();
        cursor = coll.find(query);

        try {
            while(cursor.hasNext()) {
                System.out.println(cursor.next());
                //view.showResult(cursor.next());
            }
        } finally {
            cursor.close();
        }

    }
}

Does anybody have any idea about how I can solve this? Thanks in advance =) And a small additional question: How can I handle the DBObjects according to presentation in (a JLabel in) view?

2 Answers2

0

For text-searching in Mongo, there are two options:

  • $regex operator - however unless you have a simple prefix regexp, queries won't use an index, and will result in a full scan, which usually is slow
  • In Mongo 2.4, a new text index has been introduced. A text query will split your query into words, and do an or-search for documents including any of the words. Text indexes also eliminate some stop-words and have simple stemming for some languages (see the docs).

If you are looking for a more advanced full-text search engine, with more powerful tokenising, stemming, autocomplete etc., maybe a better fit would be e.g. ElasticSearch.

adamw
  • 8,038
  • 4
  • 28
  • 32
  • do you know exactly, what type of the structure used? may be there complex array of objects with weights ( like this: [{kword:[tag1, tag2, ...], weight1}, {...}] ) – Vladimir Muzhilov Apr 24 '13 at 08:31
  • I this the regex will be the simplest one for now. I just need it to work properly. I tried this code, but it would not work. Is it all wrong written? BasicDBObject query = new BasicDBObject(searchlabel, new BasicDBObject(“$regex”, searchTerm ).append(“$options”, “i”)); – Maiken Beate Fjellanger Apr 24 '13 at 17:53
  • @VladimirMuzhilov true, if there are weights etc, then I don't think Mongo can handle that. – adamw Apr 24 '13 at 18:08
  • @MaikenBeateFjellanger well the serach term doesn't have to be a regexp and even if it so happens, it will only look for an exact match – adamw Apr 24 '13 at 18:09
  • Do you have an example of how I can write it? – Maiken Beate Fjellanger Apr 24 '13 at 18:33
  • 1
    If you want to look for whole words, maybe split the user query into words (on whitespace), and create an or-query for the searchlabel to match any of the words, case-insensitive? – adamw Apr 25 '13 at 07:44
0

I use this method in the mongo console to search with a regular expression in JavaScript:

// My name to search for
var searchWord = "alex";

// Construct a query with a simple /^alex$/i regex
var query = {};
query.animalName = new RegExp("^"+searchWord+"$","i");

// Perform find operation
var lionsNamedAlex = db.lions.find(query);
Niels Castle
  • 8,039
  • 35
  • 56