james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nor...@apache.org
Subject svn commit: r1130810 - in /james/mailbox/trunk/store/src: main/java/org/apache/james/mailbox/store/search/lucene/LuceneMessageSearchIndex.java test/java/org/apache/james/mailbox/store/lucene/LuceneMessageSearchIndexTest.java
Date Thu, 02 Jun 2011 20:57:47 GMT
Author: norman
Date: Thu Jun  2 20:57:47 2011
New Revision: 1130810

URL: http://svn.apache.org/viewvc?rev=1130810&view=rev
Log:
Fix NOT Searches when using LuceneMessageSearchIndex. Also fix some incorrect tests. See MAILBOX-10

Modified:
    james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/lucene/LuceneMessageSearchIndex.java
    james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/lucene/LuceneMessageSearchIndexTest.java

Modified: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/lucene/LuceneMessageSearchIndex.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/lucene/LuceneMessageSearchIndex.java?rev=1130810&r1=1130809&r2=1130810&view=diff
==============================================================================
--- james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/lucene/LuceneMessageSearchIndex.java
(original)
+++ james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/lucene/LuceneMessageSearchIndex.java
Thu Jun  2 20:57:47 2011
@@ -26,9 +26,11 @@ import java.nio.charset.Charset;
 import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Date;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Locale;
+import java.util.Set;
 
 import javax.mail.Flags;
 import javax.mail.Flags.Flag;
@@ -38,6 +40,7 @@ import org.apache.james.mailbox.MailboxE
 import org.apache.james.mailbox.MailboxSession;
 import org.apache.james.mailbox.MessageRange;
 import org.apache.james.mailbox.SearchQuery;
+import org.apache.james.mailbox.SearchQuery.Conjunction;
 import org.apache.james.mailbox.UnsupportedSearchException;
 import org.apache.james.mailbox.SearchQuery.AllCriterion;
 import org.apache.james.mailbox.SearchQuery.ContainsOperator;
@@ -283,7 +286,7 @@ public class LuceneMessageSearchIndex<Id
      * @see org.apache.james.mailbox.store.MessageSearchIndex#search(org.apache.james.mailbox.MailboxSession,
org.apache.james.mailbox.store.mail.model.Mailbox, org.apache.james.mailbox.SearchQuery)
      */
     public Iterator<Long> search(MailboxSession session, Mailbox<Id> mailbox,
SearchQuery searchQuery) throws MailboxException {
-        List<Long> uids = new ArrayList<Long>();
+        Set<Long> uids = new HashSet<Long>();
         IndexSearcher searcher = null;
 
         try {
@@ -295,9 +298,7 @@ public class LuceneMessageSearchIndex<Id
             for (int i = 0; i < crits.size(); i++) {
                 query.add(createQuery(crits.get(i), mailbox), BooleanClause.Occur.MUST);
             }
-            
-            //System.out.println(query.toString());
-            
+                      
             // query for all the documents sorted by uid
             TopDocs docs = searcher.search(query, null, maxQueryResults, UID_SORT);
             ScoreDoc[] sDocs = docs.scoreDocs;
@@ -573,13 +574,17 @@ public class LuceneMessageSearchIndex<Id
      */
     private Query createUidQuery(SearchQuery.UidCriterion crit) throws UnsupportedSearchException
{
         NumericRange[] ranges = crit.getOperator().getRange();
-        BooleanQuery rangesQuery = new BooleanQuery();
-        for (int i = 0; i < ranges.length; i++) {
-            NumericRange range = ranges[i];
-            rangesQuery.add(NumericRangeQuery.newLongRange(UID_FIELD, range.getLowValue(),
range.getHighValue(), true, true), BooleanClause.Occur.SHOULD);
-        }        
-        rangesQuery.add(new PrefixQuery(new Term(FLAGS_FIELD, "")), BooleanClause.Occur.MUST_NOT);
-        return rangesQuery;
+        if (ranges.length == 1) {
+            NumericRange range = ranges[0];
+            return NumericRangeQuery.newLongRange(UID_FIELD, range.getLowValue(), range.getHighValue(),
true, true);
+        } else {
+            BooleanQuery rangesQuery = new BooleanQuery();
+            for (int i = 0; i < ranges.length; i++) {
+                NumericRange range = ranges[i];
+                rangesQuery.add(NumericRangeQuery.newLongRange(UID_FIELD, range.getLowValue(),
range.getHighValue(), true, true), BooleanClause.Occur.SHOULD);
+            }        
+            return rangesQuery;
+        }
     }
     
     
@@ -753,6 +758,9 @@ public class LuceneMessageSearchIndex<Id
         for (int i = 0; i < crits.size(); i++) {
             conQuery.add(createQuery(crits.get(i), mailbox), occur);
         }
+        if (Conjunction.NOR.equals(crit.getType())) {
+            conQuery.add(new TermQuery(new Term(MAILBOX_ID_FIELD, mailbox.getMailboxId().toString())),
BooleanClause.Occur.MUST);
+        }
         return conQuery;
     }
     

Modified: james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/lucene/LuceneMessageSearchIndexTest.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/lucene/LuceneMessageSearchIndexTest.java?rev=1130810&r1=1130809&r2=1130810&view=diff
==============================================================================
--- james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/lucene/LuceneMessageSearchIndexTest.java
(original)
+++ james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/lucene/LuceneMessageSearchIndexTest.java
Thu Jun  2 20:57:47 2011
@@ -124,7 +124,7 @@ public class LuceneMessageSearchIndexTes
         SearchQuery query = new SearchQuery();
         query.andCriteria(SearchQuery.bodyContains(CUSTARD));
         Iterator<Long> result = index.search(null, mailbox3, query);
-        assertEquals(10, result.next(), 1);
+        assertEquals(10L, result.next().longValue());
         assertFalse(result.hasNext());
         
         
@@ -139,7 +139,7 @@ public class LuceneMessageSearchIndexTes
         SearchQuery query = new SearchQuery();
         query.andCriteria(SearchQuery.bodyContains(RHUBARD));
         Iterator<Long> result = index.search(null, mailbox3, query);
-        assertEquals(10, result.next(), 1);
+        assertEquals(10L, result.next().longValue());
         assertFalse(result.hasNext());
     }
 
