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.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<CompletionSug
static CompletionSuggestionBuilder innerFromXContent(QueryParseContext parseContext) throws IOException {
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;
// 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<CompletionSug
if (currentToken == XContentParser.Token.FIELD_NAME) {
currentFieldName = contextParser.currentName();
final ContextMapping mapping = contextMappings.get(currentFieldName);
queryContexts.put(currentFieldName, mapping.parseQueryContext(contextParser));
queryContexts.put(currentFieldName, mapping.parseQueryContext(context.newParseContext(contextParser)));
}
}
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.index.mapper.ParseContext;
import org.elasticsearch.index.mapper.ParseContext.Document;
import org.elasticsearch.index.query.QueryParseContext;
import java.io.IOException;
import java.util.ArrayList;
@ -139,8 +140,8 @@ public class CategoryContextMapping extends ContextMapping<CategoryQueryContext>
}
@Override
protected CategoryQueryContext fromXContent(XContentParser parser) throws IOException {
return CategoryQueryContext.fromXContent(parser);
protected CategoryQueryContext fromXContent(QueryParseContext context) throws IOException {
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.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) {

View File

@ -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<T extends ToXContent> 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<InternalQueryContext> parseQueryContext(XContentParser parser) throws IOException, ElasticsearchParseException {
public final List<InternalQueryContext> parseQueryContext(QueryParseContext context) throws IOException, ElasticsearchParseException {
List<T> 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);

View File

@ -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<GeoQueryContext> {
}
@Override
protected GeoQueryContext fromXContent(XContentParser parser) throws IOException {
return GeoQueryContext.fromXContent(parser);
protected GeoQueryContext fromXContent(QueryParseContext context) throws IOException {
return GeoQueryContext.fromXContent(context);
}
/**

View File

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

View File

@ -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<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(parser);
List<ContextMapping.InternalQueryContext> 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<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(parser);
List<ContextMapping.InternalQueryContext> 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<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(parser);
List<ContextMapping.InternalQueryContext> 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<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(parser);
List<ContextMapping.InternalQueryContext> 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<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(parser);
List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(createParseContext(parser));
assertThat(internalQueryContexts.size(), equalTo(2));
assertThat(internalQueryContexts.get(0).context, equalTo("context1"));
assertThat(internalQueryContexts.get(0).boost, equalTo(2));

View File

@ -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<CategoryQuer
}
@Override
protected CategoryQueryContext fromXContent(XContentParser parser) throws IOException {
return CategoryQueryContext.fromXContent(parser);
protected CategoryQueryContext fromXContent(QueryParseContext context) throws IOException {
return CategoryQueryContext.fromXContent(context);
}
public void testNullCategoryIsIllegal() {

View File

@ -20,6 +20,7 @@
package org.elasticsearch.search.suggest.completion;
import org.apache.lucene.index.IndexableField;
import org.elasticsearch.common.ParseFieldMatcher;
import org.elasticsearch.common.compress.CompressedXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
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.MappedFieldType;
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.ContextMapping;
import org.elasticsearch.search.suggest.completion.context.GeoContextMapping;
@ -202,7 +205,7 @@ public class GeoContextMappingTests extends ESSingleNodeTestCase {
XContentBuilder builder = jsonBuilder().value("ezs42e44yx96");
XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes());
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));
Collection<String> 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<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(parser);
List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(createParseContext(parser));
assertThat(internalQueryContexts.size(), equalTo(1 + 8));
Collection<String> 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<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));
Collection<String> 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<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));
Collection<String> 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<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));
Collection<String> 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);
};
}

View File

@ -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<GeoQueryContext>
}
@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() {

View File

@ -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<QC extends ToXContent> 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<QC extends ToXContent> 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());
}