1

This is my code:

for (String term : segment.terms) {         
            DBObject clause1 = new BasicDBObject("linkTitles", java.util.regex.Pattern.compile(term));
            or.add(clause1);
}
DBObject mongoQuery = new BasicDBObject("$and", or);        
DBCursor cursor = pageLinks.find(mongoQuery);

The problem with this is that if for example the term is "obama", it wil lgive me objects that has linkTitles like "meprobamate". I want to give a regular expression that only gives me "obama" as a whole word in any situation like " obama ", " obama." " obama, ", etc.

In all of the cases "obama" might be between spaces or any punctuation, how can I say in java code for the mongo db regex to consider any punctuation letter or space around the term?

Andi Keikha
  • 1,246
  • 2
  • 16
  • 37

3 Answers3

1

You can use \b word boundary to do this.. change your line to the following

DBObject clause1 = new BasicDBObject("linkTitles", 
                   java.util.regex.Pattern.compile("\\b"+term+"\\b"));
karthik manchala
  • 13,492
  • 1
  • 31
  • 55
1

If you have text search enabled on your field, try to search them with the text search queries: http://docs.mongodb.org/manual/reference/operator/query/text.

Example: db.articles.find( { $text: { $search: "coffee" } } )
Arash.m.h
  • 320
  • 2
  • 11
1

Somehow you need to wrap the <term> in the word boundary as below:

\\b<term>\\b

where
\b - is a word boundry
MaxZoom
  • 7,619
  • 5
  • 28
  • 44