more decoupling of current search context when parsing a query

This commit is contained in:
Shay Banon 2012-02-14 13:28:17 +02:00
parent 5836c24b80
commit d31fdccc97
3 changed files with 18 additions and 14 deletions

View File

@ -22,7 +22,6 @@ package org.elasticsearch.index.query;
import gnu.trove.list.array.TFloatArrayList; import gnu.trove.list.array.TFloatArrayList;
import org.apache.lucene.search.Filter; import org.apache.lucene.search.Filter;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.elasticsearch.ElasticSearchIllegalStateException;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.lucene.search.function.BoostScoreFunction; import org.elasticsearch.common.lucene.search.function.BoostScoreFunction;
@ -30,7 +29,6 @@ import org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery
import org.elasticsearch.common.lucene.search.function.ScoreFunction; import org.elasticsearch.common.lucene.search.function.ScoreFunction;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.script.SearchScript; import org.elasticsearch.script.SearchScript;
import org.elasticsearch.search.internal.SearchContext;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
@ -152,11 +150,7 @@ public class CustomFiltersScoreQueryParser implements QueryParser {
ScoreFunction scoreFunction; ScoreFunction scoreFunction;
String script = scripts.get(i); String script = scripts.get(i);
if (script != null) { if (script != null) {
SearchContext context = SearchContext.current(); SearchScript searchScript = parseContext.scriptService().search(parseContext.lookup(), scriptLang, script, vars);
if (context == null) {
throw new ElasticSearchIllegalStateException("No search context on going...");
}
SearchScript searchScript = context.scriptService().search(context.lookup(), scriptLang, script, vars);
scoreFunction = new CustomScoreQueryParser.ScriptScoreFunction(script, vars, searchScript); scoreFunction = new CustomScoreQueryParser.ScriptScoreFunction(script, vars, searchScript);
} else { } else {
scoreFunction = new BoostScoreFunction(boosts.get(i)); scoreFunction = new BoostScoreFunction(boosts.get(i));

View File

@ -22,14 +22,12 @@ package org.elasticsearch.index.query;
import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.Explanation; import org.apache.lucene.search.Explanation;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.elasticsearch.ElasticSearchIllegalStateException;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.lucene.search.function.FunctionScoreQuery; import org.elasticsearch.common.lucene.search.function.FunctionScoreQuery;
import org.elasticsearch.common.lucene.search.function.ScoreFunction; import org.elasticsearch.common.lucene.search.function.ScoreFunction;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.script.SearchScript; import org.elasticsearch.script.SearchScript;
import org.elasticsearch.search.internal.SearchContext;
import java.io.IOException; import java.io.IOException;
import java.util.Map; import java.util.Map;
@ -92,11 +90,7 @@ public class CustomScoreQueryParser implements QueryParser {
throw new QueryParsingException(parseContext.index(), "[custom_score] requires 'script' field"); throw new QueryParsingException(parseContext.index(), "[custom_score] requires 'script' field");
} }
SearchContext context = SearchContext.current(); SearchScript searchScript = parseContext.scriptService().search(parseContext.lookup(), scriptLang, script, vars);
if (context == null) {
throw new ElasticSearchIllegalStateException("No search context on going...");
}
SearchScript searchScript = context.scriptService().search(context.lookup(), scriptLang, script, vars);
FunctionScoreQuery functionScoreQuery = new FunctionScoreQuery(query, new ScriptScoreFunction(script, vars, searchScript)); FunctionScoreQuery functionScoreQuery = new FunctionScoreQuery(query, new ScriptScoreFunction(script, vars, searchScript));
functionScoreQuery.setBoost(boost); functionScoreQuery.setBoost(boost);
return functionScoreQuery; return functionScoreQuery;

View File

@ -40,6 +40,8 @@ import org.elasticsearch.index.mapper.FieldMappers;
import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.similarity.SimilarityService; import org.elasticsearch.index.similarity.SimilarityService;
import org.elasticsearch.script.ScriptService; import org.elasticsearch.script.ScriptService;
import org.elasticsearch.search.internal.SearchContext;
import org.elasticsearch.search.lookup.SearchLookup;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
@ -89,6 +91,7 @@ public class QueryParseContext {
} }
public void reset(XContentParser jp) { public void reset(XContentParser jp) {
this.lookup = null;
this.parser = jp; this.parser = jp;
this.namedFilters.clear(); this.namedFilters.clear();
} }
@ -274,4 +277,17 @@ public class QueryParseContext {
} }
return Arrays.asList(types); return Arrays.asList(types);
} }
private SearchLookup lookup = null;
public SearchLookup lookup() {
SearchContext current = SearchContext.current();
if (current != null) {
return current.lookup();
}
if (lookup == null) {
lookup = new SearchLookup(mapperService(), indexCache().fieldData());
}
return lookup;
}
} }