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