Search in sources :

Example 41 with Contact

use of com.zimbra.cs.mailbox.Contact in project zm-mailbox by Zimbra.

the class AbstractIndexStoreTest method prefixQuery.

@Test
public void prefixQuery() throws Exception {
    ZimbraLog.test.debug("--->TEST prefixQuery");
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    Contact contact1 = createContact(mbox, "a", "bc", "abc@zimbra.com");
    Contact contact2 = createContact(mbox, "a", "bcd", "abcd@zimbra.com");
    createContact(mbox, "x", "Y", "xy@zimbra.com");
    createContact(mbox, "x", "Yz", "x.Yz@zimbra.com");
    // Make sure all indexing has been done
    mbox.index.indexDeferredItems();
    IndexStore index = mbox.index.getIndexStore();
    ZimbraIndexSearcher searcher = index.openSearcher();
    ZimbraTopDocs result = searcher.search(new PrefixQuery(new Term(LuceneFields.L_CONTACT_DATA, "ab")), 100);
    Assert.assertNotNull("searcher.search result object - searching for 'ab' prefix", result);
    ZimbraLog.test.debug("Result for search for 'ab'\n" + result.toString());
    Assert.assertEquals("Number of hits searching for 'ab' prefix", 2, result.getTotalHits());
    String contact1Id = String.valueOf(contact1.getId());
    String contact2Id = String.valueOf(contact2.getId());
    String match1Id = getBlobIdForResultDoc(searcher, result, 0);
    String match2Id = getBlobIdForResultDoc(searcher, result, 1);
    ZimbraLog.test.debug("Contact1ID=%s Contact2ID=%s match1id=%s match2id=%s", contact1Id, contact2Id, match1Id, match2Id);
    if (contact1Id.equals(match1Id)) {
        Assert.assertEquals("2nd match isn't contact2's ID", contact2Id, match2Id);
    } else if (contact1Id.equals(match2Id)) {
        Assert.assertEquals("2nd match isn't contact1's ID", contact2Id, match1Id);
    } else {
        Assert.fail(String.format("Contact 1 ID [%s] doesn't match either [%s] or [%s]", contact1Id, match1Id, match2Id));
    }
}
Also used : Mailbox(com.zimbra.cs.mailbox.Mailbox) PrefixQuery(org.apache.lucene.search.PrefixQuery) Term(org.apache.lucene.index.Term) Contact(com.zimbra.cs.mailbox.Contact) ParsedContact(com.zimbra.cs.mime.ParsedContact) Test(org.junit.Test)

Example 42 with Contact

use of com.zimbra.cs.mailbox.Contact in project zm-mailbox by Zimbra.

the class AbstractIndexStoreTest method filteredTermQuery.

