Search in sources :

Example 36 with Contact

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

the class UserServletUtil method populateContactFields.

public static void populateContactFields(Iterator<? extends MailItem> contacts, Mailbox mbox, OperationContext octxt, ArrayList<Map<String, String>> allContacts, HashSet<String> fields) throws ServiceException {
    while (contacts.hasNext()) {
        Object obj = contacts.next();
        if (obj instanceof Contact) {
            Contact c = (Contact) obj;
            if (c.isContactGroup()) {
                HashMap<String, String> nContacts = new HashMap<String, String>();
                // first add all the fields and values
                nContacts.putAll(c.getFields());
                // remove groupMemeber
                nContacts.remove(ContactConstants.A_groupMember);
                // then re-calculate the dlist as in 7.X
                ContactGroup cg = ContactGroup.init(c, false);
                String strs = cg.migrateToDlist(mbox, octxt);
                nContacts.put(ContactConstants.A_dlist, strs);
                allContacts.add(nContacts);
                fields.addAll(nContacts.keySet());
            } else {
                allContacts.add(c.getFields());
                fields.addAll(c.getFields().keySet());
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) ContactGroup(com.zimbra.cs.mailbox.ContactGroup) Contact(com.zimbra.cs.mailbox.Contact)

Example 37 with Contact

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

the class AbstractIndexStoreTest method deleteDocument.

@Test
public void deleteDocument() throws Exception {
    ZimbraLog.test.debug("--->TEST deleteDocument");
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    IndexStore index = mbox.index.getIndexStore();
    index.deleteIndex();
    Indexer indexer = index.openIndexer();
    Assert.assertEquals("maxDocs at start", 0, indexer.maxDocs());
    Contact contact1 = createContact(mbox, "James", "Peters", "test1@zimbra.com");
    createContact(mbox, "Emma", "Peters", "test2@zimbra.com");
    // Make sure all indexing has been done
    mbox.index.indexDeferredItems();
    ZimbraIndexSearcher searcher = index.openSearcher();
    Assert.assertEquals("numDocs after 2 adds", 2, searcher.getIndexReader().numDocs());
    ZimbraTopDocs result = searcher.search(new TermQuery(new Term(LuceneFields.L_CONTACT_DATA, "@zimbra.com")), 100);
    Assert.assertNotNull("searcher.search result object - searching for '@zimbra.com'", result);
    ZimbraLog.test.debug("Result for search for '@zimbra.com'\n" + result.toString());
    Assert.assertEquals("Total hits after 2 adds", 2, result.getTotalHits());
    searcher.close();
    indexer = index.openIndexer();
    indexer.deleteDocument(Collections.singletonList(contact1.getId()));
    indexer.close();
    searcher = index.openSearcher();
    Assert.assertEquals("numDocs after 2 adds/1 del", 1, searcher.getIndexReader().numDocs());
    result = searcher.search(new TermQuery(new Term(LuceneFields.L_CONTACT_DATA, "@zimbra.com")), 100);
    Assert.assertNotNull("searcher.search result object after 2 adds/1 del", result);
    ZimbraLog.test.debug("Result for search for '@zimbra.com'\n" + result.toString());
    Assert.assertEquals("Total hits after 2 adds/1 del", 1, result.getTotalHits());
    searcher.close();
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) Mailbox(com.zimbra.cs.mailbox.Mailbox) Term(org.apache.lucene.index.Term) Contact(com.zimbra.cs.mailbox.Contact) ParsedContact(com.zimbra.cs.mime.ParsedContact) Test(org.junit.Test)

Example 38 with Contact

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

the class AbstractIndexStoreTest method booleanQuery.

@Test
public void booleanQuery() throws Exception {
    ZimbraLog.test.debug("--->TEST booleanQuery");
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    Contact contact = 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();
    // This seems to be the supported way of enabling leading wildcard queries
    QueryParser queryParser = new QueryParser(LuceneIndex.VERSION, LuceneFields.L_CONTACT_DATA, new StandardAnalyzer(LuceneIndex.VERSION));
    queryParser.setAllowLeadingWildcard(true);
    Query wquery = queryParser.parse("*irst");
    Query tquery = new TermQuery(new Term(LuceneFields.L_CONTACT_DATA, "absent"));
    Query tquery2 = new TermQuery(new Term(LuceneFields.L_CONTACT_DATA, "Last"));
    BooleanQuery bquery = new BooleanQuery();
    bquery.add(wquery, Occur.MUST);
    bquery.add(tquery, Occur.MUST_NOT);
    bquery.add(tquery2, Occur.SHOULD);
    ZimbraTopDocs result = searcher.search(bquery, 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(contact.getId());
    String match1Id = searcher.doc(result.getScoreDoc(0).getDocumentID()).get(LuceneFields.L_MAILBOX_BLOB_ID);
    Assert.assertEquals("Mailbox Blob ID of match", expected1Id, match1Id);
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) QueryParser(org.apache.lucene.queryParser.QueryParser) Mailbox(com.zimbra.cs.mailbox.Mailbox) Query(org.apache.lucene.search.Query) PhraseQuery(org.apache.lucene.search.PhraseQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Term(org.apache.lucene.index.Term) Contact(com.zimbra.cs.mailbox.Contact) ParsedContact(com.zimbra.cs.mime.ParsedContact) Test(org.junit.Test)

Example 39 with Contact

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

the class AbstractIndexStoreTest method termQuery.

@Test
public void termQuery() throws Exception {
    ZimbraLog.test.debug("--->TEST termQuery");
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    Contact contact = createContact(mbox, "First", "Last", "test@zimbra.com");
    createContact(mbox, "a", "bc", "abc@zimbra.com");
    createContact(mbox, "j", "k", "j.k@zimbra.com");
    createContact(mbox, "Matilda", "Higgs-Bozon", "matilda.higgs.bozon@zimbra.com");
    mbox.index.indexDeferredItems();
    // Stick with just one IndexStore - the one cached in Mailbox:
    // IndexStore index = IndexStore.getFactory().getIndexStore(mbox);
    IndexStore index = mbox.index.getIndexStore();
    ZimbraIndexSearcher searcher = index.openSearcher();
    ZimbraTopDocs result = searcher.search(new TermQuery(new Term(LuceneFields.L_CONTACT_DATA, "none@zimbra.com")), 100);
    Assert.assertNotNull("searcher.search result object - searching for none@zimbra.com", result);
    ZimbraLog.test.debug("Result for search for 'none@zimbra.com'\n" + result.toString());
    Assert.assertEquals("Number of hits searching for none@zimbra.com", 0, result.getTotalHits());
    result = searcher.search(new TermQuery(new Term(LuceneFields.L_CONTACT_DATA, "test@zimbra.com")), 100);
    Assert.assertNotNull("searcher.search result object - searching for test@zimbra.com", result);
    ZimbraLog.test.debug("Result for search for 'test@zimbra.com'\n" + result.toString());
    Assert.assertEquals("Number of hits searching for test@zimbra.com", 1, result.getTotalHits());
    Assert.assertEquals(String.valueOf(contact.getId()), getBlobIdForResultDoc(searcher, result, 0));
    Assert.assertEquals(4, searcher.getIndexReader().numDocs());
    searcher.close();
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) Mailbox(com.zimbra.cs.mailbox.Mailbox) Term(org.apache.lucene.index.Term) Contact(com.zimbra.cs.mailbox.Contact) ParsedContact(com.zimbra.cs.mime.ParsedContact) Test(org.junit.Test)

Example 40 with Contact

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

the class AbstractIndexStoreTest method leadingWildcardQuery.

@Test
public void leadingWildcardQuery() throws Exception {
    ZimbraLog.test.debug("--->TEST leadingWildcardQuery");
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    Contact contact = createContact(mbox, "First", "Last", "f.last@zimbra.com", "Leading Wildcard");
    createContact(mbox, "Grand", "Piano", "grand@vmware.com");
    // Make sure all indexing has been done
    mbox.index.indexDeferredItems();
    IndexStore index = mbox.index.getIndexStore();
    ZimbraIndexSearcher searcher = index.openSearcher();
    // This seems to be the supported way of enabling leading wildcard queries for Lucene
    QueryParser queryParser = new QueryParser(LuceneIndex.VERSION, LuceneFields.L_CONTACT_DATA, new StandardAnalyzer(LuceneIndex.VERSION));
    queryParser.setAllowLeadingWildcard(true);
    Query query = queryParser.parse("*irst");
    ZimbraTopDocs result = searcher.search(query, 100);
    Assert.assertNotNull("searcher.search result object - searching for *irst", result);
    ZimbraLog.test.debug("Result for search for '*irst'\n" + result.toString());
    Assert.assertEquals("Number of hits searching for *irst", 1, result.getTotalHits());
    String expected1Id = String.valueOf(contact.getId());
    String match1Id = searcher.doc(result.getScoreDoc(0).getDocumentID()).get(LuceneFields.L_MAILBOX_BLOB_ID);
    Assert.assertEquals("Mailbox Blob ID of match", expected1Id, match1Id);
}
Also used : QueryParser(org.apache.lucene.queryParser.QueryParser) Mailbox(com.zimbra.cs.mailbox.Mailbox) Query(org.apache.lucene.search.Query) PhraseQuery(org.apache.lucene.search.PhraseQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Contact(com.zimbra.cs.mailbox.Contact) ParsedContact(com.zimbra.cs.mime.ParsedContact) Test(org.junit.Test)

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