lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mindaugas Žakšauskas <>
Subject Reusability of QueryParser
Date Mon, 06 Jan 2014 11:33:43 GMT

I was wondering if a QueryParser can be reused (Lucene ver: 4.6.0)?
>From my experiment it looks like it retains some state from the
previous query.

Isolated example:

public class Test {

    public static void main(String[] args) throws ParseException, IOException {
        MyAnalyzer analyzer = new MyAnalyzer();
        QueryParser qp = new QueryParser(Version.LUCENE_46, "x", analyzer);
        Query q1 = qp.parse("foo:Moo");
        Query q2 = qp.parse("bar:Meh");
        Query q3 = new QueryParser(Version.LUCENE_46, "x", new MyAnalyzer())

    private static final class MyAnalyzer extends Analyzer {
        protected TokenStreamComponents createComponents(String field,
Reader reader) {
            KeywordTokenizer source = new KeywordTokenizer(reader);
            if ("foo".equals(field)) {
                return new TokenStreamComponents(source, new
StandardFilter(Version.LUCENE_46, source));
            } else {
                return new TokenStreamComponents(source, new
LowerCaseFilter(Version.LUCENE_46, source));

Above prints:

Comment to the above code: MyAnalyzer is a simple analyzer which
behaves slightly differently based on the field: for "foo" field it
uses StandardFilter and for all other fields ("bar" in this case) it
uses LowerCaseFilter. So in the main method when parsing q2, I expect
to get "bar:meh" but I get "bar:Meh". At the same time, if i don't
reuse the QueryParser, I get "bar:baz" for the third query which is
the correct behaviour.

I was wondering if this is a bug of QueryParser, or do I miss something?


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message