Search in sources :

Example 6 with ZimbraQueryResults

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

the class TestUtil method search.

/**
     * Searches a mailbox and returns the id's of all matching items.
     */
public static List<Integer> search(Mailbox mbox, String query, Set<MailItem.Type> types) throws ServiceException {
    List<Integer> ids = new ArrayList<Integer>();
    ZimbraQueryResults r = mbox.index.search(new OperationContext(mbox), query, types, SortBy.DATE_DESC, 100);
    while (r.hasNext()) {
        ZimbraHit hit = r.getNext();
        ids.add(new Integer(hit.getItemId()));
    }
    Closeables.closeQuietly(r);
    return ids;
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) ZimbraHit(com.zimbra.cs.index.ZimbraHit) ArrayList(java.util.ArrayList) ZimbraQueryResults(com.zimbra.cs.index.ZimbraQueryResults)

Example 7 with ZimbraQueryResults

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

the class TestUnread method testSearch.

/**
     * Makes sure that something comes back when searching for unread items.
     */
@Test
public void testSearch() throws Exception {
    ZimbraLog.test.debug("Starting Test %s", name.getMethodName());
    verifySetUp();
    ZimbraQueryResults results = mMbox.index.search(new OperationContext(mMbox), "is:unread", EnumSet.of(MailItem.Type.MESSAGE), SortBy.DATE_DESC, 100);
    Assert.assertTrue("No search results found", results.hasNext());
    results.close();
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) ZimbraQueryResults(com.zimbra.cs.index.ZimbraQueryResults) Test(org.junit.Test)

Example 8 with ZimbraQueryResults

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

the class GalSearchControl method doLocalGalAccountSearch.

private boolean doLocalGalAccountSearch(Account galAcct) {
    ZimbraQueryResults zqr = null;
    try {
        Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(galAcct);
        SearchParams searchParams = mParams.getSearchParams();
        zqr = mbox.index.search(SoapProtocol.Soap12, new OperationContext(mbox), searchParams);
        ResultsPager pager = ResultsPager.create(zqr, searchParams);
        GalSearchResultCallback callback = mParams.getResultCallback();
        int num = 0;
        while (pager.hasNext()) {
            ZimbraHit hit = pager.getNextHit();
            if (hit instanceof ContactHit) {
                Element contactElem = callback.handleContact(((ContactHit) hit).getContact());
                if (contactElem != null)
                    contactElem.addAttribute(MailConstants.A_SORT_FIELD, hit.getSortField(pager.getSortOrder()).toString());
            }
            num++;
            if (num == mParams.getLimit())
                break;
        }
        callback.setSortBy(zqr.getSortBy().toString());
        callback.setQueryOffset(searchParams.getOffset());
        callback.setHasMoreResult(pager.hasNext());
    } catch (Exception e) {
        ZimbraLog.gal.warn("search on GalSync account failed for %s", galAcct.getId(), e);
        return false;
    } finally {
        Closeables.closeQuietly(zqr);
    }
    return true;
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) ZimbraHit(com.zimbra.cs.index.ZimbraHit) SearchParams(com.zimbra.cs.index.SearchParams) Mailbox(com.zimbra.cs.mailbox.Mailbox) ContactHit(com.zimbra.cs.index.ContactHit) ResultsPager(com.zimbra.cs.index.ResultsPager) Element(com.zimbra.common.soap.Element) ZimbraQueryResults(com.zimbra.cs.index.ZimbraQueryResults) SoapFaultException(com.zimbra.common.soap.SoapFaultException) ServiceException(com.zimbra.common.service.ServiceException) IOException(java.io.IOException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException)

Example 9 with ZimbraQueryResults

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

the class Formatter method getMailItems.

