You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@lucene.apache.org by "David Smiley (JIRA)" <ji...@apache.org> on 2011/04/30 23:43:10 UTC

[jira] [Commented] (LUCENE-1418) QueryParser can throw NullPointerException during parsing of some queries in case if default field passed to constructor is null

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

David Smiley commented on LUCENE-1418:
--------------------------------------

Sorry, but I disagree Mark & Shai.

The status quo (doing nothing) is the worst option. If committers decide the default field is mandatory then the constructor should check and throw an exception.

But I happen to believe that null is valid.  In my Solr schema.xml I don't specify a default field because there isn't a suitable default field.  I use dismax (DisjunctionMaxQuery) and list appropriate fields for user queries. That works fine.  In every case a raw lucene query exists, it's one that I write, not users, and I explicitly name fields appropriate for what I'm doing as applicable. The query "*:*" for the dismax q.alt param fails due to an NPE.  This issue's title is appropriate so I'm not opening a new bug, but the reporter's description is completely off base from my scenario since I know to balance parenthesis in my queries ;-)

> 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.
For more information on JIRA, see: http://www.atlassian.com/software/jira

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