2 using Lucene
.Net
.Analysis
;
3 using Lucene
.Net
.Search
;
5 namespace Lucene
.Net
.QueryParsers
7 /* ====================================================================
8 * The Apache Software License, Version 1.1
10 * Copyright (c) 2001 The Apache Software Foundation. All rights
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in
22 * the documentation and/or other materials provided with the
25 * 3. The end-user documentation included with the redistribution,
26 * if any, must include the following acknowledgment:
27 * "This product includes software developed by the
28 * Apache Software Foundation (http://www.apache.org/)."
29 * Alternately, this acknowledgment may appear in the software itself,
30 * if and wherever such third-party acknowledgments normally appear.
32 * 4. The names "Apache" and "Apache Software Foundation"
33 * must not be used to endorse or promote products
34 * derived from this software without prior written permission. For
35 * written permission, please contact apache@apache.org.
37 * 5. Products derived from this software may not be called "Apache",
38 * nor may "Apache" appear in their name, without
39 * prior written permission of the Apache Software Foundation.
41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * ====================================================================
55 * This software consists of voluntary contributions made by many
56 * individuals on behalf of the Apache Software Foundation. For more
57 * information on the Apache Software Foundation, please see
58 * <http://www.apache.org/>.
62 /// A QueryParser which constructs queries to search multiple fields.
64 /// <author><a href="mailto:kelvin@relevanz.com">Kelvin Tan</a></author>
65 /// <version>$Revision: 1.1.1.1 $</version>
66 public class MultiFieldQueryParser
: QueryParser
68 public const int NORMAL_FIELD
= 0;
69 public const int REQUIRED_FIELD
= 1;
70 public const int PROHIBITED_FIELD
= 2;
72 public MultiFieldQueryParser(QueryParserTokenManager tm
) : base(tm
)
76 public MultiFieldQueryParser(CharStream stream
) : base(stream
)
80 public MultiFieldQueryParser(String f
, Analyzer a
) : base(f
, a
)
85 /// <p>Parses a query which searches on the fields specified.</p>
86 /// <p>If x fields are specified, this effectively constructs:</p>
89 /// (field1:query) (field2:query) (field3:query)...(fieldx:query)
93 /// <param name="query">Query string to parse</param>
94 /// <param name="fields">Fields to search on</param>
95 /// <param name="analyzer">Analyzer to use</param>
96 /// <returns></returns>
97 /// <throws>ParserException if query parsing fails</throws>
98 /// <throws>TokenMgrError if query parsing fails</throws>
99 public static Query
Parse(String query
, String
[] fields
, Analyzer analyzer
)
101 BooleanQuery bQuery
= new BooleanQuery();
102 for (int i
= 0; i
< fields
.Length
; i
++)
104 Query q
= Parse(query
, fields
[i
], analyzer
);
105 bQuery
.Add(q
, false, false);
112 /// Parses a query, searching on the fields specified.
113 /// Use this if you need to specify certain fields as required,
114 /// and others as prohibited.</p>
118 /// String[] fields = {"filename", "contents", "description"};
119 /// int[] flags = {MultiFieldQueryParser.NORMAL FIELD,
120 /// MultiFieldQueryParser.REQUIRED FIELD,
121 /// MultiFieldQueryParser.PROHIBITED FIELD,};
122 /// Parse(query, fields, flags, analyzer);
126 /// The code above would construct a query:
129 /// (filename:query) +(contents:query) -(description:query)
133 /// <param name="query">Query string to parse</param>
134 /// <param name="fields">Fields to search on</param>
135 /// <param name="flags">Flags describing the fields</param>
136 /// <param name="analyzer">Analyzer to use</param>
137 /// <returns></returns>
138 /// <throws>ParserException if query parsing fails</throws>
139 /// <throws>TokenMgrError if query parsing fails</throws>
140 public static Query
Parse(String query
, String
[] fields
, int[] flags
,
143 BooleanQuery bQuery
= new BooleanQuery();
144 for (int i
= 0; i
< fields
.Length
; i
++)
146 Query q
= Parse(query
, fields
[i
], analyzer
);
151 bQuery
.Add(q
, true, false);
153 case PROHIBITED_FIELD
:
154 bQuery
.Add(q
, false, true);
157 bQuery
.Add(q
, false, false);