From 5bd8e1b3370a6aba29abe7e50d069581cdc56594 Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Sun, 28 Oct 2012 09:59:45 +0100 Subject: [PATCH] lucene 4: fixed MLT query --- .../common/lucene/search/MoreLikeThisQuery.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/elasticsearch/common/lucene/search/MoreLikeThisQuery.java b/src/main/java/org/elasticsearch/common/lucene/search/MoreLikeThisQuery.java index cae14843d2a..170fdd72e46 100644 --- a/src/main/java/org/elasticsearch/common/lucene/search/MoreLikeThisQuery.java +++ b/src/main/java/org/elasticsearch/common/lucene/search/MoreLikeThisQuery.java @@ -22,7 +22,10 @@ package org.elasticsearch.common.lucene.search; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.index.IndexReader; import org.apache.lucene.search.*; -import org.apache.lucene.search.similar.MoreLikeThis; +import org.apache.lucene.search.similarities.DefaultSimilarity; +import org.apache.lucene.search.similarities.Similarity; +import org.apache.lucene.search.similarities.TFIDFSimilarity; +import org.apache.lucene.queries.mlt.MoreLikeThis; import org.elasticsearch.common.io.FastStringReader; import java.io.IOException; @@ -35,7 +38,7 @@ public class MoreLikeThisQuery extends Query { public static final float DEFAULT_PERCENT_TERMS_TO_MATCH = 0.3f; - private Similarity similarity; + private TFIDFSimilarity similarity; private String likeText; private String[] moreLikeFields; @@ -77,7 +80,8 @@ public class MoreLikeThisQuery extends Query { mlt.setStopWords(stopWords); mlt.setBoost(boostTerms); mlt.setBoostFactor(boostTermsFactor); - BooleanQuery bq = (BooleanQuery) mlt.like(new FastStringReader(likeText)); + //LUCENE 4 UPGRADE this mapps the 3.6 behavior (only use the first field) + BooleanQuery bq = (BooleanQuery) mlt.like(new FastStringReader(likeText), moreLikeFields[0]); BooleanClause[] clauses = bq.getClauses(); bq.setMinimumNumberShouldMatch((int) (clauses.length * percentTermsToMatch)); @@ -112,7 +116,10 @@ public class MoreLikeThisQuery extends Query { } public void setSimilarity(Similarity similarity) { - this.similarity = similarity; + if (similarity == null || similarity instanceof TFIDFSimilarity) { + //LUCENE 4 UPGRADE we need TFIDF similarity here so I only set it if it is an instance of it + this.similarity = (TFIDFSimilarity) similarity; + } } public Analyzer getAnalyzer() {