Add ConstructingObjectParser to RatedDocumentKey

This commit is contained in:
Isabel Drost-Fromm 2016-08-09 12:49:07 +02:00
parent c2bd58e13d
commit 0be997232a
1 changed files with 9 additions and 11 deletions

View File

@ -24,7 +24,7 @@ import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.xcontent.ObjectParser; import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
@ -36,12 +36,13 @@ public class RatedDocumentKey extends ToXContentToBytes implements Writeable {
public static final ParseField TYPE_FIELD = new ParseField("type"); public static final ParseField TYPE_FIELD = new ParseField("type");
public static final ParseField INDEX_FIELD = new ParseField("index"); public static final ParseField INDEX_FIELD = new ParseField("index");
private static final ObjectParser<RatedDocumentKey, RankEvalContext> PARSER = new ObjectParser<>("ratings", RatedDocumentKey::new); private static final ConstructingObjectParser<RatedDocumentKey, RankEvalContext> PARSER = new ConstructingObjectParser<>("ratings",
a -> new RatedDocumentKey((String) a[0], (String) a[1], (String) a[2]));
static { static {
PARSER.declareString(RatedDocumentKey::setIndex, INDEX_FIELD); PARSER.declareString(ConstructingObjectParser.constructorArg(), INDEX_FIELD);
PARSER.declareString(RatedDocumentKey::setType, TYPE_FIELD); PARSER.declareString(ConstructingObjectParser.constructorArg(), TYPE_FIELD);
PARSER.declareString(RatedDocumentKey::setDocId, DOC_ID_FIELD); PARSER.declareString(ConstructingObjectParser.constructorArg(), DOC_ID_FIELD);
} }
@Override @Override
@ -59,9 +60,6 @@ public class RatedDocumentKey extends ToXContentToBytes implements Writeable {
private String type; private String type;
private String index; private String index;
public RatedDocumentKey() {}
void setIndex(String index) { void setIndex(String index) {
this.index = index; this.index = index;
} }
@ -106,7 +104,7 @@ public class RatedDocumentKey extends ToXContentToBytes implements Writeable {
} }
public static RatedDocumentKey fromXContent(XContentParser parser, RankEvalContext context) throws IOException { public static RatedDocumentKey fromXContent(XContentParser parser, RankEvalContext context) throws IOException {
return PARSER.parse(parser, context); return PARSER.apply(parser, context);
} }
@Override @Override