use of org.apache.lucene.queryparser.xml.ParserException in project lucene-solr by apache.
the class UserInputQueryBuilder method getQuery.
/* (non-Javadoc)
* @see org.apache.lucene.xmlparser.QueryObjectBuilder#process(org.w3c.dom.Element)
*/
@Override
public Query getQuery(Element e) throws ParserException {
String text = DOMUtils.getText(e);
try {
Query q = null;
if (unSafeParser != null) {
//synchronize on unsafe parser
synchronized (unSafeParser) {
q = unSafeParser.parse(text);
}
} else {
String fieldName = DOMUtils.getAttribute(e, "fieldName", defaultField);
//Create new parser
QueryParser parser = createQueryParser(fieldName, analyzer);
q = parser.parse(text);
}
float boost = DOMUtils.getAttribute(e, "boost", 1.0f);
return new BoostQuery(q, boost);
} catch (ParseException e1) {
throw new ParserException(e1.getMessage());
}
}
use of org.apache.lucene.queryparser.xml.ParserException in project lucene-solr by apache.
the class LegacyNumericRangeQueryBuilder method getQuery.
@Override
public Query getQuery(Element e) throws ParserException {
String field = DOMUtils.getAttributeWithInheritanceOrFail(e, "fieldName");
final String lowerTerm = DOMUtils.getAttribute(e, "lowerTerm", null);
final String upperTerm = DOMUtils.getAttribute(e, "upperTerm", null);
boolean lowerInclusive = DOMUtils.getAttribute(e, "includeLower", true);
boolean upperInclusive = DOMUtils.getAttribute(e, "includeUpper", true);
int precisionStep = DOMUtils.getAttribute(e, "precisionStep", LegacyNumericUtils.PRECISION_STEP_DEFAULT);
String type = DOMUtils.getAttribute(e, "type", "int");
try {
Query filter;
if (type.equalsIgnoreCase("int")) {
filter = LegacyNumericRangeQuery.newIntRange(field, precisionStep, (lowerTerm == null ? null : Integer.valueOf(lowerTerm)), (upperTerm == null ? null : Integer.valueOf(upperTerm)), lowerInclusive, upperInclusive);
} else if (type.equalsIgnoreCase("long")) {
filter = LegacyNumericRangeQuery.newLongRange(field, precisionStep, (lowerTerm == null ? null : Long.valueOf(lowerTerm)), (upperTerm == null ? null : Long.valueOf(upperTerm)), lowerInclusive, upperInclusive);
} else if (type.equalsIgnoreCase("double")) {
filter = LegacyNumericRangeQuery.newDoubleRange(field, precisionStep, (lowerTerm == null ? null : Double.valueOf(lowerTerm)), (upperTerm == null ? null : Double.valueOf(upperTerm)), lowerInclusive, upperInclusive);
} else if (type.equalsIgnoreCase("float")) {
filter = LegacyNumericRangeQuery.newFloatRange(field, precisionStep, (lowerTerm == null ? null : Float.valueOf(lowerTerm)), (upperTerm == null ? null : Float.valueOf(upperTerm)), lowerInclusive, upperInclusive);
} else {
throw new ParserException("type attribute must be one of: [long, int, double, float]");
}
return filter;
} catch (NumberFormatException nfe) {
throw new ParserException("Could not parse lowerTerm or upperTerm into a number", nfe);
}
}
use of org.apache.lucene.queryparser.xml.ParserException in project lucene-solr by apache.
the class TestLegacyNumericRangeQueryBuilder method testGetFilterHandleNumericParseErrorStrict.
public void testGetFilterHandleNumericParseErrorStrict() throws Exception {
LegacyNumericRangeQueryBuilder filterBuilder = new LegacyNumericRangeQueryBuilder();
String xml = "<LegacyNumericRangeQuery fieldName='AGE' type='int' lowerTerm='-1' upperTerm='NaN'/>";
Document doc = getDocumentFromString(xml);
try {
filterBuilder.getQuery(doc.getDocumentElement());
} catch (ParserException e) {
return;
}
fail("Expected to throw " + ParserException.class);
}
use of org.apache.lucene.queryparser.xml.ParserException in project lucene-solr by apache.
the class LikeThisQueryBuilder method getQuery.
/* (non-Javadoc)
* @see org.apache.lucene.xmlparser.QueryObjectBuilder#process(org.w3c.dom.Element)
*/
@Override
public Query getQuery(Element e) throws ParserException {
//a comma-delimited list of fields
String fieldsList = e.getAttribute("fieldNames");
String[] fields = defaultFieldNames;
if ((fieldsList != null) && (fieldsList.trim().length() > 0)) {
fields = fieldsList.trim().split(",");
//trim the fieldnames
for (int i = 0; i < fields.length; i++) {
fields[i] = fields[i].trim();
}
}
//Parse any "stopWords" attribute
//TODO MoreLikeThis needs to ideally have per-field stopWords lists - until then
//I use all analyzers/fields to generate multi-field compatible stop list
String stopWords = e.getAttribute("stopWords");
Set<String> stopWordsSet = null;
if ((stopWords != null) && (fields != null)) {
stopWordsSet = new HashSet<>();
for (String field : fields) {
try (TokenStream ts = analyzer.tokenStream(field, stopWords)) {
CharTermAttribute termAtt = ts.addAttribute(CharTermAttribute.class);
ts.reset();
while (ts.incrementToken()) {
stopWordsSet.add(termAtt.toString());
}
ts.end();
} catch (IOException ioe) {
throw new ParserException("IoException parsing stop words list in " + getClass().getName() + ":" + ioe.getLocalizedMessage());
}
}
}
MoreLikeThisQuery mlt = new MoreLikeThisQuery(DOMUtils.getText(e), fields, analyzer, fields[0]);
mlt.setMaxQueryTerms(DOMUtils.getAttribute(e, "maxQueryTerms", DEFAULT_MAX_QUERY_TERMS));
mlt.setMinTermFrequency(DOMUtils.getAttribute(e, "minTermFrequency", DEFAULT_MIN_TERM_FREQUENCY));
mlt.setPercentTermsToMatch(DOMUtils.getAttribute(e, "percentTermsToMatch", DEFAULT_PERCENT_TERMS_TO_MATCH) / 100);
mlt.setStopWords(stopWordsSet);
int minDocFreq = DOMUtils.getAttribute(e, "minDocFreq", -1);
if (minDocFreq >= 0) {
mlt.setMinDocFreq(minDocFreq);
}
Query q = mlt;
float boost = DOMUtils.getAttribute(e, "boost", 1.0f);
if (boost != 1f) {
q = new BoostQuery(mlt, boost);
}
return q;
}
use of org.apache.lucene.queryparser.xml.ParserException in project lucene-solr by apache.
the class PointRangeQueryBuilder method getQuery.
@Override
public Query getQuery(Element e) throws ParserException {
String field = DOMUtils.getAttributeWithInheritanceOrFail(e, "fieldName");
final String lowerTerm = DOMUtils.getAttribute(e, "lowerTerm", null);
final String upperTerm = DOMUtils.getAttribute(e, "upperTerm", null);
String type = DOMUtils.getAttribute(e, "type", "int");
try {
if (type.equalsIgnoreCase("int")) {
return IntPoint.newRangeQuery(field, (lowerTerm == null ? Integer.MIN_VALUE : Integer.parseInt(lowerTerm)), (upperTerm == null ? Integer.MAX_VALUE : Integer.parseInt(upperTerm)));
} else if (type.equalsIgnoreCase("long")) {
return LongPoint.newRangeQuery(field, (lowerTerm == null ? Long.MIN_VALUE : Long.parseLong(lowerTerm)), (upperTerm == null ? Long.MAX_VALUE : Long.parseLong(upperTerm)));
} else if (type.equalsIgnoreCase("double")) {
return DoublePoint.newRangeQuery(field, (lowerTerm == null ? Double.NEGATIVE_INFINITY : Double.parseDouble(lowerTerm)), (upperTerm == null ? Double.POSITIVE_INFINITY : Double.parseDouble(upperTerm)));
} else if (type.equalsIgnoreCase("float")) {
return FloatPoint.newRangeQuery(field, (lowerTerm == null ? Float.NEGATIVE_INFINITY : Float.parseFloat(lowerTerm)), (upperTerm == null ? Float.POSITIVE_INFINITY : Float.parseFloat(upperTerm)));
} else {
throw new ParserException("type attribute must be one of: [long, int, double, float]");
}
} catch (NumberFormatException nfe) {
throw new ParserException("Could not parse lowerTerm or upperTerm into a number", nfe);
}
}
Aggregations