// Caller is responsible for filtering out Appointments/Tasks marked private if the requester
// is not the mailbox owner.
public Iterator<? extends MailItem> getMailItems(UserServletContext context, long startTime, long endTime, long chunkSize) throws ServiceException {
    if (context.requestedItems != null) {
        return context.getRequestedItems();
    }
    assert (context.target != null);
    String query = context.getQueryString();
    if (query != null) {
        if (context.target instanceof Folder) {
            Folder f = (Folder) context.target;
            if (f.getId() != Mailbox.ID_FOLDER_USER_ROOT) {
                query = "in:" + f.getPath() + " " + query;
            }
        }
        Set<MailItem.Type> types;
        if (context.getTypesString() == null) {
            types = getDefaultSearchTypes();
        } else {
            try {
                types = MailItem.Type.setOf(context.getTypesString());
            } catch (IllegalArgumentException e) {
                throw MailServiceException.INVALID_TYPE(e.getMessage());
            }
        }
        ZimbraQueryResults results = context.targetMailbox.index.search(context.opContext, query, types, context.sortBy, context.getOffset() + context.getLimit());
        return new QueryResultIterator(results);
    } else if (context.target instanceof Folder) {
        Collection<? extends MailItem> items = getMailItemsFromFolder(context, (Folder) context.target, startTime, endTime, chunkSize);
        return items != null ? items.iterator() : null;
    } else {
        ArrayList<MailItem> result = new ArrayList<MailItem>();
        result.add(context.target);
        return result.iterator();
    }
}
Also used : FormatType(com.zimbra.cs.service.formatter.FormatterFactory.FormatType) MailItem(com.zimbra.cs.mailbox.MailItem) ZimbraQueryResults(com.zimbra.cs.index.ZimbraQueryResults) ArrayList(java.util.ArrayList) Collection(java.util.Collection) Folder(com.zimbra.cs.mailbox.Folder)

Example 10 with ZimbraQueryResults

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

the class MessageTest method moveOutOfSpam.

@Test
public void moveOutOfSpam() throws Exception {
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    mbox.getAccount().setJunkMessagesIndexingEnabled(false);
    DeliveryOptions opt = new DeliveryOptions();
    opt.setFolderId(Mailbox.ID_FOLDER_SPAM);
    Message msg = mbox.addMessage(null, new ParsedMessage("From: spammer@zimbra.com\r\nTo: test@zimbra.com".getBytes(), false), opt, null);
    MailboxTestUtil.index(mbox);
    SearchParams params = new SearchParams();
    params.setSortBy(SortBy.NONE);
    params.setTypes(EnumSet.of(MailItem.Type.MESSAGE));
    params.setQueryString("from:spammer");
    ZimbraQueryResults result = mbox.index.search(SoapProtocol.Soap12, new OperationContext(mbox), params);
    Assert.assertFalse(result.hasNext());
    mbox.move(new OperationContext(mbox), msg.getId(), MailItem.Type.MESSAGE, Mailbox.ID_FOLDER_INBOX);
    MailboxTestUtil.index(mbox);
    result = mbox.index.search(SoapProtocol.Soap12, new OperationContext(mbox), params);
    Assert.assertTrue(result.hasNext());
    Assert.assertEquals(msg.getId(), result.getNext().getItemId());
}
Also used : SearchParams(com.zimbra.cs.index.SearchParams) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) ZimbraQueryResults(com.zimbra.cs.index.ZimbraQueryResults) Test(org.junit.Test)

Aggregations

ZimbraQueryResults (com.zimbra.cs.index.ZimbraQueryResults)22 Mailbox (com.zimbra.cs.mailbox.Mailbox)13 ZimbraHit (com.zimbra.cs.index.ZimbraHit)11 OperationContext (com.zimbra.cs.mailbox.OperationContext)11 ServiceException (com.zimbra.common.service.ServiceException)9 ArrayList (java.util.ArrayList)9 SearchParams (com.zimbra.cs.index.SearchParams)8 Element (com.zimbra.common.soap.Element)6 Account (com.zimbra.cs.account.Account)5 IOException (java.io.IOException)5 Test (org.junit.Test)5 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)4 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)4 HashMap (java.util.HashMap)4 MessageHit (com.zimbra.cs.index.MessageHit)3 SortBy (com.zimbra.cs.index.SortBy)3 Contact (com.zimbra.cs.mailbox.Contact)3 Message (com.zimbra.cs.mailbox.Message)3 Mountpoint (com.zimbra.cs.mailbox.Mountpoint)3 ZMailbox (com.zimbra.client.ZMailbox)2