lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Hoss Man (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 Wed, 04 May 2011 23:12:03 GMT

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

Hoss Man commented on LUCENE-1418:
----------------------------------

David: i agree with you that QueryParser should be usable w/o a defaultField specified, and
any query string that doesn't need the defaultField should be parsed cleanly.

the original bug reported here was actually about a case where the defaultField *was* needed
to parse the query string (the unbounded params resulted in the QUeryParser wanting to parse
"expr2" as a field query on the defaultField -- in which case i think the NPE is appropriate,
and "Not a Problem" makes sense.

However: your example of \*:\* generating an NPE if there is no defaultField should definitely
be fixed -- it sounds like a more specific example of "QueryParser throwing NPE during parsing
of a query that does not need the default field when default field is not defined", and should
probably be tracked in a distinct jira ... i was going to file one, but i can't actually reproduce
the problem you are describing to post a stack trace -- can you please do that?


> 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


Mime
View raw message