Search in sources :

Example 1 with ZimbraIndexSearcher

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

the class MailboxIndex method getAttachmentTypes.

/**
     * Returns all attachment types from the index.
     *
     * @param regex matching pattern or null to match everything
     * @return {@link BrowseTerm}s which correspond to all of the attachment types in the index
     */
public List<BrowseTerm> getAttachmentTypes(String regex) throws IOException, ServiceException {
    Pattern pattern = Strings.isNullOrEmpty(regex) ? null : Pattern.compile(regex);
    List<BrowseTerm> result = new ArrayList<BrowseTerm>();
    ZimbraIndexSearcher searcher = indexStore.openSearcher();
    TermFieldEnumeration values = null;
    try {
        values = searcher.getIndexReader().getTermsForField(LuceneFields.L_ATTACHMENTS, "");
        while (values.hasMoreElements()) {
            BrowseTerm term = values.nextElement();
            if (pattern == null || AccessBoundedRegex.matches(term.getText(), pattern, MAX_REGEX_ACCESSES)) {
                result.add(term);
            }
        }
    } finally {
        Closeables.closeQuietly(values);
        Closeables.closeQuietly(searcher);
    }
    return result;
}
Also used : Pattern(java.util.regex.Pattern) BrowseTerm(com.zimbra.cs.index.BrowseTerm) TermFieldEnumeration(com.zimbra.cs.index.ZimbraIndexReader.TermFieldEnumeration) ArrayList(java.util.ArrayList) ZimbraIndexSearcher(com.zimbra.cs.index.ZimbraIndexSearcher)

Example 2 with ZimbraIndexSearcher

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

the class MailboxIndex method getDomains.

/**
     * Returns all domain names from the index.
     *
     * @param field Lucene field name (e.g. LuceneFields.L_H_CC)
     * @param regex matching pattern or null to match everything
     * @return {@link BrowseTerm}s which correspond to all of the domain terms stored in a given field
     */
public List<BrowseTerm> getDomains(String field, String regex) throws IOException, ServiceException {
    Pattern pattern = Strings.isNullOrEmpty(regex) ? null : Pattern.compile(regex.startsWith("@") ? regex : "@" + regex);
    List<BrowseTerm> result = new ArrayList<BrowseTerm>();
    ZimbraIndexSearcher searcher = indexStore.openSearcher();
    TermFieldEnumeration values = null;
    try {
        values = searcher.getIndexReader().getTermsForField(field, "");
        while (values.hasMoreElements()) {
            BrowseTerm term = values.nextElement();
            if (term == null) {
                break;
            }
            String text = term.getText();
            // Domains are tokenized with '@' prefix. Exclude partial domain tokens.
            if (text.startsWith("@") && text.contains(".")) {
                if (pattern == null || AccessBoundedRegex.matches(text, pattern, MAX_REGEX_ACCESSES)) {
                    result.add(new BrowseTerm(text.substring(1), term.getFreq()));
                }
            }
        }
    } finally {
        Closeables.closeQuietly(values);
        Closeables.closeQuietly(searcher);
    }
    return result;
}
Also used : Pattern(java.util.regex.Pattern) BrowseTerm(com.zimbra.cs.index.BrowseTerm) TermFieldEnumeration(com.zimbra.cs.index.ZimbraIndexReader.TermFieldEnumeration) ArrayList(java.util.ArrayList) ZimbraIndexSearcher(com.zimbra.cs.index.ZimbraIndexSearcher)

Example 3 with ZimbraIndexSearcher

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

the class MailboxIndex method existsInContacts.

/**
     * Returns true if any of the specified email addresses exists in contacts, otherwise false.
     */
public boolean existsInContacts(Collection<InternetAddress> addrs) throws IOException {
    Set<MailItem.Type> types = EnumSet.of(MailItem.Type.CONTACT);
    if (getDeferredCount(types) > 0) {
        try {
            indexDeferredItems(types, new BatchStatus(), false);
        } catch (ServiceException e) {
            ZimbraLog.index.error("Failed to index deferred items", e);
        }
    }
    ZimbraIndexSearcher searcher = indexStore.openSearcher();
    try {
        for (InternetAddress addr : addrs) {
            if (!Strings.isNullOrEmpty(addr.getAddress())) {
                String lcAddr = addr.getAddress().toLowerCase();
                TermFieldEnumeration values = null;
                try {
                    values = searcher.getIndexReader().getTermsForField(LuceneFields.L_CONTACT_DATA, lcAddr);
                    if (values.hasMoreElements()) {
                        BrowseTerm term = values.nextElement();
                        if (term != null && lcAddr.equals(term.getText())) {
                            ZimbraLog.index.debug("Contact = %s present in indexed items", lcAddr);
                            return true;
                        }
                    }
                } finally {
                    Closeables.closeQuietly(values);
                }
            }
        }
        return false;
    } finally {
        Closeables.closeQuietly(searcher);
    }
}
Also used : Type(com.zimbra.cs.mailbox.MailItem.Type) InternetAddress(com.zimbra.common.mime.InternetAddress) BrowseTerm(com.zimbra.cs.index.BrowseTerm) ServiceException(com.zimbra.common.service.ServiceException) TermFieldEnumeration(com.zimbra.cs.index.ZimbraIndexReader.TermFieldEnumeration) ZimbraIndexSearcher(com.zimbra.cs.index.ZimbraIndexSearcher)

Example 4 with ZimbraIndexSearcher

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

the class MailboxIndex method getObjects.

/**
     * Returns all objects (e.g. PO, etc) from the index.
     *
     * @param regex matching pattern or null to match everything
     * @return {@link BrowseTerm}s which correspond to all of the objects in the index
     */
public List<BrowseTerm> getObjects(String regex) throws IOException, ServiceException {
    Pattern pattern = Strings.isNullOrEmpty(regex) ? null : Pattern.compile(regex);
    List<BrowseTerm> result = new ArrayList<BrowseTerm>();
    ZimbraIndexSearcher searcher = indexStore.openSearcher();
    TermFieldEnumeration values = null;
    try {
        values = searcher.getIndexReader().getTermsForField(LuceneFields.L_OBJECTS, "");
        while (values.hasMoreElements()) {
            BrowseTerm term = values.nextElement();
            if (term == null) {
                break;
            }
            if (pattern == null || AccessBoundedRegex.matches(term.getText(), pattern, MAX_REGEX_ACCESSES)) {
                result.add(term);
            }
        }
    } finally {
        Closeables.closeQuietly(values);
        Closeables.closeQuietly(searcher);
    }
    return result;
}
Also used : Pattern(java.util.regex.Pattern) BrowseTerm(com.zimbra.cs.index.BrowseTerm) TermFieldEnumeration(com.zimbra.cs.index.ZimbraIndexReader.TermFieldEnumeration) ArrayList(java.util.ArrayList) ZimbraIndexSearcher(com.zimbra.cs.index.ZimbraIndexSearcher)

Aggregations

BrowseTerm (com.zimbra.cs.index.BrowseTerm)4 TermFieldEnumeration (com.zimbra.cs.index.ZimbraIndexReader.TermFieldEnumeration)4 ZimbraIndexSearcher (com.zimbra.cs.index.ZimbraIndexSearcher)4 ArrayList (java.util.ArrayList)3 Pattern (java.util.regex.Pattern)3 InternetAddress (com.zimbra.common.mime.InternetAddress)1 ServiceException (com.zimbra.common.service.ServiceException)1 Type (com.zimbra.cs.mailbox.MailItem.Type)1