2 * Copyright 2004 The Apache Software Foundation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 using Analyzer
= Lucene
.Net
.Analysis
.Analyzer
;
18 using BooleanQuery
= Lucene
.Net
.Search
.BooleanQuery
;
19 using Query
= Lucene
.Net
.Search
.Query
;
20 namespace Lucene
.Net
.QueryParsers
23 /// <summary> A QueryParser which constructs queries to search multiple fields.
26 /// <author> <a href="mailto:kelvin@relevanz.com">Kelvin Tan</a>
28 /// <version> $Revision: 1.2 $
30 public class MultiFieldQueryParser
: QueryParser
32 public const int NORMAL_FIELD
= 0;
33 public const int REQUIRED_FIELD
= 1;
34 public const int PROHIBITED_FIELD
= 2;
36 public MultiFieldQueryParser(QueryParserTokenManager tm
):base(tm
)
40 public MultiFieldQueryParser(CharStream stream
):base(stream
)
44 public MultiFieldQueryParser(System
.String f
, Analyzer a
):base(f
, a
)
49 /// Parses a query which searches on the fields specified.
51 /// If x fields are specified, this effectively constructs:
54 /// (field1:query) (field2:query) (field3:query)...(fieldx:query)
59 /// <param name="query">Query string to parse
61 /// <param name="fields">Fields to search on
63 /// <param name="analyzer">Analyzer to use
65 /// <throws> ParseException if query parsing fails </throws>
66 /// <throws> TokenMgrError if query parsing fails </throws>
67 public static Query
Parse(System
.String query
, System
.String
[] fields
, Analyzer analyzer
)
69 BooleanQuery bQuery
= new BooleanQuery();
70 for (int i
= 0; i
< fields
.Length
; i
++)
72 Query q
= Parse(query
, fields
[i
], analyzer
);
73 bQuery
.Add(q
, false, false);
79 /// Parses a query, searching on the fields specified.
80 /// Use this if you need to specify certain fields as required,
81 /// and others as prohibited.
85 /// String[] fields = {"filename", "contents", "description"};
86 /// int[] flags = {MultiFieldQueryParser.NORMAL FIELD,
87 /// MultiFieldQueryParser.REQUIRED FIELD,
88 /// MultiFieldQueryParser.PROHIBITED FIELD,};
89 /// parse(query, fields, flags, analyzer);
93 /// The code above would construct a query:
96 /// (filename:query) +(contents:query) -(description:query)
101 /// <param name="query">Query string to parse
103 /// <param name="fields">Fields to search on
105 /// <param name="flags">Flags describing the fields
107 /// <param name="analyzer">Analyzer to use
109 /// <throws> ParseException if query parsing fails </throws>
110 /// <throws> TokenMgrError if query parsing fails </throws>
111 public static Query
Parse(System
.String query
, System
.String
[] fields
, int[] flags
, Analyzer analyzer
)
113 BooleanQuery bQuery
= new BooleanQuery();
114 for (int i
= 0; i
< fields
.Length
; i
++)
116 Query q
= Parse(query
, fields
[i
], analyzer
);
122 bQuery
.Add(q
, true, false);
125 case PROHIBITED_FIELD
:
126 bQuery
.Add(q
, false, true);
130 bQuery
.Add(q
, false, false);