Remove PROTOTYPEs from highlighting

This commit is contained in:
Nik Everett 2016-03-31 19:10:24 -04:00
parent 9d68a515b8
commit d919031469
6 changed files with 124 additions and 141 deletions

View File

@ -166,7 +166,7 @@ public final class InnerHitBuilder extends ToXContentToBytes implements Writeabl
sorts.add(in.readSortBuilder());
}
}
highlightBuilder = in.readOptionalWriteable(HighlightBuilder.PROTOTYPE::readFrom);
highlightBuilder = in.readOptionalWriteable(HighlightBuilder::new);
query = in.readQuery();
innerHitsBuilder = in.readOptionalWriteable(InnerHitsBuilder.PROTO::readFrom);
}

View File

@ -538,9 +538,7 @@ public class TopHitsAggregatorBuilder extends AggregatorBuilder<TopHitsAggregato
factory.fieldNames = fieldNames;
}
factory.from = in.readVInt();
if (in.readBoolean()) {
factory.highlightBuilder = HighlightBuilder.PROTOTYPE.readFrom(in);
}
in.readOptionalWriteable(HighlightBuilder::new);
if (in.readBoolean()) {
int size = in.readVInt();
List<ScriptField> scriptFields = new ArrayList<>(size);
@ -584,11 +582,7 @@ public class TopHitsAggregatorBuilder extends AggregatorBuilder<TopHitsAggregato
}
}
out.writeVInt(from);
boolean hasHighlighter = highlightBuilder != null;
out.writeBoolean(hasHighlighter);
if (hasHighlighter) {
highlightBuilder.writeTo(out);
}
out.writeOptionalWriteable(highlightBuilder);
boolean hasScriptFields = scriptFields != null;
out.writeBoolean(hasScriptFields);
if (hasScriptFields) {

View File

@ -1216,9 +1216,7 @@ public final class SearchSourceBuilder extends ToXContentToBytes implements Writ
builder.fieldNames = fieldNames;
}
builder.from = in.readVInt();
if (in.readBoolean()) {
builder.highlightBuilder = HighlightBuilder.PROTOTYPE.readFrom(in);
}
builder.highlightBuilder = in.readOptionalWriteable(HighlightBuilder::new);
boolean hasIndexBoost = in.readBoolean();
if (hasIndexBoost) {
int size = in.readVInt();
@ -1320,11 +1318,7 @@ public final class SearchSourceBuilder extends ToXContentToBytes implements Writ
}
}
out.writeVInt(from);
boolean hasHighlightBuilder = highlightBuilder != null;
out.writeBoolean(hasHighlightBuilder);
if (hasHighlightBuilder) {
highlightBuilder.writeTo(out);
}
out.writeOptionalWriteable(highlightBuilder);
boolean hasIndexBoost = indexBoost != null;
out.writeBoolean(hasIndexBoost);
if (hasIndexBoost) {

View File

@ -26,6 +26,7 @@ import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.ParsingException;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.xcontent.ObjectParser;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
@ -45,8 +46,8 @@ import static org.elasticsearch.common.xcontent.ObjectParser.fromList;
* This abstract class holds parameters shared by {@link HighlightBuilder} and {@link HighlightBuilder.Field}
* and provides the common setters, equality, hashCode calculation and common serialization
*/
public abstract class AbstractHighlighterBuilder<HB extends AbstractHighlighterBuilder<?>> extends ToXContentToBytes {
public abstract class AbstractHighlighterBuilder<HB extends AbstractHighlighterBuilder<?>> extends ToXContentToBytes
implements Writeable<HB> {
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");
@ -100,6 +101,75 @@ public abstract class AbstractHighlighterBuilder<HB extends AbstractHighlighterB
protected Boolean requireFieldMatch;
public AbstractHighlighterBuilder() {
}
/**
* Read from a stream.
*/
protected AbstractHighlighterBuilder(StreamInput in) throws IOException {
preTags(in.readOptionalStringArray());
postTags(in.readOptionalStringArray());
fragmentSize(in.readOptionalVInt());
numOfFragments(in.readOptionalVInt());
highlighterType(in.readOptionalString());
fragmenter(in.readOptionalString());
if (in.readBoolean()) {
highlightQuery(in.readQuery());
}
order(in.readOptionalWriteable(Order::readFromStream));
highlightFilter(in.readOptionalBoolean());
forceSource(in.readOptionalBoolean());
boundaryMaxScan(in.readOptionalVInt());
if (in.readBoolean()) {
boundaryChars(in.readString().toCharArray());
}
noMatchSize(in.readOptionalVInt());
phraseLimit(in.readOptionalVInt());
if (in.readBoolean()) {
options(in.readMap());
}
requireFieldMatch(in.readOptionalBoolean());
}
/**
* write common parameters to {@link StreamOutput}
*/
@Override
public final void writeTo(StreamOutput out) throws IOException {
out.writeOptionalStringArray(preTags);
out.writeOptionalStringArray(postTags);
out.writeOptionalVInt(fragmentSize);
out.writeOptionalVInt(numOfFragments);
out.writeOptionalString(highlighterType);
out.writeOptionalString(fragmenter);
boolean hasQuery = highlightQuery != null;
out.writeBoolean(hasQuery);
if (hasQuery) {
out.writeQuery(highlightQuery);
}
out.writeOptionalWriteable(order);
out.writeOptionalBoolean(highlightFilter);
out.writeOptionalBoolean(forceSource);
out.writeOptionalVInt(boundaryMaxScan);
boolean hasBounaryChars = boundaryChars != null;
out.writeBoolean(hasBounaryChars);
if (hasBounaryChars) {
out.writeString(String.valueOf(boundaryChars));
}
out.writeOptionalVInt(noMatchSize);
out.writeOptionalVInt(phraseLimit);
boolean hasOptions = options != null;
out.writeBoolean(hasOptions);
if (hasOptions) {
out.writeMap(options);
}
out.writeOptionalBoolean(requireFieldMatch);
doWriteTo(out);
}
protected abstract void doWriteTo(StreamOutput out) throws IOException;
/**
* Set the pre tags that will be used for highlighting.
*/
@ -535,74 +605,4 @@ public abstract class AbstractHighlighterBuilder<HB extends AbstractHighlighterB
* fields only present in subclass should be checked for equality in the implementation
*/
protected abstract boolean doEquals(HB other);
/**
* read common parameters from {@link StreamInput}
*/
@SuppressWarnings("unchecked")
protected HB readOptionsFrom(StreamInput in) throws IOException {
preTags(in.readOptionalStringArray());
postTags(in.readOptionalStringArray());
fragmentSize(in.readOptionalVInt());
numOfFragments(in.readOptionalVInt());
highlighterType(in.readOptionalString());
fragmenter(in.readOptionalString());
if (in.readBoolean()) {
highlightQuery(in.readQuery());
}
if (in.readBoolean()) {
order(Order.PROTOTYPE.readFrom(in));
}
highlightFilter(in.readOptionalBoolean());
forceSource(in.readOptionalBoolean());
boundaryMaxScan(in.readOptionalVInt());
if (in.readBoolean()) {
boundaryChars(in.readString().toCharArray());
}
noMatchSize(in.readOptionalVInt());
phraseLimit(in.readOptionalVInt());
if (in.readBoolean()) {
options(in.readMap());
}
requireFieldMatch(in.readOptionalBoolean());
return (HB) this;
}
/**
* write common parameters to {@link StreamOutput}
*/
protected void writeOptionsTo(StreamOutput out) throws IOException {
out.writeOptionalStringArray(preTags);
out.writeOptionalStringArray(postTags);
out.writeOptionalVInt(fragmentSize);
out.writeOptionalVInt(numOfFragments);
out.writeOptionalString(highlighterType);
out.writeOptionalString(fragmenter);
boolean hasQuery = highlightQuery != null;
out.writeBoolean(hasQuery);
if (hasQuery) {
out.writeQuery(highlightQuery);
}
boolean hasSetOrder = order != null;
out.writeBoolean(hasSetOrder);
if (hasSetOrder) {
order.writeTo(out);
}
out.writeOptionalBoolean(highlightFilter);
out.writeOptionalBoolean(forceSource);
out.writeOptionalVInt(boundaryMaxScan);
boolean hasBounaryChars = boundaryChars != null;
out.writeBoolean(hasBounaryChars);
if (hasBounaryChars) {
out.writeString(String.valueOf(boundaryChars));
}
out.writeOptionalVInt(noMatchSize);
out.writeOptionalVInt(phraseLimit);
boolean hasOptions = options != null;
out.writeBoolean(hasOptions);
if (hasOptions) {
out.writeMap(options);
}
out.writeOptionalBoolean(requireFieldMatch);
}
}

View File

@ -60,10 +60,7 @@ import static org.elasticsearch.common.xcontent.ObjectParser.fromList;
*
* @see org.elasticsearch.search.builder.SearchSourceBuilder#highlight()
*/
public class HighlightBuilder extends AbstractHighlighterBuilder<HighlightBuilder> implements Writeable<HighlightBuilder> {
public static final HighlightBuilder PROTOTYPE = new HighlightBuilder();
public class HighlightBuilder extends AbstractHighlighterBuilder<HighlightBuilder> {
/** default for whether to highlight fields based on the source even if stored separately */
public static final boolean DEFAULT_FORCE_SOURCE = false;
/** default for whether a field should be highlighted only if a query matches that field */
@ -114,6 +111,32 @@ public class HighlightBuilder extends AbstractHighlighterBuilder<HighlightBuilde
private boolean useExplicitFieldOrder = false;
public HighlightBuilder() {
}
/**
* Read from a stream.
*/
public HighlightBuilder(StreamInput in) throws IOException {
super(in);
encoder(in.readOptionalString());
useExplicitFieldOrder(in.readBoolean());
int fields = in.readVInt();
for (int i = 0; i < fields; i++) {
field(new Field(in));
}
}
@Override
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeOptionalString(encoder);
out.writeBoolean(useExplicitFieldOrder);
out.writeVInt(fields.size());
for (int i = 0; i < fields.size(); i++) {
fields.get(i).writeTo(out);
}
}
/**
* Adds a field to be highlighted with default fragment size of 100 characters, and
* default number of fragments of 5 using the default encoder
@ -393,32 +416,7 @@ public class HighlightBuilder extends AbstractHighlighterBuilder<HighlightBuilde
Objects.equals(fields, other.fields);
}
@Override
public HighlightBuilder readFrom(StreamInput in) throws IOException {
HighlightBuilder highlightBuilder = new HighlightBuilder();
highlightBuilder.readOptionsFrom(in)
.encoder(in.readOptionalString())
.useExplicitFieldOrder(in.readBoolean());
int fields = in.readVInt();
for (int i = 0; i < fields; i++) {
highlightBuilder.field(Field.PROTOTYPE.readFrom(in));
}
return highlightBuilder;
}
@Override
public void writeTo(StreamOutput out) throws IOException {
writeOptionsTo(out);
out.writeOptionalString(encoder);
out.writeBoolean(useExplicitFieldOrder);
out.writeVInt(fields.size());
for (int i = 0; i < fields.size(); i++) {
fields.get(i).writeTo(out);
}
}
public static class Field extends AbstractHighlighterBuilder<Field> implements Writeable<Field> {
static final Field PROTOTYPE = new Field("_na_");
public static class Field extends AbstractHighlighterBuilder<Field> {
static final NamedObjectParser<Field, QueryParseContext> PARSER;
static {
ObjectParser<Field, QueryParseContext> parser = new ObjectParser<>("highlight_field");
@ -438,6 +436,23 @@ public class HighlightBuilder extends AbstractHighlighterBuilder<HighlightBuilde
this.name = name;
}
/**
* Read from a stream.
*/
public Field(StreamInput in) throws IOException {
super(in);
name = in.readString();
fragmentOffset(in.readVInt());
matchedFields(in.readOptionalStringArray());
}
@Override
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeString(name);
out.writeVInt(fragmentOffset);
out.writeOptionalStringArray(matchedFields);
}
public String name() {
return name;
}
@ -483,32 +498,12 @@ public class HighlightBuilder extends AbstractHighlighterBuilder<HighlightBuilde
Objects.equals(fragmentOffset, other.fragmentOffset) &&
Arrays.equals(matchedFields, other.matchedFields);
}
@Override
public Field readFrom(StreamInput in) throws IOException {
Field field = new Field(in.readString());
field.fragmentOffset(in.readVInt());
field.matchedFields(in.readOptionalStringArray());
field.readOptionsFrom(in);
return field;
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(name);
out.writeVInt(fragmentOffset);
out.writeOptionalStringArray(matchedFields);
writeOptionsTo(out);
}
}
public enum Order implements Writeable<Order> {
NONE, SCORE;
static Order PROTOTYPE = NONE;
@Override
public Order readFrom(StreamInput in) throws IOException {
public static Order readFromStream(StreamInput in) throws IOException {
int ordinal = in.readVInt();
if (ordinal < 0 || ordinal >= values().length) {
throw new IOException("Unknown Order ordinal [" + ordinal + "]");

View File

@ -744,7 +744,7 @@ public class HighlightBuilderTests extends ESTestCase {
try (BytesStreamOutput output = new BytesStreamOutput()) {
original.writeTo(output);
try (StreamInput in = new NamedWriteableAwareStreamInput(StreamInput.wrap(output.bytes()), namedWriteableRegistry)) {
return HighlightBuilder.PROTOTYPE.readFrom(in);
return new HighlightBuilder(in);
}
}
}