From 6a7eedd8aef63691f59525d2741246a04bc29d14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=BCscher?= Date: Mon, 7 Dec 2015 14:56:05 +0100 Subject: [PATCH] Switching HighlighterBuilder to use ParseFields --- .../highlight/AbstractHighlighterBuilder.java | 55 +++++++---- .../search/highlight/HighlightBuilder.java | 95 ++++++++++--------- .../highlight/HighlighterParseElement.java | 6 +- .../highlight/HighlightBuilderTests.java | 3 + 4 files changed, 94 insertions(+), 65 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/search/highlight/AbstractHighlighterBuilder.java b/core/src/main/java/org/elasticsearch/search/highlight/AbstractHighlighterBuilder.java index 79927ee7447..2081f659fd9 100644 --- a/core/src/main/java/org/elasticsearch/search/highlight/AbstractHighlighterBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/highlight/AbstractHighlighterBuilder.java @@ -21,6 +21,7 @@ package org.elasticsearch.search.highlight; import org.apache.lucene.search.highlight.SimpleFragmenter; import org.apache.lucene.search.highlight.SimpleSpanFragmenter; +import org.elasticsearch.common.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -37,6 +38,28 @@ import java.util.Objects; */ public abstract class AbstractHighlighterBuilder { + public static final ParseField PRE_TAGS_FIELD = new ParseField("pre_tags"); + public static final ParseField POST_TAGS_FIELD = new ParseField("post_tags"); + public static final ParseField FIELDS_FIELD = new ParseField("fields"); + public static final ParseField ORDER_FIELD = new ParseField("order"); + public static final ParseField TAGS_SCHEMA_FIELD = new ParseField("tags_schema"); + public static final ParseField HIGHLIGHT_FILTER_FIELD = new ParseField("highlight_filter"); + public static final ParseField FRAGMENT_SIZE_FIELD = new ParseField("fragment_size"); + public static final ParseField FRAGMENT_OFFSET_FIELD = new ParseField("fragment_offset"); + public static final ParseField NUMBER_OF_FRAGMENTS_FIELD = new ParseField("number_of_fragments"); + public static final ParseField ENCODER_FIELD = new ParseField("encoder"); + public static final ParseField REQUIRE_FIELD_MATCH_FIELD = new ParseField("require_field_match"); + public static final ParseField BOUNDARY_MAX_SCAN_FIELD = new ParseField("boundary_max_scan"); + public static final ParseField BOUNDARY_CHARS_FIELD = new ParseField("boundary_chars"); + public static final ParseField TYPE_FIELD = new ParseField("type"); + public static final ParseField FRAGMENTER_FIELD = new ParseField("fragmenter"); + public static final ParseField NO_MATCH_SIZE_FIELD = new ParseField("no_match_size"); + public static final ParseField FORCE_SOURCE_FIELD = new ParseField("force_source"); + public static final ParseField PHRASE_LIMIT_FIELD = new ParseField("phrase_limit"); + public static final ParseField OPTIONS_FIELD = new ParseField("options"); + public static final ParseField HIGHLIGHT_QUERY_FIELD = new ParseField("highlight_query"); + public static final ParseField MATCHED_FIELDS_FIELD = new ParseField("matched_fields"); + protected String[] preTags; protected String[] postTags; @@ -347,52 +370,52 @@ public abstract class AbstractHighlighterBuilder 0) { - builder.field("options", options); + builder.field(OPTIONS_FIELD.getPreferredName(), options); } if (forceSource != null) { - builder.field("force_source", forceSource); + builder.field(FORCE_SOURCE_FIELD.getPreferredName(), forceSource); } if (requireFieldMatch != null) { - builder.field("require_field_match", requireFieldMatch); + builder.field(REQUIRE_FIELD_MATCH_FIELD.getPreferredName(), requireFieldMatch); } if (noMatchSize != null) { - builder.field("no_match_size", noMatchSize); + builder.field(NO_MATCH_SIZE_FIELD.getPreferredName(), noMatchSize); } if (phraseLimit != null) { - builder.field("phrase_limit", phraseLimit); + builder.field(PHRASE_LIMIT_FIELD.getPreferredName(), phraseLimit); } } diff --git a/core/src/main/java/org/elasticsearch/search/highlight/HighlightBuilder.java b/core/src/main/java/org/elasticsearch/search/highlight/HighlightBuilder.java index 8a55a079735..d21888fccfd 100644 --- a/core/src/main/java/org/elasticsearch/search/highlight/HighlightBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/highlight/HighlightBuilder.java @@ -45,6 +45,8 @@ public class HighlightBuilder extends AbstractHighlighterBuilder fields = new ArrayList<>(); private String encoder; @@ -166,7 +168,7 @@ public class HighlightBuilder extends AbstractHighlighterBuilder preTagsList = new ArrayList<>(); while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { preTagsList.add(parser.text()); } highlightBuilder.preTags(preTagsList.toArray(new String[preTagsList.size()])); - } else if ("post_tags".equals(topLevelFieldName) || "postTags".equals(topLevelFieldName)) { + } else if (parseContext.parseFieldMatcher().match(topLevelFieldName, POST_TAGS_FIELD)) { List postTagsList = new ArrayList<>(); while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { postTagsList.add(parser.text()); } highlightBuilder.postTags(postTagsList.toArray(new String[postTagsList.size()])); - } else if ("fields".equals(topLevelFieldName)) { + } else if (parseContext.parseFieldMatcher().match(topLevelFieldName, FIELDS_FIELD)) { highlightBuilder.useExplicitFieldOrder(true); while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { if (token == XContentParser.Token.START_OBJECT) { @@ -225,39 +227,40 @@ public class HighlightBuilder extends AbstractHighlighterBuilder 0) { if (useExplicitFieldOrder) { - builder.startArray("fields"); + builder.startArray(FIELDS_FIELD.getPreferredName()); } else { - builder.startObject("fields"); + builder.startObject(FIELDS_FIELD.getPreferredName()); } for (Field field : fields) { if (useExplicitFieldOrder) { @@ -396,10 +399,10 @@ public class HighlightBuilder extends AbstractHighlighterBuilder preTagsList = new ArrayList<>(); while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { preTagsList.add(parser.text()); } field.preTags(preTagsList.toArray(new String[preTagsList.size()])); - } else if ("post_tags".equals(currentFieldName) || "postTags".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, POST_TAGS_FIELD)) { List postTagsList = new ArrayList<>(); while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { postTagsList.add(parser.text()); } field.postTags(postTagsList.toArray(new String[postTagsList.size()])); - } else if ("matched_fields".equals(currentFieldName) || "matchedFields".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, MATCHED_FIELDS_FIELD)) { List matchedFields = new ArrayList<>(); while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { matchedFields.add(parser.text()); @@ -434,37 +437,37 @@ public class HighlightBuilder extends AbstractHighlighterBuilderfvh should provide highlighting on filter clauses */ public static final boolean DEFAULT_HIGHLIGHT_FILTER = false; - /** default for highlight fragments being ordered by score */ + /** default for highlight fragments being ordered by score */ public static final boolean DEFAULT_SCORE_ORDERED = false; /** the default encoder setting */ public static final String DEFAULT_ENCODER = "default"; @@ -74,7 +74,7 @@ public class HighlighterParseElement implements SearchParseElement { public static final String[] DEFAULT_PRE_TAGS = new String[]{""}; /** the default closing tag */ public static final String[] DEFAULT_POST_TAGS = new String[]{""}; - + /** the default opening tags when tag_schema = "styled" */ public static final String[] STYLED_PRE_TAG = { "", "", "", @@ -94,7 +94,7 @@ public class HighlighterParseElement implements SearchParseElement { } } - public static SearchContextHighlight parse(XContentParser parser, QueryShardContext queryShardContext) throws IOException { + public SearchContextHighlight parse(XContentParser parser, QueryShardContext queryShardContext) throws IOException { XContentParser.Token token; String topLevelFieldName = null; final List> fieldsOptions = new ArrayList<>(); diff --git a/core/src/test/java/org/elasticsearch/search/highlight/HighlightBuilderTests.java b/core/src/test/java/org/elasticsearch/search/highlight/HighlightBuilderTests.java index 70e6574b694..6ece2add646 100644 --- a/core/src/test/java/org/elasticsearch/search/highlight/HighlightBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/highlight/HighlightBuilderTests.java @@ -19,6 +19,7 @@ package org.elasticsearch.search.highlight; +import org.elasticsearch.common.ParseFieldMatcher; import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; @@ -130,6 +131,7 @@ public class HighlightBuilderTests extends ESTestCase { */ public void testFromXContent() throws IOException { QueryParseContext context = new QueryParseContext(indicesQueriesRegistry); + context.parseFieldMatcher(new ParseFieldMatcher(Settings.EMPTY)); for (int runs = 0; runs < NUMBER_OF_TESTBUILDERS; runs++) { HighlightBuilder highlightBuilder = randomHighlighterBuilder(); XContentBuilder builder = XContentFactory.contentBuilder(randomFrom(XContentType.values())); @@ -155,6 +157,7 @@ public class HighlightBuilderTests extends ESTestCase { */ public void testParsingTagsSchema() throws IOException { QueryParseContext context = new QueryParseContext(indicesQueriesRegistry); + context.parseFieldMatcher(new ParseFieldMatcher(Settings.EMPTY)); String highlightElement = "{\n" + " \"tags_schema\" : \"styled\"\n" + "}\n";