@@ -161,7 +161,7 @@ public class LuceneMessageSearchIndexTes
         SearchQuery query = new SearchQuery();
         query.andCriteria(SearchQuery.mailContains(CUSTARD));
         Iterator<Long> result = index.search(null, mailbox3, query);
-        assertEquals(10, result.next(), 1);
+        assertEquals(10L, result.next().longValue());
         assertFalse(result.hasNext());
         
         query = new SearchQuery();
@@ -175,12 +175,12 @@ public class LuceneMessageSearchIndexTes
         SearchQuery query = new SearchQuery();
         query.andCriteria(SearchQuery.mailContains(RHUBARD));
         Iterator<Long> result = index.search(null, mailbox3, query);
-        assertEquals(10, result.next(), 1);
+        assertEquals(10L, result.next().longValue());
         assertFalse(result.hasNext());
         
         query.andCriteria(SearchQuery.mailContains(RHUBARD.toLowerCase(Locale.US)));
         result = index.search(null, mailbox3, query);
-        assertEquals(10, result.next(), 1);
+        assertEquals(10L, result.next().longValue());
         assertFalse(result.hasNext());
     }
 
@@ -190,12 +190,12 @@ public class LuceneMessageSearchIndexTes
         SearchQuery query = new SearchQuery();
         query.andCriteria(SearchQuery.mailContains(FROM_ADDRESS));
         Iterator<Long> result = index.search(null, mailbox3, query);
-        assertEquals(10, result.next(), 1);
+        assertEquals(10L, result.next().longValue());
         assertFalse(result.hasNext());
         
         query.andCriteria(SearchQuery.mailContains(SUBJECT_PART));
         result = index.search(null, mailbox3, query);
-        assertEquals(10, result.next(), 1);
+        assertEquals(10L, result.next().longValue());
         assertFalse(result.hasNext());
     }
     
@@ -205,7 +205,7 @@ public class LuceneMessageSearchIndexTes
         query.andCriteria(SearchQuery.all());
         Iterator<Long> it2 = index.search(null, mailbox2, query);
         assertTrue(it2.hasNext());
-        assertEquals(1, it2.next().longValue(), 1);
+        assertEquals(1L, it2.next().longValue());
         assertFalse(it2.hasNext());
     }
     
