use of org.apache.lucene.queryparser.classic.QueryParser in project jena by apache.
the class TextIndexLucene method parseQuery.
private Query parseQuery(String queryString, Analyzer analyzer) throws ParseException {
QueryParser queryParser = getQueryParser(analyzer);
queryParser.setAllowLeadingWildcard(true);
Query query = queryParser.parse(queryString);
return query;
}
use of org.apache.lucene.queryparser.classic.QueryParser in project jackrabbit-oak by apache.
the class LucenePropertyIndex method getLuceneRequest.
/**
* Get the Lucene query for the given filter.
*
* @param plan index plan containing filter details
* @param reader the Lucene reader
* @return the Lucene query
*/
private static LuceneRequestFacade getLuceneRequest(IndexPlan plan, IndexAugmentorFactory augmentorFactory, IndexReader reader) {
FulltextQueryTermsProvider augmentor = getIndexAgumentor(plan, augmentorFactory);
List<Query> qs = new ArrayList<Query>();
Filter filter = plan.getFilter();
FullTextExpression ft = filter.getFullTextConstraint();
PlanResult planResult = getPlanResult(plan);
IndexDefinition defn = planResult.indexDefinition;
Analyzer analyzer = defn.getAnalyzer();
if (ft == null) {
// there might be no full-text constraint
// when using the LowCostLuceneIndexProvider
// which is used for testing
} else {
qs.add(getFullTextQuery(plan, ft, analyzer, augmentor));
}
//Check if native function is supported
PropertyRestriction pr = null;
if (defn.hasFunctionDefined()) {
pr = filter.getPropertyRestriction(defn.getFunctionName());
}
if (pr != null) {
String query = String.valueOf(pr.first.getValue(pr.first.getType()));
QueryParser queryParser = new QueryParser(VERSION, "", analyzer);
if (query.startsWith("mlt?")) {
String mltQueryString = query.replace("mlt?", "");
if (reader != null) {
Query moreLikeThis = MoreLikeThisHelper.getMoreLikeThis(reader, analyzer, mltQueryString);
if (moreLikeThis != null) {
qs.add(moreLikeThis);
}
}
} else if (query.startsWith("spellcheck?")) {
String spellcheckQueryString = query.replace("spellcheck?", "");
if (reader != null) {
return new LuceneRequestFacade<SpellcheckHelper.SpellcheckQuery>(SpellcheckHelper.getSpellcheckQuery(spellcheckQueryString, reader));
}
} else if (query.startsWith("suggest?")) {
String suggestQueryString = query.replace("suggest?", "");
if (reader != null) {
return new LuceneRequestFacade<SuggestHelper.SuggestQuery>(SuggestHelper.getSuggestQuery(suggestQueryString));
}
} else {
try {
qs.add(queryParser.parse(query));
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
} else if (planResult.evaluateNonFullTextConstraints()) {
addNonFullTextConstraints(qs, plan, reader);
}
if (qs.size() == 0 && plan.getSortOrder() != null) {
//This case indicates that query just had order by and no
//property restriction defined. In this case property
//existence queries for each sort entry
List<OrderEntry> orders = removeNativeSort(plan.getSortOrder());
for (int i = 0; i < orders.size(); i++) {
OrderEntry oe = orders.get(i);
PropertyDefinition pd = planResult.getOrderedProperty(i);
PropertyRestriction orderRest = new PropertyRestriction();
orderRest.propertyName = oe.getPropertyName();
Query q = createQuery(orderRest, pd);
if (q != null) {
qs.add(q);
}
}
}
if (qs.size() == 0) {
if (reader == null) {
//just return match all queries
return new LuceneRequestFacade<Query>(new MatchAllDocsQuery());
}
//be returned (if the index definition has a single rule)
if (planResult.evaluateNodeTypeRestriction()) {
return new LuceneRequestFacade<Query>(new MatchAllDocsQuery());
}
throw new IllegalStateException("No query created for filter " + filter);
}
return performAdditionalWraps(qs);
}
use of org.apache.lucene.queryparser.classic.QueryParser in project lucene-solr by apache.
the class EnwikiQueryMaker method createQueries.
/**
* Parse the strings containing Lucene queries.
*
* @param qs array of strings containing query expressions
* @param a analyzer to use when parsing queries
* @return array of Lucene queries
*/
private static Query[] createQueries(List<Object> qs, Analyzer a) {
QueryParser qp = new QueryParser(DocMaker.BODY_FIELD, a);
List<Object> queries = new ArrayList<>();
for (int i = 0; i < qs.size(); i++) {
try {
Object query = qs.get(i);
Query q = null;
if (query instanceof String) {
q = qp.parse((String) query);
} else if (query instanceof Query) {
q = (Query) query;
} else {
System.err.println("Unsupported Query Type: " + query);
}
if (q != null) {
queries.add(q);
}
} catch (Exception e) {
e.printStackTrace();
}
}
return queries.toArray(new Query[0]);
}
use of org.apache.lucene.queryparser.classic.QueryParser in project lucene-solr by apache.
the class FileBasedQueryMaker method prepareQueries.
@Override
protected Query[] prepareQueries() throws Exception {
Analyzer anlzr = NewAnalyzerTask.createAnalyzer(config.get("analyzer", "org.apache.lucene.analysis.standard.StandardAnalyzer"));
String defaultField = config.get("file.query.maker.default.field", DocMaker.BODY_FIELD);
QueryParser qp = new QueryParser(defaultField, anlzr);
qp.setAllowLeadingWildcard(true);
List<Query> qq = new ArrayList<>();
String fileName = config.get("file.query.maker.file", null);
if (fileName != null) {
Path path = Paths.get(fileName);
Reader reader = null;
// note: we use a decoding reader, so if your queries are screwed up you know
if (Files.exists(path)) {
reader = Files.newBufferedReader(path, StandardCharsets.UTF_8);
} else {
//see if we can find it as a resource
InputStream asStream = FileBasedQueryMaker.class.getClassLoader().getResourceAsStream(fileName);
if (asStream != null) {
reader = IOUtils.getDecodingReader(asStream, StandardCharsets.UTF_8);
}
}
if (reader != null) {
try {
BufferedReader buffered = new BufferedReader(reader);
String line = null;
int lineNum = 0;
while ((line = buffered.readLine()) != null) {
line = line.trim();
if (line.length() != 0 && !line.startsWith("#")) {
try {
qq.add(qp.parse(line));
} catch (ParseException e) {
System.err.println("Exception: " + e.getMessage() + " occurred while parsing line: " + lineNum + " Text: " + line);
}
}
lineNum++;
}
} finally {
reader.close();
}
} else {
System.err.println("No Reader available for: " + fileName);
}
}
return qq.toArray(new Query[qq.size()]);
}
use of org.apache.lucene.queryparser.classic.QueryParser in project lucene-solr by apache.
the class LongToEnglishQueryMaker method setConfig.
@Override
public void setConfig(Config config) throws Exception {
Analyzer anlzr = NewAnalyzerTask.createAnalyzer(config.get("analyzer", StandardAnalyzer.class.getName()));
parser = new QueryParser(DocMaker.BODY_FIELD, anlzr);
}
Aggregations