lucene-java-user mailing list archives

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

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");
        System.out.println(q1);
        System.out.println(q2);
        Query q3 = new QueryParser(Version.LUCENE_46, "x", new MyAnalyzer())
                                       .parse("bar:Baz");
        System.out.println(q3);
    }

    private static final class MyAnalyzer extends Analyzer {
        @Override
        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:
foo:Moo
bar:Meh
bar:baz

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?

Regards,
Mindaugas

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


Mime
View raw message