@Test
public void filteredTermQuery() throws Exception {
    ZimbraLog.test.debug("--->TEST filteredTermQuery");
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    Folder folder = mbox.getFolderById(null, Mailbox.ID_FOLDER_CONTACTS);
    Contact contact1 = createContact(mbox, "a", "bc", "abc@zimbra.com");
    Contact contact2 = createContact(mbox, "a", "bcd", "abcd@zimbra.com");
    Contact contact3 = createContact(mbox, "x", "y", "xy@zimbra.com");
    Contact contact4 = createContact(mbox, "x", "yz", "xyz@zimbra.com");
    Contact contact5 = createContact(mbox, "x", "yz", "xyz@zimbra.com");
    // Make sure we don't index items after the deleteIndex() below
    mbox.index.indexDeferredItems();
    IndexStore index = mbox.index.getIndexStore();
    index.deleteIndex();
    Indexer indexer = index.openIndexer();
    indexer.addDocument(folder, contact1, contact1.generateIndexData());
    indexer.addDocument(folder, contact2, contact2.generateIndexData());
    indexer.addDocument(folder, contact3, contact3.generateIndexData());
    indexer.addDocument(folder, contact4, contact4.generateIndexData());
    // Note: NOT indexed contact5
    indexer.close();
    List<Term> terms = Lists.newArrayList();
    terms.add(new Term(LuceneFields.L_MAILBOX_BLOB_ID, String.valueOf(contact2.getId())));
    terms.add(new Term(LuceneFields.L_MAILBOX_BLOB_ID, String.valueOf(contact4.getId())));
    terms.add(new Term(LuceneFields.L_MAILBOX_BLOB_ID, String.valueOf(contact5.getId())));
    ZimbraTermsFilter filter = new ZimbraTermsFilter(terms);
    ZimbraIndexSearcher searcher = index.openSearcher();
    ZimbraTopDocs result;
    result = searcher.search(new TermQuery(new Term(LuceneFields.L_CONTACT_DATA, "zimbra.com")), filter, 100);
    Assert.assertNotNull("searcher.search result object - searching for zimbra.com", result);
    ZimbraLog.test.debug("Result for search for 'zimbra.com', filtering by IDs\n%s", result.toString());
    Assert.assertEquals("Number of hits", 2, result.getTotalHits());
    List<String> expecteds = Lists.newArrayList();
    List<String> matches = Lists.newArrayList();
    matches.add(getBlobIdForResultDoc(searcher, result, 0));
    matches.add(getBlobIdForResultDoc(searcher, result, 1));
    expecteds.add(String.valueOf(contact2.getId()));
    expecteds.add(String.valueOf(contact4.getId()));
    Collections.sort(matches);
    Collections.sort(expecteds);
    Assert.assertEquals("Match Blob ID", expecteds.get(0), matches.get(0));
    Assert.assertEquals("Match Blob ID", expecteds.get(1), matches.get(1));
    searcher.close();
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) Term(org.apache.lucene.index.Term) Folder(com.zimbra.cs.mailbox.Folder) Contact(com.zimbra.cs.mailbox.Contact) ParsedContact(com.zimbra.cs.mime.ParsedContact) Mailbox(com.zimbra.cs.mailbox.Mailbox) Test(org.junit.Test)

Example 43 with Contact

use of com.zimbra.cs.mailbox.Contact in project zm-mailbox by Zimbra.

the class AbstractIndexStoreTest method phraseQuery.

@Test
public void phraseQuery() throws Exception {
    ZimbraLog.test.debug("--->TEST phraseQuery");
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    createContact(mbox, "Non", "Match", "nOn.MaTchiNg@zimbra.com");
    Contact contact2 = createContact(mbox, "First", "Last", "f.last@zimbra.com", "Software Development Engineer");
    createContact(mbox, "Given", "Surname", "GiV.SurN@zimbra.com");
    // Make sure all indexing has been done
    mbox.index.indexDeferredItems();
    IndexStore index = mbox.index.getIndexStore();
    ZimbraIndexSearcher searcher = index.openSearcher();
    PhraseQuery pquery = new PhraseQuery();
    // Lower case required for each term for Lucene
    pquery.add(new Term(LuceneFields.L_CONTENT, "software"));
    pquery.add(new Term(LuceneFields.L_CONTENT, "development"));
    pquery.add(new Term(LuceneFields.L_CONTENT, "engineer"));
    ZimbraTopDocs result = searcher.search(pquery, 100);
    Assert.assertNotNull("searcher.search result object", result);
    ZimbraLog.test.debug("Result for search [hits=%d]:%s", result.getTotalHits(), result.toString());
    Assert.assertEquals("Number of hits", 1, result.getTotalHits());
    String expected1Id = String.valueOf(contact2.getId());
    String match1Id = getBlobIdForResultDoc(searcher, result, 0);
    Assert.assertEquals("Mailbox Blob ID of match", expected1Id, match1Id);
    pquery = new PhraseQuery();
    // Try again with words out of order
    pquery.add(new Term(LuceneFields.L_CONTENT, "development"));
    pquery.add(new Term(LuceneFields.L_CONTENT, "software"));
    pquery.add(new Term(LuceneFields.L_CONTENT, "engineer"));
    result = searcher.search(pquery, 100);
    Assert.assertNotNull("searcher.search result object", result);
    ZimbraLog.test.debug("Result for search [hits=%d]:%s", result.getTotalHits(), result.toString());
    Assert.assertEquals("Number of hits", 0, result.getTotalHits());
}
Also used : Mailbox(com.zimbra.cs.mailbox.Mailbox) PhraseQuery(org.apache.lucene.search.PhraseQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) Term(org.apache.lucene.index.Term) Contact(com.zimbra.cs.mailbox.Contact) ParsedContact(com.zimbra.cs.mime.ParsedContact) Test(org.junit.Test)

