~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to lucene/contrib/queries/src/java/org/apache/lucene/search/similar/MoreLikeThisQuery.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Created on 25-Jan-2006
3
 
 */
4
 
package org.apache.lucene.search.similar;
5
 
 
6
 
/**
7
 
 * Licensed to the Apache Software Foundation (ASF) under one or more
8
 
 * contributor license agreements.  See the NOTICE file distributed with
9
 
 * this work for additional information regarding copyright ownership.
10
 
 * The ASF licenses this file to You under the Apache License, Version 2.0
11
 
 * (the "License"); you may not use this file except in compliance with
12
 
 * the License.  You may obtain a copy of the License at
13
 
 *
14
 
 *     http://www.apache.org/licenses/LICENSE-2.0
15
 
 *
16
 
 * Unless required by applicable law or agreed to in writing, software
17
 
 * distributed under the License is distributed on an "AS IS" BASIS,
18
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
 
 * See the License for the specific language governing permissions and
20
 
 * limitations under the License.
21
 
 */
22
 
 
23
 
import java.io.IOException;
24
 
import java.util.Set;
25
 
 
26
 
import org.apache.lucene.analysis.Analyzer;
27
 
import org.apache.lucene.index.IndexReader;
28
 
import org.apache.lucene.search.BooleanClause;
29
 
import org.apache.lucene.search.BooleanQuery;
30
 
import org.apache.lucene.search.Query;
31
 
import org.apache.lucene.search.similar.MoreLikeThis;
32
 
 
33
 
import java.io.StringReader;
34
 
 
35
 
/**
36
 
 * A simple wrapper for MoreLikeThis for use in scenarios where a Query object
37
 
 * is required eg in custom QueryParser extensions. At query.rewrite() time the
38
 
 * reader is used to construct the actual MoreLikeThis object and obtain the
39
 
 * real Query object.
40
 
 */
41
 
public class MoreLikeThisQuery extends Query {
42
 
  
43
 
  private String likeText;
44
 
  private String[] moreLikeFields;
45
 
  private Analyzer analyzer;
46
 
  private String fieldName;
47
 
  private float percentTermsToMatch = 0.3f;
48
 
  private int minTermFrequency = 1;
49
 
  private int maxQueryTerms = 5;
50
 
  private Set<?> stopWords = null;
51
 
  private int minDocFreq = -1;
52
 
 
53
 
  /** @deprecated use {@link #MoreLikeThisQuery(String, String[], Analyzer, String)} instead. */
54
 
  @Deprecated
55
 
  public MoreLikeThisQuery(String likeText, String[] moreLikeFields, Analyzer analyzer) {
56
 
    this(likeText, moreLikeFields, analyzer, moreLikeFields[0]);
57
 
  }
58
 
 
59
 
  /**
60
 
   * @param moreLikeFields
61
 
   */
62
 
  public MoreLikeThisQuery(String likeText, String[] moreLikeFields, Analyzer analyzer, String fieldName) {
63
 
    this.likeText = likeText;
64
 
    this.moreLikeFields = moreLikeFields;
65
 
    this.analyzer = analyzer;
66
 
    this.fieldName = fieldName;
67
 
  }
68
 
  
69
 
  @Override
70
 
  public Query rewrite(IndexReader reader) throws IOException {
71
 
    MoreLikeThis mlt = new MoreLikeThis(reader);
72
 
    
73
 
    mlt.setFieldNames(moreLikeFields);
74
 
    mlt.setAnalyzer(analyzer);
75
 
    mlt.setMinTermFreq(minTermFrequency);
76
 
    if (minDocFreq >= 0) {
77
 
      mlt.setMinDocFreq(minDocFreq);
78
 
    }
79
 
    mlt.setMaxQueryTerms(maxQueryTerms);
80
 
    mlt.setStopWords(stopWords);
81
 
    BooleanQuery bq = (BooleanQuery) mlt.like(new StringReader(likeText), fieldName);
82
 
    BooleanClause[] clauses = bq.getClauses();
83
 
    // make at least half the terms match
84
 
    bq.setMinimumNumberShouldMatch((int) (clauses.length * percentTermsToMatch));
85
 
    return bq;
86
 
  }
87
 
  
88
 
  /*
89
 
   * (non-Javadoc)
90
 
   * 
91
 
   * @see org.apache.lucene.search.Query#toString(java.lang.String)
92
 
   */
93
 
  @Override
94
 
  public String toString(String field) {
95
 
    return "like:" + likeText;
96
 
  }
97
 
  
98
 
  public float getPercentTermsToMatch() {
99
 
    return percentTermsToMatch;
100
 
  }
101
 
  
102
 
  public void setPercentTermsToMatch(float percentTermsToMatch) {
103
 
    this.percentTermsToMatch = percentTermsToMatch;
104
 
  }
105
 
  
106
 
  public Analyzer getAnalyzer() {
107
 
    return analyzer;
108
 
  }
109
 
  
110
 
  public void setAnalyzer(Analyzer analyzer) {
111
 
    this.analyzer = analyzer;
112
 
  }
113
 
  
114
 
  public String getLikeText() {
115
 
    return likeText;
116
 
  }
117
 
  
118
 
  public void setLikeText(String likeText) {
119
 
    this.likeText = likeText;
120
 
  }
121
 
  
122
 
  public int getMaxQueryTerms() {
123
 
    return maxQueryTerms;
124
 
  }
125
 
  
126
 
  public void setMaxQueryTerms(int maxQueryTerms) {
127
 
    this.maxQueryTerms = maxQueryTerms;
128
 
  }
129
 
  
130
 
  public int getMinTermFrequency() {
131
 
    return minTermFrequency;
132
 
  }
133
 
  
134
 
  public void setMinTermFrequency(int minTermFrequency) {
135
 
    this.minTermFrequency = minTermFrequency;
136
 
  }
137
 
  
138
 
  public String[] getMoreLikeFields() {
139
 
    return moreLikeFields;
140
 
  }
141
 
  
142
 
  public void setMoreLikeFields(String[] moreLikeFields) {
143
 
    this.moreLikeFields = moreLikeFields;
144
 
  }
145
 
  
146
 
  public Set<?> getStopWords() {
147
 
    return stopWords;
148
 
  }
149
 
  
150
 
  public void setStopWords(Set<?> stopWords) {
151
 
    this.stopWords = stopWords;
152
 
  }
153
 
  
154
 
  public int getMinDocFreq() {
155
 
    return minDocFreq;
156
 
  }
157
 
  
158
 
  public void setMinDocFreq(int minDocFreq) {
159
 
    this.minDocFreq = minDocFreq;
160
 
  }
161
 
}