From f7e79f4981ddeb1d14e0aaa0417b3ed378d8e298 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=BCscher?= Date: Fri, 15 Apr 2016 18:18:15 +0200 Subject: [PATCH] Pass down parse context instead of just parser in completion context suggester. --- .../completion/CompletionSuggestionBuilder.java | 5 ++--- .../context/CategoryContextMapping.java | 5 +++-- .../context/CategoryQueryContext.java | 4 +++- .../completion/context/ContextMapping.java | 10 ++++++---- .../completion/context/GeoContextMapping.java | 5 +++-- .../completion/context/GeoQueryContext.java | 7 ++++--- .../completion/CategoryContextMappingTests.java | 17 ++++++++++++----- .../completion/CategoryQueryContextTests.java | 6 +++--- .../completion/GeoContextMappingTests.java | 17 ++++++++++++----- .../completion/GeoQueryContextTests.java | 6 +++--- .../completion/QueryContextTestCase.java | 7 +++++-- 11 files changed, 56 insertions(+), 33 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggestionBuilder.java b/core/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggestionBuilder.java index 4afad501cba..ce75351dfb5 100644 --- a/core/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggestionBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggestionBuilder.java @@ -20,7 +20,6 @@ package org.elasticsearch.search.suggest.completion; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.ParseField; -import org.elasticsearch.common.ParseFieldMatcher; import org.elasticsearch.common.ParseFieldMatcherSupplier; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.StreamInput; @@ -265,7 +264,7 @@ public class CompletionSuggestionBuilder extends SuggestionBuilder ParseFieldMatcher.STRICT); + TLP_PARSER.parse(parseContext.parser(), builder, parseContext); String field = builder.field; // now we should have field name, check and copy fields over to the suggestion builder we return if (field == null) { @@ -301,7 +300,7 @@ public class CompletionSuggestionBuilder extends SuggestionBuilder } @Override - protected CategoryQueryContext fromXContent(XContentParser parser) throws IOException { - return CategoryQueryContext.fromXContent(parser); + protected CategoryQueryContext fromXContent(QueryParseContext context) throws IOException { + return CategoryQueryContext.fromXContent(context); } /** diff --git a/core/src/main/java/org/elasticsearch/search/suggest/completion/context/CategoryQueryContext.java b/core/src/main/java/org/elasticsearch/search/suggest/completion/context/CategoryQueryContext.java index 1384868f0dc..a17b7a87b43 100644 --- a/core/src/main/java/org/elasticsearch/search/suggest/completion/context/CategoryQueryContext.java +++ b/core/src/main/java/org/elasticsearch/search/suggest/completion/context/CategoryQueryContext.java @@ -27,6 +27,7 @@ import org.elasticsearch.common.xcontent.ObjectParser; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.index.query.QueryParseContext; import java.io.IOException; import java.util.Objects; @@ -104,7 +105,8 @@ public final class CategoryQueryContext implements ToXContent { 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(); Builder builder = builder(); if (token == XContentParser.Token.START_OBJECT) { diff --git a/core/src/main/java/org/elasticsearch/search/suggest/completion/context/ContextMapping.java b/core/src/main/java/org/elasticsearch/search/suggest/completion/context/ContextMapping.java index cb445b17706..305a3b1afd3 100644 --- a/core/src/main/java/org/elasticsearch/search/suggest/completion/context/ContextMapping.java +++ b/core/src/main/java/org/elasticsearch/search/suggest/completion/context/ContextMapping.java @@ -27,6 +27,7 @@ import org.elasticsearch.common.xcontent.XContentParser.Token; import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.mapper.ParseContext; import org.elasticsearch.index.mapper.core.CompletionFieldMapper; +import org.elasticsearch.index.query.QueryParseContext; import java.io.IOException; import java.util.ArrayList; @@ -99,19 +100,20 @@ public abstract class ContextMapping implements ToXContent /** * 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 */ - public final List parseQueryContext(XContentParser parser) throws IOException, ElasticsearchParseException { + public final List parseQueryContext(QueryParseContext context) throws IOException, ElasticsearchParseException { List queryContexts = new ArrayList<>(); + XContentParser parser = context.parser(); Token token = parser.nextToken(); if (token == Token.START_OBJECT || token == Token.VALUE_STRING) { - queryContexts.add(fromXContent(parser)); + queryContexts.add(fromXContent(context)); } else if (token == Token.START_ARRAY) { while (parser.nextToken() != Token.END_ARRAY) { - queryContexts.add(fromXContent(parser)); + queryContexts.add(fromXContent(context)); } } return toInternalQueryContexts(queryContexts); diff --git a/core/src/main/java/org/elasticsearch/search/suggest/completion/context/GeoContextMapping.java b/core/src/main/java/org/elasticsearch/search/suggest/completion/context/GeoContextMapping.java index 83345be24b1..a9aa9d340ca 100644 --- a/core/src/main/java/org/elasticsearch/search/suggest/completion/context/GeoContextMapping.java +++ b/core/src/main/java/org/elasticsearch/search/suggest/completion/context/GeoContextMapping.java @@ -33,6 +33,7 @@ import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.mapper.ParseContext; import org.elasticsearch.index.mapper.ParseContext.Document; import org.elasticsearch.index.mapper.geo.GeoPointFieldMapper; +import org.elasticsearch.index.query.QueryParseContext; import java.io.IOException; import java.util.ArrayList; @@ -223,8 +224,8 @@ public class GeoContextMapping extends ContextMapping { } @Override - protected GeoQueryContext fromXContent(XContentParser parser) throws IOException { - return GeoQueryContext.fromXContent(parser); + protected GeoQueryContext fromXContent(QueryParseContext context) throws IOException { + return GeoQueryContext.fromXContent(context); } /** diff --git a/core/src/main/java/org/elasticsearch/search/suggest/completion/context/GeoQueryContext.java b/core/src/main/java/org/elasticsearch/search/suggest/completion/context/GeoQueryContext.java index dd625f252de..6ed8754c978 100644 --- a/core/src/main/java/org/elasticsearch/search/suggest/completion/context/GeoQueryContext.java +++ b/core/src/main/java/org/elasticsearch/search/suggest/completion/context/GeoQueryContext.java @@ -21,7 +21,6 @@ package org.elasticsearch.search.suggest.completion.context; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.ParseField; -import org.elasticsearch.common.ParseFieldMatcher; import org.elasticsearch.common.ParseFieldMatcherSupplier; import org.elasticsearch.common.geo.GeoPoint; 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.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.index.query.QueryParseContext; import java.io.IOException; import java.util.Collections; @@ -125,11 +125,12 @@ public final class GeoQueryContext implements ToXContent { 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(); GeoQueryContext.Builder builder = new Builder(); 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) { builder.setGeoPoint(GeoPoint.fromGeohash(parser.text())); } else { diff --git a/core/src/test/java/org/elasticsearch/search/suggest/completion/CategoryContextMappingTests.java b/core/src/test/java/org/elasticsearch/search/suggest/completion/CategoryContextMappingTests.java index e0aabe77302..de912bb9636 100644 --- a/core/src/test/java/org/elasticsearch/search/suggest/completion/CategoryContextMappingTests.java +++ b/core/src/test/java/org/elasticsearch/search/suggest/completion/CategoryContextMappingTests.java @@ -23,6 +23,7 @@ import org.apache.lucene.document.Field; import org.apache.lucene.document.StringField; import org.apache.lucene.index.IndexableField; import org.apache.lucene.search.suggest.document.ContextSuggestField; +import org.elasticsearch.common.ParseFieldMatcher; import org.elasticsearch.common.compress.CompressedXContent; import org.elasticsearch.common.xcontent.XContentBuilder; 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.ParseContext; 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.ContextBuilder; import org.elasticsearch.search.suggest.completion.context.ContextMapping; @@ -190,7 +193,7 @@ public class CategoryContextMappingTests extends ESSingleNodeTestCase { XContentBuilder builder = jsonBuilder().value("context1"); XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes()); CategoryContextMapping mapping = ContextBuilder.category("cat").build(); - List internalQueryContexts = mapping.parseQueryContext(parser); + List internalQueryContexts = mapping.parseQueryContext(createParseContext(parser)); assertThat(internalQueryContexts.size(), equalTo(1)); assertThat(internalQueryContexts.get(0).context, equalTo("context1")); assertThat(internalQueryContexts.get(0).boost, equalTo(1)); @@ -204,7 +207,7 @@ public class CategoryContextMappingTests extends ESSingleNodeTestCase { .endArray(); XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes()); CategoryContextMapping mapping = ContextBuilder.category("cat").build(); - List internalQueryContexts = mapping.parseQueryContext(parser); + List internalQueryContexts = mapping.parseQueryContext(createParseContext(parser)); assertThat(internalQueryContexts.size(), equalTo(2)); assertThat(internalQueryContexts.get(0).context, equalTo("context1")); assertThat(internalQueryContexts.get(0).boost, equalTo(1)); @@ -222,7 +225,7 @@ public class CategoryContextMappingTests extends ESSingleNodeTestCase { .endObject(); XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes()); CategoryContextMapping mapping = ContextBuilder.category("cat").build(); - List internalQueryContexts = mapping.parseQueryContext(parser); + List internalQueryContexts = mapping.parseQueryContext(createParseContext(parser)); assertThat(internalQueryContexts.size(), equalTo(1)); assertThat(internalQueryContexts.get(0).context, equalTo("context1")); assertThat(internalQueryContexts.get(0).boost, equalTo(10)); @@ -245,7 +248,7 @@ public class CategoryContextMappingTests extends ESSingleNodeTestCase { .endArray(); XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes()); CategoryContextMapping mapping = ContextBuilder.category("cat").build(); - List internalQueryContexts = mapping.parseQueryContext(parser); + List internalQueryContexts = mapping.parseQueryContext(createParseContext(parser)); assertThat(internalQueryContexts.size(), equalTo(2)); assertThat(internalQueryContexts.get(0).context, equalTo("context1")); assertThat(internalQueryContexts.get(0).boost, equalTo(2)); @@ -255,6 +258,10 @@ public class CategoryContextMappingTests extends ESSingleNodeTestCase { 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 { XContentBuilder builder = jsonBuilder().startArray() .startObject() @@ -266,7 +273,7 @@ public class CategoryContextMappingTests extends ESSingleNodeTestCase { .endArray(); XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes()); CategoryContextMapping mapping = ContextBuilder.category("cat").build(); - List internalQueryContexts = mapping.parseQueryContext(parser); + List internalQueryContexts = mapping.parseQueryContext(createParseContext(parser)); assertThat(internalQueryContexts.size(), equalTo(2)); assertThat(internalQueryContexts.get(0).context, equalTo("context1")); assertThat(internalQueryContexts.get(0).boost, equalTo(2)); diff --git a/core/src/test/java/org/elasticsearch/search/suggest/completion/CategoryQueryContextTests.java b/core/src/test/java/org/elasticsearch/search/suggest/completion/CategoryQueryContextTests.java index 0197340d1a8..59d7bbd61b9 100644 --- a/core/src/test/java/org/elasticsearch/search/suggest/completion/CategoryQueryContextTests.java +++ b/core/src/test/java/org/elasticsearch/search/suggest/completion/CategoryQueryContextTests.java @@ -19,7 +19,7 @@ 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 java.io.IOException; @@ -40,8 +40,8 @@ public class CategoryQueryContextTests extends QueryContextTestCase internalQueryContexts = mapping.parseQueryContext(parser); + List internalQueryContexts = mapping.parseQueryContext(createParseContext(parser)); assertThat(internalQueryContexts.size(), equalTo(1 + 8)); Collection locations = new ArrayList<>(); locations.add("ezs42e"); @@ -221,7 +224,7 @@ public class GeoContextMappingTests extends ESSingleNodeTestCase { .endObject(); XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes()); GeoContextMapping mapping = ContextBuilder.geo("geo").build(); - List internalQueryContexts = mapping.parseQueryContext(parser); + List internalQueryContexts = mapping.parseQueryContext(createParseContext(parser)); assertThat(internalQueryContexts.size(), equalTo(1 + 8)); Collection locations = new ArrayList<>(); locations.add("wh0n94"); @@ -244,7 +247,7 @@ public class GeoContextMappingTests extends ESSingleNodeTestCase { .endObject(); XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes()); GeoContextMapping mapping = ContextBuilder.geo("geo").build(); - List internalQueryContexts = mapping.parseQueryContext(parser); + List internalQueryContexts = mapping.parseQueryContext(createParseContext(parser)); assertThat(internalQueryContexts.size(), equalTo(1 + 1 + 8 + 1 + 8 + 1 + 8)); Collection locations = new ArrayList<>(); locations.add("wh0n94"); @@ -282,7 +285,7 @@ public class GeoContextMappingTests extends ESSingleNodeTestCase { .endArray(); XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes()); GeoContextMapping mapping = ContextBuilder.geo("geo").build(); - List internalQueryContexts = mapping.parseQueryContext(parser); + List internalQueryContexts = mapping.parseQueryContext(createParseContext(parser)); assertThat(internalQueryContexts.size(), equalTo(1 + 1 + 8 + 1 + 8 + 1 + 8 + 1 + 1 + 8)); Collection firstLocations = new ArrayList<>(); firstLocations.add("wh0n94"); @@ -325,7 +328,7 @@ public class GeoContextMappingTests extends ESSingleNodeTestCase { .endArray(); XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes()); GeoContextMapping mapping = ContextBuilder.geo("geo").build(); - List internalQueryContexts = mapping.parseQueryContext(parser); + List internalQueryContexts = mapping.parseQueryContext(createParseContext(parser)); assertThat(internalQueryContexts.size(), equalTo(1 + 1 + 8 + 1 + 8 + 1 + 8)); Collection firstLocations = new ArrayList<>(); firstLocations.add("wh0n94"); @@ -347,4 +350,8 @@ public class GeoContextMappingTests extends ESSingleNodeTestCase { assertThat(internalQueryContext.isPrefix, equalTo(internalQueryContext.context.length() < GeoContextMapping.DEFAULT_PRECISION)); } } + + private static QueryParseContext createParseContext(XContentParser parser) { + return new QueryParseContext(new IndicesQueriesRegistry(), parser, ParseFieldMatcher.STRICT); + }; } diff --git a/core/src/test/java/org/elasticsearch/search/suggest/completion/GeoQueryContextTests.java b/core/src/test/java/org/elasticsearch/search/suggest/completion/GeoQueryContextTests.java index 1d058350a98..3e22513ec11 100644 --- a/core/src/test/java/org/elasticsearch/search/suggest/completion/GeoQueryContextTests.java +++ b/core/src/test/java/org/elasticsearch/search/suggest/completion/GeoQueryContextTests.java @@ -20,7 +20,7 @@ package org.elasticsearch.search.suggest.completion; 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 java.io.IOException; @@ -51,8 +51,8 @@ public class GeoQueryContextTests extends QueryContextTestCase } @Override - protected GeoQueryContext fromXContent(XContentParser parser) throws IOException { - return GeoQueryContext.fromXContent(parser); + protected GeoQueryContext fromXContent(QueryParseContext context) throws IOException { + return GeoQueryContext.fromXContent(context); } public void testNullGeoPointIsIllegal() { diff --git a/core/src/test/java/org/elasticsearch/search/suggest/completion/QueryContextTestCase.java b/core/src/test/java/org/elasticsearch/search/suggest/completion/QueryContextTestCase.java index 58e2409cdee..2960a6e23d8 100644 --- a/core/src/test/java/org/elasticsearch/search/suggest/completion/QueryContextTestCase.java +++ b/core/src/test/java/org/elasticsearch/search/suggest/completion/QueryContextTestCase.java @@ -19,11 +19,14 @@ package org.elasticsearch.search.suggest.completion; +import org.elasticsearch.common.ParseFieldMatcher; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.index.query.QueryParseContext; +import org.elasticsearch.indices.query.IndicesQueriesRegistry; import org.elasticsearch.test.ESTestCase; import java.io.IOException; @@ -40,7 +43,7 @@ public abstract class QueryContextTestCase extends ESTest /** * read the context */ - protected abstract QC fromXContent(XContentParser parser) throws IOException; + protected abstract QC fromXContent(QueryParseContext context) throws IOException; public void testToXContext() throws IOException { for (int i = 0; i < NUMBER_OF_RUNS; i++) { @@ -50,7 +53,7 @@ public abstract class QueryContextTestCase extends ESTest BytesReference bytesReference = builder.bytes(); XContentParser parser = XContentFactory.xContent(bytesReference).createParser(bytesReference); parser.nextToken(); - QC fromXContext = fromXContent(parser); + QC fromXContext = fromXContent(new QueryParseContext(new IndicesQueriesRegistry(), parser, ParseFieldMatcher.STRICT)); assertEquals(toXContent, fromXContext); assertEquals(toXContent.hashCode(), fromXContext.hashCode()); }