Example 44 with Contact

use of com.zimbra.cs.mailbox.Contact in project zm-mailbox by Zimbra.

the class AbstractIndexStoreTest method sortedFilteredTermQuery.

@Test
public void sortedFilteredTermQuery() throws Exception {
    ZimbraLog.test.debug("--->TEST sortedFilteredTermQuery");
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    Folder folder = mbox.getFolderById(null, Mailbox.ID_FOLDER_CONTACTS);
    Contact con1 = createContact(mbox, "a", "bc", "abc@zimbra.com");
    Contact con2 = createContact(mbox, "abcd@zimbra.com");
    Contact con3 = createContact(mbox, "xy@zimbra.com");
    // To ensure different sort date
    Thread.sleep(1001);
    Contact con4 = createContact(mbox, "xyz@zimbra.com");
    Contact con5 = createContact(mbox, "xyz@zimbra.com");
    // Make sure we don't index items after the deleteIndex() below
    mbox.index.indexDeferredItems();
    IndexStore index = mbox.index.getIndexStore();
    index.deleteIndex();
    Indexer indexer = index.openIndexer();
    indexer.addDocument(folder, con1, con1.generateIndexData());
    indexer.addDocument(folder, con2, con2.generateIndexData());
    indexer.addDocument(folder, con3, con3.generateIndexData());
    indexer.addDocument(folder, con4, con4.generateIndexData());
    // Note: NOT indexed contact5
    indexer.close();
    List<Term> terms = Lists.newArrayList();
    terms.add(new Term(LuceneFields.L_MAILBOX_BLOB_ID, String.valueOf(con2.getId())));
    terms.add(new Term(LuceneFields.L_MAILBOX_BLOB_ID, String.valueOf(con4.getId())));
    terms.add(new Term(LuceneFields.L_MAILBOX_BLOB_ID, String.valueOf(con5.getId())));
    ZimbraTermsFilter filter = new ZimbraTermsFilter(terms);
    ZimbraIndexSearcher srchr = index.openSearcher();
    ZimbraTopDocs result;
    Sort sort = new Sort(new SortField(LuceneFields.L_SORT_DATE, SortField.STRING, false));
    result = srchr.search(new TermQuery(new Term(LuceneFields.L_CONTACT_DATA, "zimbra.com")), filter, 100, sort);
    Assert.assertNotNull("searcher.search result object - searching for zimbra.com", result);
    ZimbraLog.test.debug("Result for search for 'zimbra.com', filtering by IDs 2,4 & 5\n%s", result.toString());
    Assert.assertEquals("Number of hits", 2, result.getTotalHits());
    Assert.assertEquals("Match Blob ID 1", String.valueOf(con2.getId()), getBlobIdForResultDoc(srchr, result, 0));
    Assert.assertEquals("Match Blob ID 2", String.valueOf(con4.getId()), getBlobIdForResultDoc(srchr, result, 1));
    // Repeat but with a reverse sort this time
    sort = new Sort(new SortField(LuceneFields.L_SORT_DATE, SortField.STRING, true));
    result = srchr.search(new TermQuery(new Term(LuceneFields.L_CONTACT_DATA, "zimbra.com")), filter, 100, sort);
    Assert.assertNotNull("searcher.search result object - searching for zimbra.com", result);
    ZimbraLog.test.debug("Result for search for 'zimbra.com' sorted reverse, filter by IDs\n%s", result.toString());
    Assert.assertEquals("Number of hits", 2, result.getTotalHits());
    Assert.assertEquals("Match Blob ID 1", String.valueOf(con4.getId()), getBlobIdForResultDoc(srchr, result, 0));
    Assert.assertEquals("Match Blob ID 2", String.valueOf(con2.getId()), getBlobIdForResultDoc(srchr, result, 1));
    srchr.close();
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) SortField(org.apache.lucene.search.SortField) Term(org.apache.lucene.index.Term) Folder(com.zimbra.cs.mailbox.Folder) Contact(com.zimbra.cs.mailbox.Contact) ParsedContact(com.zimbra.cs.mime.ParsedContact) Mailbox(com.zimbra.cs.mailbox.Mailbox) Sort(org.apache.lucene.search.Sort) Test(org.junit.Test)

