inject similarity to parsing context of queries so it can be used

This commit is contained in:
kimchy 2010-04-01 00:35:54 +03:00
parent 2ea7423f3a
commit aac05e2629
8 changed files with 42 additions and 10 deletions

View File

@ -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<String, IndexQueryParser> 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<String, IndexQueryParserFactory> indexQueryParsersFactories) {
super(index, indexSettings);
Map<String, Settings> 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);
}

View File

@ -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<ThreadLocals.CleanableValue<JsonQueryParseContext>> cache = new ThreadLocal<ThreadLocals.CleanableValue<JsonQueryParseContext>>() {
@Override protected ThreadLocals.CleanableValue<JsonQueryParseContext> initialValue() {
return new ThreadLocals.CleanableValue<JsonQueryParseContext>(new JsonQueryParseContext(index, queryParserRegistry, mapperService, filterCache));
return new ThreadLocals.CleanableValue<JsonQueryParseContext>(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<String, JsonQueryParserFactory> jsonQueryParsers,
@Nullable Map<String, JsonFilterParserFactory> jsonFilterParsers,
@Assisted String name, @Assisted @Nullable Settings settings) {
super(index, indexSettings);
this.name = name;
this.mapperService = mapperService;
this.similarityService = similarityService;
this.filterCache = filterCache;
List<JsonQueryParser> queryParsers = newArrayList();

View File

@ -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;
}

View File

@ -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) {

View File

@ -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;

View File

@ -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;
}

View File

@ -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 {

View File

@ -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)
);