Search in sources :

Example 1 with ParserException

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());
    }
}
Also used : ParserException(org.apache.lucene.queryparser.xml.ParserException) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) Query(org.apache.lucene.search.Query) BoostQuery(org.apache.lucene.search.BoostQuery) ParseException(org.apache.lucene.queryparser.classic.ParseException) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 2 with ParserException

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);
    }
}
Also used : ParserException(org.apache.lucene.queryparser.xml.ParserException) Query(org.apache.lucene.search.Query) LegacyNumericRangeQuery(org.apache.solr.legacy.LegacyNumericRangeQuery)

Example 3 with ParserException

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);
}
Also used : ParserException(org.apache.lucene.queryparser.xml.ParserException) Document(org.w3c.dom.Document)

Example 4 with ParserException

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;
}
Also used : ParserException(org.apache.lucene.queryparser.xml.ParserException) TokenStream(org.apache.lucene.analysis.TokenStream) Query(org.apache.lucene.search.Query) MoreLikeThisQuery(org.apache.lucene.queries.mlt.MoreLikeThisQuery) BoostQuery(org.apache.lucene.search.BoostQuery) IOException(java.io.IOException) BoostQuery(org.apache.lucene.search.BoostQuery) CharTermAttribute(org.apache.lucene.analysis.tokenattributes.CharTermAttribute) MoreLikeThisQuery(org.apache.lucene.queries.mlt.MoreLikeThisQuery)

Example 5 with ParserException

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);
    }
}
Also used : ParserException(org.apache.lucene.queryparser.xml.ParserException)

Aggregations

ParserException (org.apache.lucene.queryparser.xml.ParserException)6 Query (org.apache.lucene.search.Query)3 IOException (java.io.IOException)2 TokenStream (org.apache.lucene.analysis.TokenStream)2 BoostQuery (org.apache.lucene.search.BoostQuery)2 ArrayList (java.util.ArrayList)1 CharTermAttribute (org.apache.lucene.analysis.tokenattributes.CharTermAttribute)1 TermToBytesRefAttribute (org.apache.lucene.analysis.tokenattributes.TermToBytesRefAttribute)1 Term (org.apache.lucene.index.Term)1 MoreLikeThisQuery (org.apache.lucene.queries.mlt.MoreLikeThisQuery)1 ParseException (org.apache.lucene.queryparser.classic.ParseException)1 QueryParser (org.apache.lucene.queryparser.classic.QueryParser)1 SpanBoostQuery (org.apache.lucene.search.spans.SpanBoostQuery)1 SpanOrQuery (org.apache.lucene.search.spans.SpanOrQuery)1 SpanQuery (org.apache.lucene.search.spans.SpanQuery)1 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)1 LegacyNumericRangeQuery (org.apache.solr.legacy.LegacyNumericRangeQuery)1 Document (org.w3c.dom.Document)1