@@ -215,8 +215,8 @@ public class LuceneMessageSearchIndexTes
         SearchQuery q = new SearchQuery();
         q.andCriteria(SearchQuery.flagIsSet(Flag.DELETED));
         Iterator<Long> it3 = index.search(null, mailbox, q);
-        assertEquals(3, it3.next().longValue(), 1);
-        assertEquals(4, it3.next().longValue(), 1);
+        assertEquals(2L, it3.next().longValue());
+        assertEquals(3L, it3.next().longValue());
         assertFalse(it3.hasNext());
     }
     
@@ -225,9 +225,9 @@ public class LuceneMessageSearchIndexTes
         SearchQuery q2 = new SearchQuery();
         q2.andCriteria(SearchQuery.bodyContains("body"));
         Iterator<Long> it4 = index.search(null, mailbox, q2);
-        assertEquals(1, it4.next().longValue(), 1);
-        assertEquals(2, it4.next().longValue(), 1);
-        assertEquals(3, it4.next().longValue(), 1);
+        assertEquals(1L, it4.next().longValue());
+        assertEquals(2L, it4.next().longValue());
+        assertEquals(3L, it4.next().longValue());
 
         assertFalse(it4.hasNext());
     }
@@ -237,9 +237,9 @@ public class LuceneMessageSearchIndexTes
         SearchQuery q2 = new SearchQuery();
         q2.andCriteria(SearchQuery.mailContains("body"));
         Iterator<Long> it4 = index.search(null, mailbox, q2);
-        assertEquals(1, it4.next().longValue(), 1);
-        assertEquals(2, it4.next().longValue(), 1);
-        assertEquals(3, it4.next().longValue(), 1);
+        assertEquals(1L, it4.next().longValue());
+        assertEquals(2L, it4.next().longValue());
+        assertEquals(3L, it4.next().longValue());
 
         assertFalse(it4.hasNext());
     }
@@ -249,8 +249,8 @@ public class LuceneMessageSearchIndexTes
         SearchQuery q2 = new SearchQuery();
         q2.andCriteria(SearchQuery.headerContains("Subject", "test"));
         Iterator<Long> it4 = index.search(null, mailbox, q2);
-        assertEquals(1, it4.next().longValue(), 1);
-        assertEquals(2, it4.next().longValue(), 1);
+        assertEquals(1L, it4.next().longValue());
+        assertEquals(3L, it4.next().longValue());
 
         assertFalse(it4.hasNext());
     }
@@ -260,8 +260,8 @@ public class LuceneMessageSearchIndexTes
         SearchQuery q2 = new SearchQuery();
         q2.andCriteria(SearchQuery.headerExists("Subject"));
         Iterator<Long> it4 = index.search(null, mailbox, q2);
-        assertEquals(1, it4.next().longValue(), 1);
-        assertEquals(3, it4.next().longValue(), 1);
+        assertEquals(1L, it4.next().longValue());
+        assertEquals(3L, it4.next().longValue());
 
         assertFalse(it4.hasNext());
     }
@@ -271,9 +271,9 @@ public class LuceneMessageSearchIndexTes
         SearchQuery q2 = new SearchQuery();
         q2.andCriteria(SearchQuery.flagIsUnSet(Flag.DRAFT));
         Iterator<Long> it4 = index.search(null, mailbox, q2);
-        assertEquals(1, it4.next().longValue(), 1);
-        assertEquals(2, it4.next().longValue(), 1);
-        assertEquals(3, it4.next().longValue(), 1);
+        assertEquals(1L, it4.next().longValue());
+        assertEquals(2L, it4.next().longValue());
+        assertEquals(3L, it4.next().longValue());
 
         assertFalse(it4.hasNext());
     }
@@ -287,7 +287,7 @@ public class LuceneMessageSearchIndexTes
         q2.andCriteria(SearchQuery.internalDateBefore(cal.getTime(), DateResolution.Day));
         
         Iterator<Long> it4 = index.search(null, mailbox, q2);
-        assertEquals(2, it4.next().longValue(), 1);
+        assertEquals(2L, it4.next().longValue());
         assertFalse(it4.hasNext());
     }
     
@@ -299,7 +299,7 @@ public class LuceneMessageSearchIndexTes
         cal.setTime(new Date());
         q2.andCriteria(SearchQuery.internalDateAfter(cal.getTime(), DateResolution.Day));
         Iterator<Long> it4 = index.search(null, mailbox, q2);
