Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/telefonicasoluciones/search/server/util/MultiFieldQueryParser.java


1   package com.telefonicasoluciones.search.server.util;
2   
3   /* ====================================================================
4    * The Apache Software License, Version 1.1
5    *
6    * Copyright (c) 2001 The Apache Software Foundation.  All rights
7    * reserved.
8    *
9    * Redistribution and use in source and binary forms, with or without
10   * modification, are permitted provided that the following conditions
11   * are met:
12   *
13   * 1. Redistributions of source code must retain the above copyright
14   *    notice, this list of conditions and the following disclaimer.
15   *
16   * 2. Redistributions in binary form must reproduce the above copyright
17   *    notice, this list of conditions and the following disclaimer in
18   *    the documentation and/or other materials provided with the
19   *    distribution.
20   *
21   * 3. The end-user documentation included with the redistribution,
22   *    if any, must include the following acknowledgment:
23   *       "This product includes software developed by the
24   *        Apache Software Foundation (http://www.apache.org/)."
25   *    Alternately, this acknowledgment may appear in the software itself,
26   *    if and wherever such third-party acknowledgments normally appear.
27   *
28   * 4. The names "Apache" and "Apache Software Foundation"
29   *    must not be used to endorse or promote products
30   *    derived from this software without prior written permission. For
31   *    written permission, please contact apache@apache.org.
32   *
33   * 5. Products derived from this software may not be called "Apache",
34   *    nor may "Apache" appear in their name, without
35   *    prior written permission of the Apache Software Foundation.
36   *
37   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48   * SUCH DAMAGE.
49   * ====================================================================
50   *
51   * This software consists of voluntary contributions made by many
52   * individuals on behalf of the Apache Software Foundation.  For more
53   * information on the Apache Software Foundation, please see
54   * <http://www.apache.org/>.
55   */
56  
57  import org.apache.lucene.analysis.Analyzer;
58  import org.apache.lucene.queryParser.CharStream;
59  import org.apache.lucene.queryParser.ParseException;
60  import org.apache.lucene.queryParser.QueryParser;
61  import org.apache.lucene.queryParser.QueryParserTokenManager;
62  import org.apache.lucene.search.BooleanQuery;
63  import org.apache.lucene.search.Query;
64  import java.util.HashMap;
65  
66  /**
67   * A QueryParser which constructs queries to search multiple fields.
68   *
69   * @author <a href="mailto:kelvin@relevanz.com">Kelvin Tan</a>
70   * @version $Revision: 1.2 $
71   */
72  public class MultiFieldQueryParser extends QueryParser
73  {
74      public static final int NORMAL_FIELD     = 0;
75      public static final int REQUIRED_FIELD   = 1;
76      public static final int PROHIBITED_FIELD = 2;
77  
78      public MultiFieldQueryParser(QueryParserTokenManager tm)
79      {
80          super(tm);
81      }
82  
83      public MultiFieldQueryParser(CharStream stream)
84      {
85          super(stream);
86      }
87  
88      public MultiFieldQueryParser(String f, Analyzer a)
89      {
90          super(f, a);
91      }
92  
93      /**
94       * <p>
95       * Parses a query which searches on the fields specified.
96       * <p>
97       * If x fields are specified, this effectively constructs:
98       * <pre>
99       * <code>
100      * (field1:query) (field2:query) (field3:query)...(fieldx:query)
101      * </code>
102      * </pre>
103      *
104      * @param query Query string to parse
105      * @param fields Fields to search on
106      * @param analyzer Analyzer to use
107      * @throws ParserException if query parsing fails
108      * @throws TokenMgrError if query parsing fails
109      */
110     public static Query parse(String query, String[] fields, Analyzer analyzer)
111   throws ParseException
112     {
113         BooleanQuery bQuery = new BooleanQuery();
114         for (int i = fields.length; --i > 0 ; ) {
115             Query q = parse(query, fields[i], analyzer);
116             bQuery.add(q, false, false);
117         }
118         return bQuery;
119     }
120 
121     /**
122      * <p>
123      * Parses a query, searching on the fields specified.
124      * Use this if you need to specify certain fields as required,
125      * and others as prohibited.
126      * <p><pre>
127      * Usage:
128      * <code>
129      * String[] fields = {"filename", "contents", "description"};
130      * int[] flags = {MultiFieldQueryParser.NORMAL FIELD,
131      *                MultiFieldQueryParser.REQUIRED FIELD,
132      *                MultiFieldQueryParser.PROHIBITED FIELD,};
133      * parse(query, fields, flags, analyzer);
134      * </code>
135      * </pre>
136      *<p>
137      * The code above would construct a query:
138      * <pre>
139      * <code>
140      * (filename:query) +(contents:query) -(description:query)
141      * </code>
142      * </pre>
143      *
144      * @param query Query string to parse
145      * @param fields Fields to search on
146      * @param flags Flags describing the fields
147      * @param analyzer Analyzer to use
148      * @throws ParserException if query parsing fails
149      * @throws TokenMgrError if query parsing fails
150      */
151     public static Query parse(String query, String[] fields, int[] flags,
152   Analyzer analyzer)
153   throws ParseException
154     {
155         BooleanQuery bQuery = new BooleanQuery();
156         for (int i = 0; i < fields.length; i++) {
157             Query q = parse(query, fields[i], analyzer);
158             int flag = flags[i];
159             switch (flag)
160             {
161                 case REQUIRED_FIELD:
162                     bQuery.add(q, true, false);
163                     break;
164                 case PROHIBITED_FIELD:
165                     bQuery.add(q, false, true);
166                     break;
167                 default:
168                     bQuery.add(q, false, false);
169                     break;
170             }
171         }
172         return bQuery;
173     }
174     public static Query parse(HashMap fields, Analyzer analyzer)
175   throws ParseException
176     {
177         BooleanQuery bQuery = new BooleanQuery();
178         Object keys[] = fields.keySet().toArray();
179         for (int i = keys.length; --i >= 0 ; ) {
180             Query q = parse((String) fields.get(keys[i]), (String) keys[i], analyzer);
181             bQuery.add(q, true, false);
182         }
183         return bQuery;
184     }
185 }