Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
MoreLikeThisQuery.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 
19 using System;
20 using System.Collections.Generic;
21 using System.Linq;
22 using System.Text;
23 
24 using Lucene.Net.Search;
25 using Lucene.Net.Analysis;
26 using Lucene.Net.Index;
27 
28 namespace Lucene.Net.Search.Similar
29 {
30  /*<summary>
31  * A simple wrapper for MoreLikeThis for use in scenarios where a Query object is required eg
32  * in custom QueryParser extensions. At query.rewrite() time the reader is used to construct the
33  * actual MoreLikeThis object and obtain the real Query object.
34  * </summary>
35  */
36  public class MoreLikeThisQuery : Query
37  {
38  private String likeText;
39  private String[] moreLikeFields;
40  private Analyzer analyzer;
41  float percentTermsToMatch = 0.3f;
42  int minTermFrequency = 1;
43  int maxQueryTerms = 5;
44  ISet<string> stopWords = null;
45  int minDocFreq = -1;
46 
47 
48  /*<summary></summary>
49  * <param name="moreLikeFields"></param>
50  * <param name="likeText"></param>
51  * <param name="analyzer"></param>
52  */
53  public MoreLikeThisQuery(String likeText, String[] moreLikeFields, Analyzer analyzer)
54  {
55  this.likeText = likeText;
56  this.moreLikeFields = moreLikeFields;
57  this.analyzer = analyzer;
58  }
59 
60  public override Query Rewrite(IndexReader reader)
61  {
62  MoreLikeThis mlt = new MoreLikeThis(reader);
63 
64  mlt.SetFieldNames(moreLikeFields);
65  mlt.Analyzer = analyzer;
66  mlt.MinTermFreq = minTermFrequency;
67  if (minDocFreq >= 0)
68  {
69  mlt.MinDocFreq = minDocFreq;
70  }
71  mlt.MaxQueryTerms = maxQueryTerms;
72  mlt.SetStopWords(stopWords);
73  BooleanQuery bq = (BooleanQuery)mlt.Like( new System.IO.StringReader(likeText));
74  BooleanClause[] clauses = bq.GetClauses();
75  //make at least half the terms match
76  bq.MinimumNumberShouldMatch = (int)(clauses.Length * percentTermsToMatch);
77  return bq;
78  }
79  /* (non-Javadoc)
80  * <see cref="org.apache.lucene.search.Query.toString(java.lang.String)"/>
81  */
82  public override String ToString(String field)
83  {
84  return "like:" + likeText;
85  }
86 
87  public float PercentTermsToMatch
88  {
89  get { return percentTermsToMatch; }
90  set { this.percentTermsToMatch = value; }
91  }
92 
93  public Analyzer Analyzer
94  {
95  get { return analyzer; }
96  set { this.analyzer = value; }
97  }
98 
99  public string LikeText
100  {
101  get { return likeText; }
102  set { this.likeText = value; }
103  }
104 
105  public int MaxQueryTerms
106  {
107  get { return maxQueryTerms; }
108  set { this.maxQueryTerms = value; }
109  }
110 
111  public int MinTermFrequency
112  {
113  get { return minTermFrequency; }
114  set { this.minTermFrequency = value; }
115  }
116 
117  public String[] GetMoreLikeFields()
118  {
119  return moreLikeFields;
120  }
121 
122  public void SetMoreLikeFields(String[] moreLikeFields)
123  {
124  this.moreLikeFields = moreLikeFields;
125  }
126  public ISet<string> GetStopWords()
127  {
128  return stopWords;
129  }
130  public void SetStopWords(ISet<string> stopWords)
131  {
132  this.stopWords = stopWords;
133  }
134 
135  public int MinDocFreq
136  {
137  get { return minDocFreq; }
138  set { this.minDocFreq = value; }
139  }
140  }
141 }