Example 45 with Contact

use of com.zimbra.cs.mailbox.Contact in project zm-mailbox by Zimbra.

the class ContactQueryTest method search.

@Ignore
public void search() throws Exception {
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    Map<String, Object> fields = new HashMap<String, Object>();
    fields.put(ContactConstants.A_firstName, "Michael");
    fields.put(ContactConstants.A_lastName, "Smith");
    fields.put(ContactConstants.A_email, "michael.smith@zimbra.com");
    mbox.createContact(null, new ParsedContact(fields), Mailbox.ID_FOLDER_CONTACTS, null);
    fields.put(ContactConstants.A_firstName, "Jonathan");
    fields.put(ContactConstants.A_lastName, "Smith");
    fields.put(ContactConstants.A_email, "jonathan.smith@zimbra.com");
    Contact contact = mbox.createContact(null, new ParsedContact(fields), Mailbox.ID_FOLDER_CONTACTS, null);
    ZimbraQueryResults results = mbox.index.search(new OperationContext(mbox), "contact:\"Jon Smith\"", EnumSet.of(MailItem.Type.CONTACT), SortBy.NONE, 100);
    Assert.assertTrue("Expected some hits", results.hasNext());
    Assert.assertEquals("Hit ItemId not as expected", contact.getId(), results.getNext().getItemId());
    results.close();
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) ParsedContact(com.zimbra.cs.mime.ParsedContact) Mailbox(com.zimbra.cs.mailbox.Mailbox) HashMap(java.util.HashMap) ZimbraQueryResults(com.zimbra.cs.index.ZimbraQueryResults) Contact(com.zimbra.cs.mailbox.Contact) ParsedContact(com.zimbra.cs.mime.ParsedContact) Ignore(org.junit.Ignore)

Aggregations

Contact (com.zimbra.cs.mailbox.Contact)52 Mailbox (com.zimbra.cs.mailbox.Mailbox)32 ParsedContact (com.zimbra.cs.mime.ParsedContact)26 Test (org.junit.Test)23 OperationContext (com.zimbra.cs.mailbox.OperationContext)13 HashMap (java.util.HashMap)13 ZMailbox (com.zimbra.client.ZMailbox)10 ContactGroup (com.zimbra.cs.mailbox.ContactGroup)10 MailItem (com.zimbra.cs.mailbox.MailItem)10 ItemId (com.zimbra.cs.service.util.ItemId)10 ServiceException (com.zimbra.common.service.ServiceException)9 ArrayList (java.util.ArrayList)9 ZContact (com.zimbra.client.ZContact)8 Folder (com.zimbra.cs.mailbox.Folder)7 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)7 Term (org.apache.lucene.index.Term)7 Element (com.zimbra.common.soap.Element)6 Account (com.zimbra.cs.account.Account)6 Message (com.zimbra.cs.mailbox.Message)6 List (java.util.List)6