lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Thomas Pl├╝mpe <thoman...@gmx.de>
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 = searcher.search(q);
> 
> 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
	parser.setOperator(QueryParser.DEFAULT_OPERATOR_AND);
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

Thomas


    /**
     * 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);
        parser.setOperator(QueryParser.DEFAULT_OPERATOR_AND);
        return parser.parse(query);
    }



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


Mime
View raw message