Search in sources :

Example 16 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 17 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 18 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 19 with QueryParser

use of org.apache.lucene.queryParser.QueryParser in project collect by openforis.

the class RecordIndexManager method createQuery.

protected Query createQuery(SearchType searchType, String indexFieldKey, String searchText) throws ParseException {
    String escapedSearchText = QueryParser.escape(searchText.trim().toLowerCase(Locale.ENGLISH));
    String queryText = escapedSearchText;
    if (StringUtils.isNotBlank(queryText)) {
        switch(searchType) {
            case STARTS_WITH:
                queryText = escapedSearchText + "*";
                break;
            case CONTAINS:
                // queryText = "*" + escapedSearchText + "*"; TODO support CONTAINS query
                queryText = escapedSearchText + "*";
                break;
            default:
                queryText = escapedSearchText;
        }
    }
    SimpleAnalyzer analyzer = new SimpleAnalyzer(LUCENE_VERSION);
    QueryParser queryParser = new QueryParser(LUCENE_VERSION, indexFieldKey, analyzer);
    queryParser.setDefaultOperator(Operator.AND);
    Query query = queryParser.parse(queryText);
    return query;
}
Also used : QueryParser(org.apache.lucene.queryParser.QueryParser) Query(org.apache.lucene.search.Query) SimpleAnalyzer(org.apache.lucene.analysis.SimpleAnalyzer)

Example 20 with QueryParser

use of org.apache.lucene.queryParser.QueryParser in project graphdb by neo4j-attic.

the class IndexType method query.

Query query(String keyOrNull, Object value, QueryContext contextOrNull) {
    if (value instanceof Query) {
        return (Query) value;
    }
    QueryParser parser = new QueryParser(Version.LUCENE_30, keyOrNull, analyzer);
    parser.setAllowLeadingWildcard(true);
    parser.setLowercaseExpandedTerms(toLowerCase);
    if (contextOrNull != null && contextOrNull.getDefaultOperator() != null) {
        parser.setDefaultOperator(contextOrNull.getDefaultOperator());
    }
    try {
        return parser.parse(value.toString());
    } catch (ParseException e) {
        throw new RuntimeException(e);
    }
}
Also used : QueryParser(org.apache.lucene.queryParser.QueryParser) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) ParseException(org.apache.lucene.queryParser.ParseException)

Aggregations

QueryParser (org.apache.lucene.queryParser.QueryParser)21 Query (org.apache.lucene.search.Query)12 ParseException (org.apache.lucene.queryParser.ParseException)8 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)7 IOException (java.io.IOException)6 Document (org.apache.lucene.document.Document)6 BooleanQuery (org.apache.lucene.search.BooleanQuery)6 TermQuery (org.apache.lucene.search.TermQuery)6 Analyzer (org.apache.lucene.analysis.Analyzer)4 IndexSearcher (org.apache.lucene.search.IndexSearcher)4 RAMDirectory (org.apache.lucene.store.RAMDirectory)4 Name (org.apache.jackrabbit.spi.Name)3 Field (org.apache.lucene.document.Field)3 IndexReader (org.apache.lucene.index.IndexReader)3 IndexWriter (org.apache.lucene.index.IndexWriter)3 Hits (org.apache.lucene.search.Hits)3 Test (org.junit.Test)3 Contact (com.zimbra.cs.mailbox.Contact)2 Mailbox (com.zimbra.cs.mailbox.Mailbox)2 ParsedContact (com.zimbra.cs.mime.ParsedContact)2