lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Thomas Pl├╝mpe <>
Subject Re: search for and condition
Date Tue, 10 Aug 2004 13:07:37 GMT
> String line="text1 text2";
> Query q = MultiFieldQueryParser.parse(line,fields,analyzer);
> Hits hits =;
> Here search will take "text1 OR text2" Condition
> How  to make searh  "text1 and  text2" Condiation
MultiFieldQueryParser uses QueryParser.parse which defaults to OR
searches. If you just copy MFQP.parse and have it use your own parse
method and call
on the QueryParser instance you should be done. (see further below)

Note that AND searches done this way only retrieve hits when the "ANDed"
terms are in the same field, e.g.
	football AND computers
would produce a hit for documents which have, say,

  Document 1:
    hobbies: football, computers

but not for documents which have these terms in different fields, say,

  Document 2:
    hobbies: football, tennis
    skills: computers, gardening


     * This method is taken from the MultiFieldQueryParser, but it uses
     * its own parse method, which sets the QueryParser to use AND
     * as a default.
    static Query parse(String query, String[] fields, Analyzer analyzer)
        throws ParseException
        BooleanQuery bQuery = new BooleanQuery();
        for (int i = 0; i < fields.length; i++)
            Query q = parse(query, fields[i], analyzer);
            bQuery.add(q, false, false);
        return bQuery;
     * Copied from QueryParser, but defaults to AND searches.
    static public Query parse(String query, String field, Analyzer analyzer)
         throws ParseException {
        QueryParser parser = new QueryParser(field, analyzer);
        return parser.parse(query);

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

View raw message