From aac05e262905c58306a21cdadfecb13f6cd774be Mon Sep 17 00:00:00 2001 From: kimchy Date: Thu, 1 Apr 2010 00:35:54 +0300 Subject: [PATCH] inject similarity to parsing context of queries so it can be used --- .../index/query/IndexQueryParserService.java | 6 ++++-- .../index/query/json/JsonIndexQueryParser.java | 8 ++++++-- .../index/query/json/JsonQueryParseContext.java | 16 +++++++++++++++- .../json/MoreLikeThisFieldJsonQueryParser.java | 1 + .../query/json/MoreLikeThisJsonQueryParser.java | 1 + .../util/lucene/search/MoreLikeThisQuery.java | 16 ++++++++++++---- .../json/SimpleJsonIndexQueryParserTests.java | 2 +- .../json/guice/IndexQueryParserModuleTests.java | 2 ++ 8 files changed, 42 insertions(+), 10 deletions(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/IndexQueryParserService.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/IndexQueryParserService.java index cb09cd987c4..64dfcbda3f8 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/IndexQueryParserService.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/IndexQueryParserService.java @@ -28,6 +28,7 @@ import org.elasticsearch.index.cache.filter.FilterCache; import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.query.json.JsonIndexQueryParser; import org.elasticsearch.index.settings.IndexSettings; +import org.elasticsearch.index.similarity.SimilarityService; import org.elasticsearch.util.Nullable; import org.elasticsearch.util.settings.ImmutableSettings; import org.elasticsearch.util.settings.Settings; @@ -51,12 +52,13 @@ public class IndexQueryParserService extends AbstractIndexComponent { private final Map indexQueryParsers; public IndexQueryParserService(Index index, MapperService mapperService, FilterCache filterCache, AnalysisService analysisService) { - this(index, ImmutableSettings.Builder.EMPTY_SETTINGS, mapperService, filterCache, analysisService, null); + this(index, ImmutableSettings.Builder.EMPTY_SETTINGS, mapperService, filterCache, analysisService, null, null); } @Inject public IndexQueryParserService(Index index, @IndexSettings Settings indexSettings, MapperService mapperService, FilterCache filterCache, AnalysisService analysisService, + @Nullable SimilarityService similarityService, @Nullable Map indexQueryParsersFactories) { super(index, indexSettings); Map queryParserGroupSettings; @@ -74,7 +76,7 @@ public class IndexQueryParserService extends AbstractIndexComponent { } } if (!qparsers.containsKey(Defaults.DEFAULT)) { - IndexQueryParser defaultQueryParser = new JsonIndexQueryParser(index, indexSettings, mapperService, filterCache, analysisService, null, null, Defaults.DEFAULT, null); + IndexQueryParser defaultQueryParser = new JsonIndexQueryParser(index, indexSettings, mapperService, filterCache, analysisService, similarityService, null, null, Defaults.DEFAULT, null); qparsers.put(Defaults.DEFAULT, defaultQueryParser); } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/JsonIndexQueryParser.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/JsonIndexQueryParser.java index 28b7c49055a..280d95f083f 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/JsonIndexQueryParser.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/JsonIndexQueryParser.java @@ -34,6 +34,7 @@ import org.elasticsearch.index.query.IndexQueryParser; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryParsingException; import org.elasticsearch.index.settings.IndexSettings; +import org.elasticsearch.index.similarity.SimilarityService; import org.elasticsearch.util.Nullable; import org.elasticsearch.util.ThreadLocals; import org.elasticsearch.util.io.FastCharArrayReader; @@ -60,7 +61,7 @@ public class JsonIndexQueryParser extends AbstractIndexComponent implements Inde private ThreadLocal> cache = new ThreadLocal>() { @Override protected ThreadLocals.CleanableValue initialValue() { - return new ThreadLocals.CleanableValue(new JsonQueryParseContext(index, queryParserRegistry, mapperService, filterCache)); + return new ThreadLocals.CleanableValue(new JsonQueryParseContext(index, queryParserRegistry, mapperService, similarityService, filterCache)); } }; @@ -70,6 +71,8 @@ public class JsonIndexQueryParser extends AbstractIndexComponent implements Inde private final MapperService mapperService; + private final SimilarityService similarityService; + private final FilterCache filterCache; private final JsonQueryParserRegistry queryParserRegistry; @@ -77,13 +80,14 @@ public class JsonIndexQueryParser extends AbstractIndexComponent implements Inde @Inject public JsonIndexQueryParser(Index index, @IndexSettings Settings indexSettings, MapperService mapperService, FilterCache filterCache, - AnalysisService analysisService, + AnalysisService analysisService, @Nullable SimilarityService similarityService, @Nullable Map jsonQueryParsers, @Nullable Map jsonFilterParsers, @Assisted String name, @Assisted @Nullable Settings settings) { super(index, indexSettings); this.name = name; this.mapperService = mapperService; + this.similarityService = similarityService; this.filterCache = filterCache; List queryParsers = newArrayList(); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/JsonQueryParseContext.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/JsonQueryParseContext.java index 0c9466b14c9..b60c60abe7e 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/JsonQueryParseContext.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/JsonQueryParseContext.java @@ -21,6 +21,7 @@ package org.elasticsearch.index.query.json; import org.apache.lucene.search.Filter; import org.apache.lucene.search.Query; +import org.apache.lucene.search.Similarity; import org.codehaus.jackson.JsonParser; import org.codehaus.jackson.JsonToken; import org.elasticsearch.index.Index; @@ -29,6 +30,8 @@ import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.mapper.FieldMappers; import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.query.QueryParsingException; +import org.elasticsearch.index.similarity.SimilarityService; +import org.elasticsearch.util.Nullable; import java.io.IOException; @@ -41,6 +44,8 @@ public class JsonQueryParseContext { private final MapperService mapperService; + private final SimilarityService similarityService; + private final FilterCache filterCache; private final JsonQueryParserRegistry queryParserRegistry; @@ -48,10 +53,11 @@ public class JsonQueryParseContext { private JsonParser jp; public JsonQueryParseContext(Index index, JsonQueryParserRegistry queryParserRegistry, - MapperService mapperService, FilterCache filterCache) { + MapperService mapperService, SimilarityService similarityService, FilterCache filterCache) { this.index = index; this.queryParserRegistry = queryParserRegistry; this.mapperService = mapperService; + this.similarityService = similarityService; this.filterCache = filterCache; } @@ -67,6 +73,14 @@ public class JsonQueryParseContext { return mapperService; } + @Nullable public SimilarityService similarityService() { + return this.similarityService; + } + + public Similarity searchSimilarity() { + return similarityService != null ? similarityService.defaultSearchSimilarity() : null; + } + public FilterCache filterCache() { return filterCache; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/MoreLikeThisFieldJsonQueryParser.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/MoreLikeThisFieldJsonQueryParser.java index 930622ecccb..96315248c3d 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/MoreLikeThisFieldJsonQueryParser.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/MoreLikeThisFieldJsonQueryParser.java @@ -65,6 +65,7 @@ public class MoreLikeThisFieldJsonQueryParser extends AbstractIndexComponent imp MoreLikeThisQuery mltQuery = new MoreLikeThisQuery(); + mltQuery.setSimilarity(parseContext.searchSimilarity()); String currentFieldName = null; while ((token = jp.nextToken()) != JsonToken.END_OBJECT) { diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/MoreLikeThisJsonQueryParser.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/MoreLikeThisJsonQueryParser.java index 4ada2548f05..29a042f9fac 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/MoreLikeThisJsonQueryParser.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/MoreLikeThisJsonQueryParser.java @@ -58,6 +58,7 @@ public class MoreLikeThisJsonQueryParser extends AbstractIndexComponent implemen MoreLikeThisQuery mltQuery = new MoreLikeThisQuery(); mltQuery.setMoreLikeFields(new String[]{AllFieldMapper.NAME}); + mltQuery.setSimilarity(parseContext.searchSimilarity()); JsonToken token; String currentFieldName = null; diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/util/lucene/search/MoreLikeThisQuery.java b/modules/elasticsearch/src/main/java/org/elasticsearch/util/lucene/search/MoreLikeThisQuery.java index ef30e023a7b..01e08369fdc 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/util/lucene/search/MoreLikeThisQuery.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/util/lucene/search/MoreLikeThisQuery.java @@ -21,9 +21,7 @@ package org.elasticsearch.util.lucene.search; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.index.IndexReader; -import org.apache.lucene.search.BooleanClause; -import org.apache.lucene.search.BooleanQuery; -import org.apache.lucene.search.Query; +import org.apache.lucene.search.*; import org.apache.lucene.search.similar.MoreLikeThis; import org.elasticsearch.util.io.FastStringReader; @@ -37,6 +35,8 @@ public class MoreLikeThisQuery extends Query { public static final float DEFAULT_PERCENT_TERMS_TO_MATCH = 0.3f; + private Similarity similarity; + private String likeText; private String[] moreLikeFields; private Analyzer analyzer; @@ -63,7 +63,7 @@ public class MoreLikeThisQuery extends Query { } @Override public Query rewrite(IndexReader reader) throws IOException { - MoreLikeThis mlt = new MoreLikeThis(reader); + MoreLikeThis mlt = new MoreLikeThis(reader, similarity == null ? new DefaultSimilarity() : similarity); mlt.setFieldNames(moreLikeFields); mlt.setAnalyzer(analyzer); @@ -103,6 +103,14 @@ public class MoreLikeThisQuery extends Query { this.moreLikeFields = moreLikeFields; } + public Similarity getSimilarity() { + return similarity; + } + + public void setSimilarity(Similarity similarity) { + this.similarity = similarity; + } + public Analyzer getAnalyzer() { return analyzer; } diff --git a/modules/elasticsearch/src/test/java/org/elasticsearch/index/query/json/SimpleJsonIndexQueryParserTests.java b/modules/elasticsearch/src/test/java/org/elasticsearch/index/query/json/SimpleJsonIndexQueryParserTests.java index 1f871d8e28c..94f96fcc327 100644 --- a/modules/elasticsearch/src/test/java/org/elasticsearch/index/query/json/SimpleJsonIndexQueryParserTests.java +++ b/modules/elasticsearch/src/test/java/org/elasticsearch/index/query/json/SimpleJsonIndexQueryParserTests.java @@ -757,7 +757,7 @@ public class SimpleJsonIndexQueryParserTests { private JsonIndexQueryParser newQueryParser() throws IOException { return new JsonIndexQueryParser(new Index("test"), EMPTY_SETTINGS, - newMapperService(), new NoneFilterCache(index, EMPTY_SETTINGS), new AnalysisService(index), null, null, "test", null); + newMapperService(), new NoneFilterCache(index, EMPTY_SETTINGS), new AnalysisService(index), null, null, null, "test", null); } private MapperService newMapperService() throws IOException { diff --git a/modules/elasticsearch/src/test/java/org/elasticsearch/index/query/json/guice/IndexQueryParserModuleTests.java b/modules/elasticsearch/src/test/java/org/elasticsearch/index/query/json/guice/IndexQueryParserModuleTests.java index 4a5c4dec946..7f87f4d3e6f 100644 --- a/modules/elasticsearch/src/test/java/org/elasticsearch/index/query/json/guice/IndexQueryParserModuleTests.java +++ b/modules/elasticsearch/src/test/java/org/elasticsearch/index/query/json/guice/IndexQueryParserModuleTests.java @@ -30,6 +30,7 @@ import org.elasticsearch.index.query.IndexQueryParserService; import org.elasticsearch.index.query.json.JsonIndexQueryParser; import org.elasticsearch.index.query.json.JsonQueryParserRegistry; import org.elasticsearch.index.settings.IndexSettingsModule; +import org.elasticsearch.index.similarity.SimilarityModule; import org.elasticsearch.util.settings.Settings; import org.testng.annotations.Test; @@ -56,6 +57,7 @@ public class IndexQueryParserModuleTests { new IndexSettingsModule(settings), new FilterCacheModule(settings), new AnalysisModule(settings), + new SimilarityModule(settings), new IndexQueryParserModule(settings), new IndexNameModule(index) );