Search in sources :

Example 11 with QueryParser

use of org.apache.lucene.queryParser.QueryParser in project jackrabbit by apache.

the class LuceneQueryFactory method create.

/**
     * Creates a lucene query for the given QOM full text search.
     *
     * @param fts the full text search constraint.
     * @return the lucene query for the given constraint.
     * @throws RepositoryException if an error occurs while creating the query.
     */
public Query create(FullTextSearchImpl fts) throws RepositoryException {
    String fieldname;
    if (fts.getPropertyName() == null) {
        // fulltext on node
        fieldname = FieldNames.FULLTEXT;
    } else {
        // final path element is a property name
        Name propName = fts.getPropertyQName();
        StringBuffer tmp = new StringBuffer();
        tmp.append(nsMappings.getPrefix(propName.getNamespaceURI()));
        tmp.append(":").append(FieldNames.FULLTEXT_PREFIX);
        tmp.append(propName.getLocalName());
        fieldname = tmp.toString();
    }
    QueryParser parser = new JackrabbitQueryParser(fieldname, index.getTextAnalyzer(), index.getSynonymProvider(), cache);
    try {
        StaticOperand expr = fts.getFullTextSearchExpression();
        return parser.parse(evaluator.getValue(expr).getString());
    } catch (ParseException e) {
        throw new RepositoryException(e);
    }
}
Also used : QueryParser(org.apache.lucene.queryParser.QueryParser) StaticOperand(javax.jcr.query.qom.StaticOperand) RepositoryException(javax.jcr.RepositoryException) ParseException(org.apache.lucene.queryParser.ParseException) NodeName(javax.jcr.query.qom.NodeName) NodeLocalName(javax.jcr.query.qom.NodeLocalName) Name(org.apache.jackrabbit.spi.Name)

Example 12 with QueryParser

use of org.apache.lucene.queryParser.QueryParser 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 13 with QueryParser

use of org.apache.lucene.queryParser.QueryParser 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)

Example 14 with QueryParser

use of org.apache.lucene.queryParser.QueryParser in project ddf by codice.

the class ContextualEvaluator method evaluate.

/**
     * @param cec
     * @return
     * @throws IOException
     * @throws ParseException
     */
public static boolean evaluate(ContextualEvaluationCriteria cec) throws IOException, ParseException {
    String methodName = "evaluate";
    Directory index = cec.getIndex();
    String searchPhrase = cec.getCriteria();
    // and be used to determine if an element or attribute exist
    if (searchPhrase == null || searchPhrase.isEmpty()) {
        String[] textPaths = cec.getTextPaths();
        String fullDocument = cec.getMetadata();
        if (textPaths != null && textPaths.length > 0 && fullDocument != null) {
            String indexableText = getIndexableText(fullDocument, textPaths);
            if (indexableText != null && !indexableText.isEmpty()) {
                LOGGER.trace("Found element/attribute for textPaths");
                return true;
            }
        }
        LOGGER.trace("No search phrase specified and could not find element/attribute based on textPaths");
        return false;
    }
    // a. query
    QueryParser queryParser = null;
    if (cec.isCaseSensitiveSearch()) {
        LOGGER.debug("Doing case-sensitive search ...");
        queryParser = new QueryParser(Version.LUCENE_30, CASE_SENSITIVE_FIELD_NAME, new CaseSensitiveContextualAnalyzer(Version.LUCENE_30));
        // Make Wildcard, Prefix, Fuzzy, and Range queries *not* be automatically lower-cased,
        // i.e., make them be case-sensitive
        queryParser.setLowercaseExpandedTerms(false);
    } else {
        LOGGER.debug("Doing case-insensitive search ...");
        queryParser = new QueryParser(Version.LUCENE_30, FIELD_NAME, new ContextualAnalyzer(Version.LUCENE_30));
    }
    // Configures Lucene query parser to allow a wildcard as first character in the
    // contextual search phrase
    queryParser.setAllowLeadingWildcard(true);
    Query q = queryParser.parse(searchPhrase);
    // b. search
    int hitsPerPage = 1;
    IndexSearcher searcher = new IndexSearcher(index, true);
    TopDocs topDocs = searcher.search(q, hitsPerPage);
    // c. display results
    LOGGER.debug("Found {} hits.", topDocs.totalHits);
    // searcher can only be closed when there
    // is no need to access the documents any more.
    searcher.close();
    return topDocs.totalHits > 0;
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) QueryParser(org.apache.lucene.queryParser.QueryParser) Query(org.apache.lucene.search.Query) RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory)

Aggregations

QueryParser (org.apache.lucene.queryParser.QueryParser)14 Query (org.apache.lucene.search.Query)11 ParseException (org.apache.lucene.queryParser.ParseException)8 BooleanQuery (org.apache.lucene.search.BooleanQuery)6 TermQuery (org.apache.lucene.search.TermQuery)6 IOException (java.io.IOException)4 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)4 IndexSearcher (org.apache.lucene.search.IndexSearcher)4 Name (org.apache.jackrabbit.spi.Name)3 Hits (org.apache.lucene.search.Hits)3 Contact (com.zimbra.cs.mailbox.Contact)2 Mailbox (com.zimbra.cs.mailbox.Mailbox)2 ParsedContact (com.zimbra.cs.mime.ParsedContact)2 RepositoryException (javax.jcr.RepositoryException)2 NodeLocalName (javax.jcr.query.qom.NodeLocalName)2 NodeName (javax.jcr.query.qom.NodeName)2 StaticOperand (javax.jcr.query.qom.StaticOperand)2 Document (org.apache.lucene.document.Document)2 Term (org.apache.lucene.index.Term)2 MultiPhraseQuery (org.apache.lucene.search.MultiPhraseQuery)2