From d31fdccc977e4f14e478714c3bd14b82c8ec623f Mon Sep 17 00:00:00 2001 From: Shay Banon Date: Tue, 14 Feb 2012 13:28:17 +0200 Subject: [PATCH] more decoupling of current search context when parsing a query --- .../query/CustomFiltersScoreQueryParser.java | 8 +------- .../index/query/CustomScoreQueryParser.java | 8 +------- .../index/query/QueryParseContext.java | 16 ++++++++++++++++ 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/elasticsearch/index/query/CustomFiltersScoreQueryParser.java b/src/main/java/org/elasticsearch/index/query/CustomFiltersScoreQueryParser.java index 8d4c36ce668..efedceb30e2 100644 --- a/src/main/java/org/elasticsearch/index/query/CustomFiltersScoreQueryParser.java +++ b/src/main/java/org/elasticsearch/index/query/CustomFiltersScoreQueryParser.java @@ -22,7 +22,6 @@ package org.elasticsearch.index.query; import gnu.trove.list.array.TFloatArrayList; import org.apache.lucene.search.Filter; import org.apache.lucene.search.Query; -import org.elasticsearch.ElasticSearchIllegalStateException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.inject.Inject; 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.xcontent.XContentParser; import org.elasticsearch.script.SearchScript; -import org.elasticsearch.search.internal.SearchContext; import java.io.IOException; import java.util.ArrayList; @@ -152,11 +150,7 @@ public class CustomFiltersScoreQueryParser implements QueryParser { ScoreFunction scoreFunction; String script = scripts.get(i); if (script != null) { - SearchContext context = SearchContext.current(); - if (context == null) { - throw new ElasticSearchIllegalStateException("No search context on going..."); - } - SearchScript searchScript = context.scriptService().search(context.lookup(), scriptLang, script, vars); + SearchScript searchScript = parseContext.scriptService().search(parseContext.lookup(), scriptLang, script, vars); scoreFunction = new CustomScoreQueryParser.ScriptScoreFunction(script, vars, searchScript); } else { scoreFunction = new BoostScoreFunction(boosts.get(i)); diff --git a/src/main/java/org/elasticsearch/index/query/CustomScoreQueryParser.java b/src/main/java/org/elasticsearch/index/query/CustomScoreQueryParser.java index 253d8770e5f..4822797c024 100644 --- a/src/main/java/org/elasticsearch/index/query/CustomScoreQueryParser.java +++ b/src/main/java/org/elasticsearch/index/query/CustomScoreQueryParser.java @@ -22,14 +22,12 @@ package org.elasticsearch.index.query; import org.apache.lucene.index.IndexReader; import org.apache.lucene.search.Explanation; import org.apache.lucene.search.Query; -import org.elasticsearch.ElasticSearchIllegalStateException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.lucene.search.function.FunctionScoreQuery; import org.elasticsearch.common.lucene.search.function.ScoreFunction; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.script.SearchScript; -import org.elasticsearch.search.internal.SearchContext; import java.io.IOException; import java.util.Map; @@ -92,11 +90,7 @@ public class CustomScoreQueryParser implements QueryParser { throw new QueryParsingException(parseContext.index(), "[custom_score] requires 'script' field"); } - SearchContext context = SearchContext.current(); - if (context == null) { - throw new ElasticSearchIllegalStateException("No search context on going..."); - } - SearchScript searchScript = context.scriptService().search(context.lookup(), scriptLang, script, vars); + SearchScript searchScript = parseContext.scriptService().search(parseContext.lookup(), scriptLang, script, vars); FunctionScoreQuery functionScoreQuery = new FunctionScoreQuery(query, new ScriptScoreFunction(script, vars, searchScript)); functionScoreQuery.setBoost(boost); return functionScoreQuery; diff --git a/src/main/java/org/elasticsearch/index/query/QueryParseContext.java b/src/main/java/org/elasticsearch/index/query/QueryParseContext.java index dcbc286625b..6e30c4314ef 100644 --- a/src/main/java/org/elasticsearch/index/query/QueryParseContext.java +++ b/src/main/java/org/elasticsearch/index/query/QueryParseContext.java @@ -40,6 +40,8 @@ import org.elasticsearch.index.mapper.FieldMappers; import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.similarity.SimilarityService; import org.elasticsearch.script.ScriptService; +import org.elasticsearch.search.internal.SearchContext; +import org.elasticsearch.search.lookup.SearchLookup; import java.io.IOException; import java.util.Arrays; @@ -89,6 +91,7 @@ public class QueryParseContext { } public void reset(XContentParser jp) { + this.lookup = null; this.parser = jp; this.namedFilters.clear(); } @@ -274,4 +277,17 @@ public class QueryParseContext { } 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; + } }