Lucene.Net  3.0.3
Lucene.Net is a .NET port of the Java Lucene Indexing Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties
SimilarityQueries.cs
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 using System;
19 using System.Collections.Generic;
20 using Lucene.Net.Analysis;
21 using Lucene.Net.Index;
22 using Lucene.Net.Analysis.Tokenattributes;
23 
24 namespace Lucene.Net.Search.Similar
25 {
26 
33  public sealed class SimilarityQueries
34  {
36  private SimilarityQueries()
37  {
38  }
39 
80  public static Query FormSimilarQuery(System.String body, Analyzer a, System.String field, ISet<string> stop)
81  {
82  TokenStream ts = a.TokenStream(field, new System.IO.StringReader(body));
83  ITermAttribute termAtt = ts.AddAttribute<ITermAttribute>();
84 
85  BooleanQuery tmp = new BooleanQuery();
86  ISet<string> already = Lucene.Net.Support.Compatibility.SetFactory.CreateHashSet<string>(); // ignore dups
87  while (ts.IncrementToken())
88  {
89  String word = termAtt.Term;
90  // ignore opt stop words
91  if (stop != null && stop.Contains(word))
92  continue;
93  // ignore dups
94  if (already.Contains(word))
95  continue;
96  already.Add(word);
97  // add to query
98  TermQuery tq = new TermQuery(new Term(field, word));
99  try
100  {
101  tmp.Add(tq, Occur.SHOULD);
102  }
103  catch (BooleanQuery.TooManyClauses)
104  {
105  // fail-safe, just return what we have, not the end of the world
106  break;
107  }
108  }
109  return tmp;
110  }
111  }
112 }