-        assertEquals(3, it4.next().longValue(), 1);
+        assertEquals(3L, it4.next().longValue());
         assertFalse(it4.hasNext());
     }
     
@@ -312,7 +312,7 @@ public class LuceneMessageSearchIndexTes
         cal.setTime(new Date());
         q2.andCriteria(SearchQuery.internalDateOn(cal.getTime(), DateResolution.Day));
         Iterator<Long> it4 = index.search(null, mailbox, q2);
-        assertEquals(1, it4.next().longValue(), 1);
+        assertEquals(1L, it4.next().longValue());
         assertFalse(it4.hasNext());
     }
     
@@ -323,7 +323,7 @@ public class LuceneMessageSearchIndexTes
         cal.setTime(new Date());
         q2.andCriteria(SearchQuery.uid(new SearchQuery.NumericRange[] {new SearchQuery.NumericRange(1)}));
         Iterator<Long> it4 = index.search(null, mailbox, q2);
-        assertEquals(1, it4.next(), 1);
+        assertEquals(1L, it4.next().longValue());
         assertFalse(it4.hasNext());
     }
     
@@ -335,9 +335,9 @@ public class LuceneMessageSearchIndexTes
         cal.setTime(new Date());
         q2.andCriteria(SearchQuery.uid(new SearchQuery.NumericRange[] {new SearchQuery.NumericRange(1),
new SearchQuery.NumericRange(2,3)}));
         Iterator<Long> it4 = index.search(null, mailbox, q2);
-        assertEquals(1, it4.next().longValue(), 1);
-        assertEquals(2, it4.next().longValue(), 1);
-        assertEquals(3, it4.next().longValue(), 1);
+        assertEquals(1L, it4.next().longValue());
+        assertEquals(2L, it4.next().longValue());
+        assertEquals(3L, it4.next().longValue());
 
         assertFalse(it4.hasNext());
     }
@@ -349,7 +349,7 @@ public class LuceneMessageSearchIndexTes
         SearchQuery q2 = new SearchQuery();
         q2.andCriteria(SearchQuery.sizeEquals(200));
         Iterator<Long> it4 = index.search(null, mailbox, q2);
-        assertEquals(1, it4.next().longValue(), 1);
+        assertEquals(1L, it4.next().longValue());
 
         assertFalse(it4.hasNext());
     }
@@ -359,8 +359,8 @@ public class LuceneMessageSearchIndexTes
         SearchQuery q2 = new SearchQuery();
         q2.andCriteria(SearchQuery.sizeLessThan(200));
         Iterator<Long> it4 = index.search(null, mailbox, q2);
-        assertEquals(2, it4.next().longValue(), 1);
-        assertEquals(3, it4.next().longValue(), 1);
+        assertEquals(2L, it4.next().longValue());
+        assertEquals(3L, it4.next().longValue());
 
         assertFalse(it4.hasNext());
     }
@@ -371,12 +371,23 @@ public class LuceneMessageSearchIndexTes
         SearchQuery q2 = new SearchQuery();
         q2.andCriteria(SearchQuery.sizeGreaterThan(6));
         Iterator<Long> it4 = index.search(null, mailbox, q2);
-        assertEquals(1, it4.next().longValue(), 1);
-        assertEquals(2, it4.next().longValue(), 1);
-        assertEquals(3, it4.next().longValue(), 1);
+        assertEquals(1L, it4.next().longValue());
+        assertEquals(2L, it4.next().longValue());
+        assertEquals(3L, it4.next().longValue());
 
         assertFalse(it4.hasNext());
     }
+    
+    @Test
+    public void testNot() throws Exception {
+        SearchQuery q2 = new SearchQuery();
+        q2.andCriteria(SearchQuery.not(SearchQuery.uid(new SearchQuery.NumericRange[] { new
SearchQuery.NumericRange(1)})));
+        Iterator<Long> it4 = index.search(null, mailbox, q2);
+        assertEquals(2L, it4.next().longValue());
+        assertEquals(3L, it4.next().longValue());
+        assertFalse(it4.hasNext());
+    }
+    
     private final class SimpleMailbox implements Mailbox<Long> {
         private long id;
 



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message