Pass down parse context instead of just parser in completion context suggester.

This commit is contained in:
Christoph Büscher 2016-04-15 18:18:15 +02:00
parent 4e77adf38e
commit f7e79f4981
11 changed files with 56 additions and 33 deletions

View File

@ -20,7 +20,6 @@ package org.elasticsearch.search.suggest.completion;
import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.ParseField; import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.ParseFieldMatcher;
import org.elasticsearch.common.ParseFieldMatcherSupplier; import org.elasticsearch.common.ParseFieldMatcherSupplier;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
@ -265,7 +264,7 @@ public class CompletionSuggestionBuilder extends SuggestionBuilder<CompletionSug
static CompletionSuggestionBuilder innerFromXContent(QueryParseContext parseContext) throws IOException { static CompletionSuggestionBuilder innerFromXContent(QueryParseContext parseContext) throws IOException {
CompletionSuggestionBuilder.InnerBuilder builder = new CompletionSuggestionBuilder.InnerBuilder(); CompletionSuggestionBuilder.InnerBuilder builder = new CompletionSuggestionBuilder.InnerBuilder();
TLP_PARSER.parse(parseContext.parser(), builder, () -> ParseFieldMatcher.STRICT); TLP_PARSER.parse(parseContext.parser(), builder, parseContext);
String field = builder.field; String field = builder.field;
// now we should have field name, check and copy fields over to the suggestion builder we return // now we should have field name, check and copy fields over to the suggestion builder we return
if (field == null) { if (field == null) {
@ -301,7 +300,7 @@ public class CompletionSuggestionBuilder extends SuggestionBuilder<CompletionSug
if (currentToken == XContentParser.Token.FIELD_NAME) { if (currentToken == XContentParser.Token.FIELD_NAME) {
currentFieldName = contextParser.currentName(); currentFieldName = contextParser.currentName();
final ContextMapping mapping = contextMappings.get(currentFieldName); final ContextMapping mapping = contextMappings.get(currentFieldName);
queryContexts.put(currentFieldName, mapping.parseQueryContext(contextParser)); queryContexts.put(currentFieldName, mapping.parseQueryContext(context.newParseContext(contextParser)));
} }
} }
suggestionContext.setQueryContexts(queryContexts); suggestionContext.setQueryContexts(queryContexts);

View File

@ -27,6 +27,7 @@ import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentParser.Token; import org.elasticsearch.common.xcontent.XContentParser.Token;
import org.elasticsearch.index.mapper.ParseContext; import org.elasticsearch.index.mapper.ParseContext;
import org.elasticsearch.index.mapper.ParseContext.Document; import org.elasticsearch.index.mapper.ParseContext.Document;
import org.elasticsearch.index.query.QueryParseContext;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
@ -139,8 +140,8 @@ public class CategoryContextMapping extends ContextMapping<CategoryQueryContext>
} }
@Override @Override
protected CategoryQueryContext fromXContent(XContentParser parser) throws IOException { protected CategoryQueryContext fromXContent(QueryParseContext context) throws IOException {
return CategoryQueryContext.fromXContent(parser); return CategoryQueryContext.fromXContent(context);
} }
/** /**

View File

@ -27,6 +27,7 @@ import org.elasticsearch.common.xcontent.ObjectParser;
import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.query.QueryParseContext;
import java.io.IOException; import java.io.IOException;
import java.util.Objects; import java.util.Objects;
@ -104,7 +105,8 @@ public final class CategoryQueryContext implements ToXContent {
CATEGORY_PARSER.declareBoolean(Builder::setPrefix, new ParseField(CONTEXT_PREFIX)); CATEGORY_PARSER.declareBoolean(Builder::setPrefix, new ParseField(CONTEXT_PREFIX));
} }
public static CategoryQueryContext fromXContent(XContentParser parser) throws IOException { public static CategoryQueryContext fromXContent(QueryParseContext context) throws IOException {
XContentParser parser = context.parser();
XContentParser.Token token = parser.currentToken(); XContentParser.Token token = parser.currentToken();
Builder builder = builder(); Builder builder = builder();
if (token == XContentParser.Token.START_OBJECT) { if (token == XContentParser.Token.START_OBJECT) {

View File

@ -27,6 +27,7 @@ import org.elasticsearch.common.xcontent.XContentParser.Token;
import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.index.mapper.ParseContext; import org.elasticsearch.index.mapper.ParseContext;
import org.elasticsearch.index.mapper.core.CompletionFieldMapper; import org.elasticsearch.index.mapper.core.CompletionFieldMapper;
import org.elasticsearch.index.query.QueryParseContext;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
@ -99,19 +100,20 @@ public abstract class ContextMapping<T extends ToXContent> implements ToXContent
/** /**
* Prototype for the query context * Prototype for the query context
*/ */
protected abstract T fromXContent(XContentParser parser) throws IOException; protected abstract T fromXContent(QueryParseContext context) throws IOException;
/** /**
* Parses query contexts for this mapper * Parses query contexts for this mapper
*/ */
public final List<InternalQueryContext> parseQueryContext(XContentParser parser) throws IOException, ElasticsearchParseException { public final List<InternalQueryContext> parseQueryContext(QueryParseContext context) throws IOException, ElasticsearchParseException {
List<T> queryContexts = new ArrayList<>(); List<T> queryContexts = new ArrayList<>();
XContentParser parser = context.parser();
Token token = parser.nextToken(); Token token = parser.nextToken();
if (token == Token.START_OBJECT || token == Token.VALUE_STRING) { if (token == Token.START_OBJECT || token == Token.VALUE_STRING) {
queryContexts.add(fromXContent(parser)); queryContexts.add(fromXContent(context));
} else if (token == Token.START_ARRAY) { } else if (token == Token.START_ARRAY) {
while (parser.nextToken() != Token.END_ARRAY) { while (parser.nextToken() != Token.END_ARRAY) {
queryContexts.add(fromXContent(parser)); queryContexts.add(fromXContent(context));
} }
} }
return toInternalQueryContexts(queryContexts); return toInternalQueryContexts(queryContexts);

View File

@ -33,6 +33,7 @@ import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.ParseContext; import org.elasticsearch.index.mapper.ParseContext;
import org.elasticsearch.index.mapper.ParseContext.Document; import org.elasticsearch.index.mapper.ParseContext.Document;
import org.elasticsearch.index.mapper.geo.GeoPointFieldMapper; import org.elasticsearch.index.mapper.geo.GeoPointFieldMapper;
import org.elasticsearch.index.query.QueryParseContext;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
@ -223,8 +224,8 @@ public class GeoContextMapping extends ContextMapping<GeoQueryContext> {
} }
@Override @Override
protected GeoQueryContext fromXContent(XContentParser parser) throws IOException { protected GeoQueryContext fromXContent(QueryParseContext context) throws IOException {
return GeoQueryContext.fromXContent(parser); return GeoQueryContext.fromXContent(context);
} }
/** /**

View File

@ -21,7 +21,6 @@ package org.elasticsearch.search.suggest.completion.context;
import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.ParseField; import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.ParseFieldMatcher;
import org.elasticsearch.common.ParseFieldMatcherSupplier; import org.elasticsearch.common.ParseFieldMatcherSupplier;
import org.elasticsearch.common.geo.GeoPoint; import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.geo.GeoUtils; import org.elasticsearch.common.geo.GeoUtils;
@ -29,6 +28,7 @@ import org.elasticsearch.common.xcontent.ObjectParser;
import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.query.QueryParseContext;
import java.io.IOException; import java.io.IOException;
import java.util.Collections; import java.util.Collections;
@ -125,11 +125,12 @@ public final class GeoQueryContext implements ToXContent {
GEO_CONTEXT_PARSER.declareDouble(GeoQueryContext.Builder::setLon, new ParseField("lon")); GEO_CONTEXT_PARSER.declareDouble(GeoQueryContext.Builder::setLon, new ParseField("lon"));
} }
public static GeoQueryContext fromXContent(XContentParser parser) throws IOException { public static GeoQueryContext fromXContent(QueryParseContext context) throws IOException {
XContentParser parser = context.parser();
XContentParser.Token token = parser.currentToken(); XContentParser.Token token = parser.currentToken();
GeoQueryContext.Builder builder = new Builder(); GeoQueryContext.Builder builder = new Builder();
if (token == XContentParser.Token.START_OBJECT) { if (token == XContentParser.Token.START_OBJECT) {
GEO_CONTEXT_PARSER.parse(parser, builder, () -> ParseFieldMatcher.STRICT); GEO_CONTEXT_PARSER.parse(parser, builder, context);
} else if (token == XContentParser.Token.VALUE_STRING) { } else if (token == XContentParser.Token.VALUE_STRING) {
builder.setGeoPoint(GeoPoint.fromGeohash(parser.text())); builder.setGeoPoint(GeoPoint.fromGeohash(parser.text()));
} else { } else {

View File

@ -23,6 +23,7 @@ import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField; import org.apache.lucene.document.StringField;
import org.apache.lucene.index.IndexableField; import org.apache.lucene.index.IndexableField;
import org.apache.lucene.search.suggest.document.ContextSuggestField; import org.apache.lucene.search.suggest.document.ContextSuggestField;
import org.elasticsearch.common.ParseFieldMatcher;
import org.elasticsearch.common.compress.CompressedXContent; import org.elasticsearch.common.compress.CompressedXContent;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
@ -33,6 +34,8 @@ import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.mapper.ParseContext; import org.elasticsearch.index.mapper.ParseContext;
import org.elasticsearch.index.mapper.ParsedDocument; import org.elasticsearch.index.mapper.ParsedDocument;
import org.elasticsearch.index.query.QueryParseContext;
import org.elasticsearch.indices.query.IndicesQueriesRegistry;
import org.elasticsearch.search.suggest.completion.context.CategoryContextMapping; import org.elasticsearch.search.suggest.completion.context.CategoryContextMapping;
import org.elasticsearch.search.suggest.completion.context.ContextBuilder; import org.elasticsearch.search.suggest.completion.context.ContextBuilder;
import org.elasticsearch.search.suggest.completion.context.ContextMapping; import org.elasticsearch.search.suggest.completion.context.ContextMapping;
@ -190,7 +193,7 @@ public class CategoryContextMappingTests extends ESSingleNodeTestCase {
XContentBuilder builder = jsonBuilder().value("context1"); XContentBuilder builder = jsonBuilder().value("context1");
XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes()); XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes());
CategoryContextMapping mapping = ContextBuilder.category("cat").build(); CategoryContextMapping mapping = ContextBuilder.category("cat").build();
List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(parser); List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(createParseContext(parser));
assertThat(internalQueryContexts.size(), equalTo(1)); assertThat(internalQueryContexts.size(), equalTo(1));
assertThat(internalQueryContexts.get(0).context, equalTo("context1")); assertThat(internalQueryContexts.get(0).context, equalTo("context1"));
assertThat(internalQueryContexts.get(0).boost, equalTo(1)); assertThat(internalQueryContexts.get(0).boost, equalTo(1));
@ -204,7 +207,7 @@ public class CategoryContextMappingTests extends ESSingleNodeTestCase {
.endArray(); .endArray();
XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes()); XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes());
CategoryContextMapping mapping = ContextBuilder.category("cat").build(); CategoryContextMapping mapping = ContextBuilder.category("cat").build();
List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(parser); List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(createParseContext(parser));
assertThat(internalQueryContexts.size(), equalTo(2)); assertThat(internalQueryContexts.size(), equalTo(2));
assertThat(internalQueryContexts.get(0).context, equalTo("context1")); assertThat(internalQueryContexts.get(0).context, equalTo("context1"));
assertThat(internalQueryContexts.get(0).boost, equalTo(1)); assertThat(internalQueryContexts.get(0).boost, equalTo(1));
@ -222,7 +225,7 @@ public class CategoryContextMappingTests extends ESSingleNodeTestCase {
.endObject(); .endObject();
XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes()); XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes());
CategoryContextMapping mapping = ContextBuilder.category("cat").build(); CategoryContextMapping mapping = ContextBuilder.category("cat").build();
List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(parser); List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(createParseContext(parser));
assertThat(internalQueryContexts.size(), equalTo(1)); assertThat(internalQueryContexts.size(), equalTo(1));
assertThat(internalQueryContexts.get(0).context, equalTo("context1")); assertThat(internalQueryContexts.get(0).context, equalTo("context1"));
assertThat(internalQueryContexts.get(0).boost, equalTo(10)); assertThat(internalQueryContexts.get(0).boost, equalTo(10));
@ -245,7 +248,7 @@ public class CategoryContextMappingTests extends ESSingleNodeTestCase {
.endArray(); .endArray();
XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes()); XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes());
CategoryContextMapping mapping = ContextBuilder.category("cat").build(); CategoryContextMapping mapping = ContextBuilder.category("cat").build();
List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(parser); List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(createParseContext(parser));
assertThat(internalQueryContexts.size(), equalTo(2)); assertThat(internalQueryContexts.size(), equalTo(2));
assertThat(internalQueryContexts.get(0).context, equalTo("context1")); assertThat(internalQueryContexts.get(0).context, equalTo("context1"));
assertThat(internalQueryContexts.get(0).boost, equalTo(2)); assertThat(internalQueryContexts.get(0).boost, equalTo(2));
@ -255,6 +258,10 @@ public class CategoryContextMappingTests extends ESSingleNodeTestCase {
assertThat(internalQueryContexts.get(1).isPrefix, equalTo(false)); assertThat(internalQueryContexts.get(1).isPrefix, equalTo(false));
} }
private static QueryParseContext createParseContext(XContentParser parser) {
return new QueryParseContext(new IndicesQueriesRegistry(), parser, ParseFieldMatcher.STRICT);
}
public void testQueryContextParsingMixed() throws Exception { public void testQueryContextParsingMixed() throws Exception {
XContentBuilder builder = jsonBuilder().startArray() XContentBuilder builder = jsonBuilder().startArray()
.startObject() .startObject()
@ -266,7 +273,7 @@ public class CategoryContextMappingTests extends ESSingleNodeTestCase {
.endArray(); .endArray();
XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes()); XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes());
CategoryContextMapping mapping = ContextBuilder.category("cat").build(); CategoryContextMapping mapping = ContextBuilder.category("cat").build();
List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(parser); List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(createParseContext(parser));
assertThat(internalQueryContexts.size(), equalTo(2)); assertThat(internalQueryContexts.size(), equalTo(2));
assertThat(internalQueryContexts.get(0).context, equalTo("context1")); assertThat(internalQueryContexts.get(0).context, equalTo("context1"));
assertThat(internalQueryContexts.get(0).boost, equalTo(2)); assertThat(internalQueryContexts.get(0).boost, equalTo(2));

View File

@ -19,7 +19,7 @@
package org.elasticsearch.search.suggest.completion; package org.elasticsearch.search.suggest.completion;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.query.QueryParseContext;
import org.elasticsearch.search.suggest.completion.context.CategoryQueryContext; import org.elasticsearch.search.suggest.completion.context.CategoryQueryContext;
import java.io.IOException; import java.io.IOException;
@ -40,8 +40,8 @@ public class CategoryQueryContextTests extends QueryContextTestCase<CategoryQuer
} }
@Override @Override
protected CategoryQueryContext fromXContent(XContentParser parser) throws IOException { protected CategoryQueryContext fromXContent(QueryParseContext context) throws IOException {
return CategoryQueryContext.fromXContent(parser); return CategoryQueryContext.fromXContent(context);
} }
public void testNullCategoryIsIllegal() { public void testNullCategoryIsIllegal() {

View File

@ -20,6 +20,7 @@
package org.elasticsearch.search.suggest.completion; package org.elasticsearch.search.suggest.completion;
import org.apache.lucene.index.IndexableField; import org.apache.lucene.index.IndexableField;
import org.elasticsearch.common.ParseFieldMatcher;
import org.elasticsearch.common.compress.CompressedXContent; import org.elasticsearch.common.compress.CompressedXContent;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
@ -29,6 +30,8 @@ import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.mapper.ParsedDocument; import org.elasticsearch.index.mapper.ParsedDocument;
import org.elasticsearch.index.query.QueryParseContext;
import org.elasticsearch.indices.query.IndicesQueriesRegistry;
import org.elasticsearch.search.suggest.completion.context.ContextBuilder; import org.elasticsearch.search.suggest.completion.context.ContextBuilder;
import org.elasticsearch.search.suggest.completion.context.ContextMapping; import org.elasticsearch.search.suggest.completion.context.ContextMapping;
import org.elasticsearch.search.suggest.completion.context.GeoContextMapping; import org.elasticsearch.search.suggest.completion.context.GeoContextMapping;
@ -202,7 +205,7 @@ public class GeoContextMappingTests extends ESSingleNodeTestCase {
XContentBuilder builder = jsonBuilder().value("ezs42e44yx96"); XContentBuilder builder = jsonBuilder().value("ezs42e44yx96");
XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes()); XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes());
GeoContextMapping mapping = ContextBuilder.geo("geo").build(); GeoContextMapping mapping = ContextBuilder.geo("geo").build();
List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(parser); List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(createParseContext(parser));
assertThat(internalQueryContexts.size(), equalTo(1 + 8)); assertThat(internalQueryContexts.size(), equalTo(1 + 8));
Collection<String> locations = new ArrayList<>(); Collection<String> locations = new ArrayList<>();
locations.add("ezs42e"); locations.add("ezs42e");
@ -221,7 +224,7 @@ public class GeoContextMappingTests extends ESSingleNodeTestCase {
.endObject(); .endObject();
XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes()); XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes());
GeoContextMapping mapping = ContextBuilder.geo("geo").build(); GeoContextMapping mapping = ContextBuilder.geo("geo").build();
List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(parser); List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(createParseContext(parser));
assertThat(internalQueryContexts.size(), equalTo(1 + 8)); assertThat(internalQueryContexts.size(), equalTo(1 + 8));
Collection<String> locations = new ArrayList<>(); Collection<String> locations = new ArrayList<>();
locations.add("wh0n94"); locations.add("wh0n94");
@ -244,7 +247,7 @@ public class GeoContextMappingTests extends ESSingleNodeTestCase {
.endObject(); .endObject();
XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes()); XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes());
GeoContextMapping mapping = ContextBuilder.geo("geo").build(); GeoContextMapping mapping = ContextBuilder.geo("geo").build();
List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(parser); List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(createParseContext(parser));
assertThat(internalQueryContexts.size(), equalTo(1 + 1 + 8 + 1 + 8 + 1 + 8)); assertThat(internalQueryContexts.size(), equalTo(1 + 1 + 8 + 1 + 8 + 1 + 8));
Collection<String> locations = new ArrayList<>(); Collection<String> locations = new ArrayList<>();
locations.add("wh0n94"); locations.add("wh0n94");
@ -282,7 +285,7 @@ public class GeoContextMappingTests extends ESSingleNodeTestCase {
.endArray(); .endArray();
XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes()); XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes());
GeoContextMapping mapping = ContextBuilder.geo("geo").build(); GeoContextMapping mapping = ContextBuilder.geo("geo").build();
List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(parser); List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(createParseContext(parser));
assertThat(internalQueryContexts.size(), equalTo(1 + 1 + 8 + 1 + 8 + 1 + 8 + 1 + 1 + 8)); assertThat(internalQueryContexts.size(), equalTo(1 + 1 + 8 + 1 + 8 + 1 + 8 + 1 + 1 + 8));
Collection<String> firstLocations = new ArrayList<>(); Collection<String> firstLocations = new ArrayList<>();
firstLocations.add("wh0n94"); firstLocations.add("wh0n94");
@ -325,7 +328,7 @@ public class GeoContextMappingTests extends ESSingleNodeTestCase {
.endArray(); .endArray();
XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes()); XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes());
GeoContextMapping mapping = ContextBuilder.geo("geo").build(); GeoContextMapping mapping = ContextBuilder.geo("geo").build();
List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(parser); List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(createParseContext(parser));
assertThat(internalQueryContexts.size(), equalTo(1 + 1 + 8 + 1 + 8 + 1 + 8)); assertThat(internalQueryContexts.size(), equalTo(1 + 1 + 8 + 1 + 8 + 1 + 8));
Collection<String> firstLocations = new ArrayList<>(); Collection<String> firstLocations = new ArrayList<>();
firstLocations.add("wh0n94"); firstLocations.add("wh0n94");
@ -347,4 +350,8 @@ public class GeoContextMappingTests extends ESSingleNodeTestCase {
assertThat(internalQueryContext.isPrefix, equalTo(internalQueryContext.context.length() < GeoContextMapping.DEFAULT_PRECISION)); assertThat(internalQueryContext.isPrefix, equalTo(internalQueryContext.context.length() < GeoContextMapping.DEFAULT_PRECISION));
} }
} }
private static QueryParseContext createParseContext(XContentParser parser) {
return new QueryParseContext(new IndicesQueriesRegistry(), parser, ParseFieldMatcher.STRICT);
};
} }

View File

@ -20,7 +20,7 @@
package org.elasticsearch.search.suggest.completion; package org.elasticsearch.search.suggest.completion;
import org.elasticsearch.common.geo.GeoPoint; import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.query.QueryParseContext;
import org.elasticsearch.search.suggest.completion.context.GeoQueryContext; import org.elasticsearch.search.suggest.completion.context.GeoQueryContext;
import java.io.IOException; import java.io.IOException;
@ -51,8 +51,8 @@ public class GeoQueryContextTests extends QueryContextTestCase<GeoQueryContext>
} }
@Override @Override
protected GeoQueryContext fromXContent(XContentParser parser) throws IOException { protected GeoQueryContext fromXContent(QueryParseContext context) throws IOException {
return GeoQueryContext.fromXContent(parser); return GeoQueryContext.fromXContent(context);
} }
public void testNullGeoPointIsIllegal() { public void testNullGeoPointIsIllegal() {

View File

@ -19,11 +19,14 @@
package org.elasticsearch.search.suggest.completion; package org.elasticsearch.search.suggest.completion;
import org.elasticsearch.common.ParseFieldMatcher;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.query.QueryParseContext;
import org.elasticsearch.indices.query.IndicesQueriesRegistry;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.ESTestCase;
import java.io.IOException; import java.io.IOException;
@ -40,7 +43,7 @@ public abstract class QueryContextTestCase<QC extends ToXContent> extends ESTest
/** /**
* read the context * read the context
*/ */
protected abstract QC fromXContent(XContentParser parser) throws IOException; protected abstract QC fromXContent(QueryParseContext context) throws IOException;
public void testToXContext() throws IOException { public void testToXContext() throws IOException {
for (int i = 0; i < NUMBER_OF_RUNS; i++) { for (int i = 0; i < NUMBER_OF_RUNS; i++) {
@ -50,7 +53,7 @@ public abstract class QueryContextTestCase<QC extends ToXContent> extends ESTest
BytesReference bytesReference = builder.bytes(); BytesReference bytesReference = builder.bytes();
XContentParser parser = XContentFactory.xContent(bytesReference).createParser(bytesReference); XContentParser parser = XContentFactory.xContent(bytesReference).createParser(bytesReference);
parser.nextToken(); parser.nextToken();
QC fromXContext = fromXContent(parser); QC fromXContext = fromXContent(new QueryParseContext(new IndicesQueriesRegistry(), parser, ParseFieldMatcher.STRICT));
assertEquals(toXContent, fromXContext); assertEquals(toXContent, fromXContext);
assertEquals(toXContent.hashCode(), fromXContext.hashCode()); assertEquals(toXContent.hashCode(), fromXContext.hashCode());
} }