2 * Created on 25-Jan-2006
4 package org.apache.lucene.search.similar;
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
14 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 import java.io.IOException;
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;
33 import java.io.StringReader;
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
41 public class MoreLikeThisQuery extends Query {
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;
53 /** @deprecated use {@link #MoreLikeThisQuery(String, String[], Analyzer, String)} instead. */
55 public MoreLikeThisQuery(String likeText, String[] moreLikeFields, Analyzer analyzer) {
56 this(likeText, moreLikeFields, analyzer, moreLikeFields[0]);
60 * @param moreLikeFields
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;
70 public Query rewrite(IndexReader reader) throws IOException {
71 MoreLikeThis mlt = new MoreLikeThis(reader);
73 mlt.setFieldNames(moreLikeFields);
74 mlt.setAnalyzer(analyzer);
75 mlt.setMinTermFreq(minTermFrequency);
76 if (minDocFreq >= 0) {
77 mlt.setMinDocFreq(minDocFreq);
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));
91 * @see org.apache.lucene.search.Query#toString(java.lang.String)
94 public String toString(String field) {
95 return "like:" + likeText;
98 public float getPercentTermsToMatch() {
99 return percentTermsToMatch;
102 public void setPercentTermsToMatch(float percentTermsToMatch) {
103 this.percentTermsToMatch = percentTermsToMatch;
106 public Analyzer getAnalyzer() {
110 public void setAnalyzer(Analyzer analyzer) {
111 this.analyzer = analyzer;
114 public String getLikeText() {
118 public void setLikeText(String likeText) {
119 this.likeText = likeText;
122 public int getMaxQueryTerms() {
123 return maxQueryTerms;
126 public void setMaxQueryTerms(int maxQueryTerms) {
127 this.maxQueryTerms = maxQueryTerms;
130 public int getMinTermFrequency() {
131 return minTermFrequency;
134 public void setMinTermFrequency(int minTermFrequency) {
135 this.minTermFrequency = minTermFrequency;
138 public String[] getMoreLikeFields() {
139 return moreLikeFields;
142 public void setMoreLikeFields(String[] moreLikeFields) {
143 this.moreLikeFields = moreLikeFields;
146 public Set<?> getStopWords() {
150 public void setStopWords(Set<?> stopWords) {
151 this.stopWords = stopWords;
154 public int getMinDocFreq() {
158 public void setMinDocFreq(int minDocFreq) {
159 this.minDocFreq = minDocFreq;