Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / beagled / Lucene.Net / QueryParser / MultiFieldQueryParser.cs
bloba2289fc01dac1996b51c837c309a9a18569032e7
1 /*
2 * Copyright 2004 The Apache Software Foundation
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
16 using System;
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.
24 ///
25 /// </summary>
26 /// <author> <a href="mailto:kelvin@relevanz.com">Kelvin Tan</a>
27 /// </author>
28 /// <version> $Revision: 1.2 $
29 /// </version>
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)
48 /// <summary> <p>
49 /// Parses a query which searches on the fields specified.
50 /// <p>
51 /// If x fields are specified, this effectively constructs:
52 /// <pre>
53 /// <code>
54 /// (field1:query) (field2:query) (field3:query)...(fieldx:query)
55 /// </code>
56 /// </pre>
57 ///
58 /// </summary>
59 /// <param name="query">Query string to parse
60 /// </param>
61 /// <param name="fields">Fields to search on
62 /// </param>
63 /// <param name="analyzer">Analyzer to use
64 /// </param>
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);
75 return bQuery;
78 /// <summary> <p>
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.
82 /// <p><pre>
83 /// Usage:
84 /// <code>
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);
90 /// </code>
91 /// </pre>
92 /// <p>
93 /// The code above would construct a query:
94 /// <pre>
95 /// <code>
96 /// (filename:query) +(contents:query) -(description:query)
97 /// </code>
98 /// </pre>
99 ///
100 /// </summary>
101 /// <param name="query">Query string to parse
102 /// </param>
103 /// <param name="fields">Fields to search on
104 /// </param>
105 /// <param name="flags">Flags describing the fields
106 /// </param>
107 /// <param name="analyzer">Analyzer to use
108 /// </param>
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);
117 int flag = flags[i];
118 switch (flag)
121 case REQUIRED_FIELD:
122 bQuery.Add(q, true, false);
123 break;
125 case PROHIBITED_FIELD:
126 bQuery.Add(q, false, true);
127 break;
129 default:
130 bQuery.Add(q, false, false);
131 break;
135 return bQuery;