lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From o...@apache.org
Subject cvs commit: jakarta-lucene/src/java/org/apache/lucene/queryParser MultiFieldQueryParser.java
Date Mon, 23 Aug 2004 23:07:42 GMT
otis        2004/08/23 16:07:42

  Modified:    src/java/org/apache/lucene/queryParser
                        MultiFieldQueryParser.java
  Log:
  - Added 2 new parse methods that take arrays of query strings.
    Submited by Andraz Skoric (andraz.skoric@medius.si) back in April 2004
  
  Revision  Changes    Path
  1.5       +89 -1     jakarta-lucene/src/java/org/apache/lucene/queryParser/MultiFieldQueryParser.java
  
  Index: MultiFieldQueryParser.java
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/src/java/org/apache/lucene/queryParser/MultiFieldQueryParser.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- MultiFieldQueryParser.java	29 Mar 2004 22:48:03 -0000	1.4
  +++ MultiFieldQueryParser.java	23 Aug 2004 23:07:41 -0000	1.5
  @@ -82,6 +82,37 @@
   
       /**
        * <p>
  +     * Parses a query which searches on the fields specified.
  +     * <p>
  +     * If x fields are specified, this effectively constructs:
  +     * <pre>
  +     * <code>
  +     * (field1:query1) (field2:query2) (field3:query3)...(fieldx:queryx)
  +     * </code>
  +     * </pre>
  +     * @param queries Queries strings to parse
  +     * @param fields Fields to search on
  +     * @param analyzer Analyzer to use
  +     * @throws ParseException if query parsing fails
  +     * @throws TokenMgrError if query parsing fails
  +     */
  +    public static Query parse(String[] queries, String[] fields,
  +        Analyzer analyzer) throws ParseException
  +    {
  +        if (queries.length != fields.length)
  +            // TODO Exception handling
  +            throw new ParseException("queries.length != fields.length");
  +        BooleanQuery bQuery = new BooleanQuery();
  +        for (int i = 0; i < fields.length; i++)
  +        {
  +            Query q = parse(queries[i], fields[i], analyzer);
  +            bQuery.add(q, false, false);
  +        }
  +        return bQuery;
  +    }
  +
  +    /**
  +     * <p>
        * Parses a query, searching on the fields specified.
        * Use this if you need to specify certain fields as required,
        * and others as prohibited.
  @@ -118,6 +149,63 @@
           for (int i = 0; i < fields.length; i++)
           {
               Query q = parse(query, fields[i], analyzer);
  +            int flag = flags[i];
  +            switch (flag)
  +            {
  +                case REQUIRED_FIELD:
  +                    bQuery.add(q, true, false);
  +                    break;
  +                case PROHIBITED_FIELD:
  +                    bQuery.add(q, false, true);
  +                    break;
  +                default:
  +                    bQuery.add(q, false, false);
  +                    break;
  +            }
  +        }
  +        return bQuery;
  +    }
  +
  +    /**
  +     * <p>
  +     * Parses a query, searching on the fields specified.
  +     * Use this if you need to specify certain fields as required,
  +     * and others as prohibited.
  +     * <p><pre>
  +     * Usage:
  +     * <code>
  +     * String[] fields = {"filename", "contents", "description"};
  +     * int[] flags = {MultiFieldQueryParser.NORMAL FIELD,
  +     *                MultiFieldQueryParser.REQUIRED FIELD,
  +     *                MultiFieldQueryParser.PROHIBITED FIELD,};
  +     * parse(query, fields, flags, analyzer);
  +     * </code>
  +     * </pre>
  +     *<p>
  +     * The code above would construct a query:
  +     * <pre>
  +     * <code>
  +     * (filename:query1) +(contents:query2) -(description:query3)
  +     * </code>
  +     * </pre>
  +     *
  +     * @param queries Queries string to parse
  +     * @param fields Fields to search on
  +     * @param flags Flags describing the fields
  +     * @param analyzer Analyzer to use
  +     * @throws ParseException if query parsing fails
  +     * @throws TokenMgrError if query parsing fails
  +     */
  +    public static Query parse(String[] queries, String[] fields, int[] flags,
  +        Analyzer analyzer) throws ParseException
  +    {
  +        if (queries.length != fields.length)
  +            // TODO Exception handling
  +            throw new ParseException("queries.length != fields.length");
  +        BooleanQuery bQuery = new BooleanQuery();
  +        for (int i = 0; i < fields.length; i++)
  +        {
  +            Query q = parse(queries[i], fields[i], analyzer);
               int flag = flags[i];
               switch (flag)
               {
  
  
  

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


Mime
View raw message