lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Mark Miller (JIRA)" <j...@apache.org>
Subject [jira] Commented: (LUCENE-1418) QueryParser can throw NullPointerException during parsing of some queries in case if default field passed to constructor is null
Date Thu, 13 Nov 2008 00:11:46 GMT

    [ https://issues.apache.org/jira/browse/LUCENE-1418?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12647132#action_12647132
] 

Mark Miller commented on LUCENE-1418:
-------------------------------------

You shouldn't be passing null to the queryparser as a field right? And if you do, its not
part of the query syntax, so it doesn't make sense to feed a "" instead of the null to force
a parse exception. Just don't pass null, or if someone is really worried about it we could
check for null at the top of the method and throw a null argument exception as you suggest.

> QueryParser can throw NullPointerException during parsing of some queries in case if
default field passed to constructor is null
> --------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: LUCENE-1418
>                 URL: https://issues.apache.org/jira/browse/LUCENE-1418
>             Project: Lucene - Java
>          Issue Type: Bug
>          Components: QueryParser
>    Affects Versions: 2.4
>         Environment: CentOS 5.2 (probably any applies)
>            Reporter: Alexei Dets
>            Priority: Minor
>
> In case if QueryParser was constructed using "QueryParser(String f,  Analyzer a)" constructor
and f equals null then QueryParser can fail with NullPointerException during parsing of some
queries that _does_ contain field name but have unbalanced parenthesis.
> Example 1:
> Query:  field:(expr1) expr2)
> Result:
> java.lang.NullPointerException
> 	at org.apache.lucene.index.Term.<init>(Term.java:50)
> 	at org.apache.lucene.index.Term.<init>(Term.java:36)
> 	at org.apache.lucene.queryParser.QueryParser.getFieldQuery(QueryParser.java:543)
> 	at org.apache.lucene.queryParser.QueryParser.Term(QueryParser.java:1324)
> 	at org.apache.lucene.queryParser.QueryParser.Clause(QueryParser.java:1211)
> 	at org.apache.lucene.queryParser.QueryParser.Query(QueryParser.java:1168)
> 	at org.apache.lucene.queryParser.QueryParser.TopLevelQuery(QueryParser.java:1128)
> 	at org.apache.lucene.queryParser.QueryParser.parse(QueryParser.java:170)
> Example2:
> Query:  field:(expr1) "expr2")
> Result:
> java.lang.NullPointerException
> 	at org.apache.lucene.index.Term.<init>(Term.java:50)
> 	at org.apache.lucene.index.Term.<init>(Term.java:36)
> 	at org.apache.lucene.queryParser.QueryParser.getFieldQuery(QueryParser.java:543)
> 	at org.apache.lucene.queryParser.QueryParser.getFieldQuery(QueryParser.java:612)
> 	at org.apache.lucene.queryParser.QueryParser.Term(QueryParser.java:1459)
> 	at org.apache.lucene.queryParser.QueryParser.Clause(QueryParser.java:1211)
> 	at org.apache.lucene.queryParser.QueryParser.Query(QueryParser.java:1168)
> 	at org.apache.lucene.queryParser.QueryParser.TopLevelQuery(QueryParser.java:1128)
> 	at org.apache.lucene.queryParser.QueryParser.parse(QueryParser.java:170)
> Workaround: pass in constructor empty string as a default field name - in this case QueryParser.parse
method will throw ParseException (expected result because query string is wrong) instead of
NullPointerException.
> It is not obvious to me how to fix this so I'll describe my usecase, may be I'm doing
something completely wrong.
> Basically I have a set of per-field queries entered by user and need to programmatically
construct (after some preprocessing) one real Lucene query combined from these user-entered
per-field subqueries.
> To achieve this I basically do the following (simplified a bit):
> QueryParser parser = new QueryParser(null, analyzer); // I'll always provide a field
name in a query string as it is different each time and I don't have any default
> BooleanQuery query = new BooleanQuery();
> Query subQuery1 = parser.parse(field1 + ":(" + queryString1 + ')');
> query.add(subQuery1, operator1); // operator = BooleanClause.Occur.MUST, BooleanClause.Occur.MUST_NOT
or BooleanClause.Occur.SHOULD
> Query subQuery2 = parser.parse(field2 + ":(" + queryString2 + ')');
> query.add(subQuery2, operator2); 
> Query subQuery3 = parser.parse(field3 + ":(" + queryString3 + ')');
> query.add(subQuery3, operator3); 
> ...
> IMHO either QueryParser constructor should be changed to throw NullPointerException/InvalidArgumentException
in case of null field passed (and API documentation updated) or QueryParser.parse behavior
should be fixed to correctly throw ParseException instead of NullPointerException. Also IMHO
of a great help can be _public_ setField/getField methods of QueryParser (that set/get field),
this can help in use cases like my:
> QueryParser parser = new QueryParser(null, analyzer); // or add constructor with analyzer
_only_ for such cases
> BooleanQuery query = new BooleanQuery();
> parser.setField(field1);
> Query subQuery1 = parser.parse(queryString1);
> query.add(subQuery1, operator1);
> parser.setField(field2);
> Query subQuery2 = parser.parse(queryString2);
> query.add(subQuery2, operator2); 
> ...

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


---------------------------------------------------------------------
To unsubscribe, e-mail: java-dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-dev-help@lucene.apache.org


Mime
View raw message