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);
}
}
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);
}
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);
}
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;
}
Aggregations