QueryResponses.cs, DumpIndex.cs, IQueryResult.cs, QueryExecutor.cs, QueryResult.cs...
[beagle.git] / beagled / Lucene.Net / Search / Regex / RegexTermEnum.cs
blobfdafa7d4361ef3026b5569b7b2e2c3e2819bae6b
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.
17 using System;
18 using IndexReader = Lucene.Net.Index.IndexReader;
19 using Term = Lucene.Net.Index.Term;
20 using FilteredTermEnum = Lucene.Net.Search.FilteredTermEnum;
21 using Pattern = System.Text.RegularExpressions.Regex;
23 namespace Lucene.Net.Search.Regex
26 public class RegexTermEnum : FilteredTermEnum
28 private System.String field = "";
29 private System.String pre = "";
30 internal bool endEnum = false;
31 private Pattern pattern;
33 public RegexTermEnum(IndexReader reader, Term term) : base()
35 field = term.Field();
36 System.String text = term.Text();
38 pattern = new Pattern(text);
40 // Find the first regex character position, to find the
41 // maximum prefix to use for term enumeration
42 int index = 0;
43 while (index < text.Length)
45 char c = text[index];
47 if (!System.Char.IsLetterOrDigit(c))
48 break;
50 index++;
53 pre = text.Substring(0, (index) - (0));
55 SetEnum(reader.Terms(new Term(term.Field(), pre)));
58 protected internal override bool TermCompare(Term term)
60 if ((System.Object) field == (System.Object) term.Field())
62 System.String searchText = term.Text();
63 if (searchText.StartsWith(pre))
65 return pattern.Match(searchText).Success;
68 endEnum = true;
69 return false;
72 public override float Difference()
74 // TODO: adjust difference based on distance of searchTerm.text() and term().text()
75 return 1.0f;
78 public override bool EndEnum()
80 return endEnum;
83 public override void Close()
85 base.Close();
86 field = null;