
org.elasticsearch.index.reindex.remote.RemoteResponseParsers Maven / Gradle / Ivy
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.index.reindex.remote;
import org.elasticsearch.Version;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.ParsingException;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.ObjectParser;
import org.elasticsearch.common.xcontent.ObjectParser.ValueType;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentLocation;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.reindex.ScrollableHitSource.BasicHit;
import org.elasticsearch.index.reindex.ScrollableHitSource.Hit;
import org.elasticsearch.index.reindex.ScrollableHitSource.Response;
import org.elasticsearch.index.reindex.ScrollableHitSource.SearchFailure;
import java.io.IOException;
import java.util.List;
import java.util.function.BiFunction;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static java.util.Objects.requireNonNull;
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg;
/**
* Parsers to convert the response from the remote host into objects useful for {@link RemoteScrollableHitSource}.
*/
final class RemoteResponseParsers {
private RemoteResponseParsers() {}
/**
* Parser for an individual {@code hit} element.
*/
public static final ConstructingObjectParser HIT_PARSER =
new ConstructingObjectParser<>("hit", true, a -> {
int i = 0;
String index = (String) a[i++];
String type = (String) a[i++];
String id = (String) a[i++];
Long version = (Long) a[i++];
return new BasicHit(index, type, id, version == null ? -1 : version);
});
static {
HIT_PARSER.declareString(constructorArg(), new ParseField("_index"));
HIT_PARSER.declareString(constructorArg(), new ParseField("_type"));
HIT_PARSER.declareString(constructorArg(), new ParseField("_id"));
HIT_PARSER.declareLong(optionalConstructorArg(), new ParseField("_version"));
HIT_PARSER.declareObject(((basicHit, tuple) -> basicHit.setSource(tuple.v1(), tuple.v2())), (p, s) -> {
try {
/*
* We spool the data from the remote back into xcontent so we can get bytes to send. There ought to be a better way but for
* now this should do.
*/
try (XContentBuilder b = XContentBuilder.builder(s.xContent())) {
b.copyCurrentStructure(p);
// a hack but this lets us get the right xcontent type to go with the source
return new Tuple<>(BytesReference.bytes(b), s);
}
} catch (IOException e) {
throw new ParsingException(p.getTokenLocation(), "[hit] failed to parse [_source]", e);
}
}, new ParseField("_source"));
ParseField routingField = new ParseField("_routing");
ParseField parentField = new ParseField("_parent");
ParseField ttlField = new ParseField("_ttl");
HIT_PARSER.declareString(BasicHit::setRouting, routingField);
HIT_PARSER.declareString(BasicHit::setParent, parentField);
// Pre-2.0.0 parent and routing come back in "fields"
class Fields {
String routing;
String parent;
}
ObjectParser fieldsParser = new ObjectParser<>("fields", Fields::new);
HIT_PARSER.declareObject((hit, fields) -> {
hit.setRouting(fields.routing);
hit.setParent(fields.parent);
}, fieldsParser, new ParseField("fields"));
fieldsParser.declareString((fields, routing) -> fields.routing = routing, routingField);
fieldsParser.declareString((fields, parent) -> fields.parent = parent, parentField);
fieldsParser.declareLong((fields, ttl) -> {}, ttlField); // ignore ttls since they have been removed
}
/**
* Parser for the {@code hits} element. Parsed to an array of {@code [total (Long), hits (List)]}.
*/
public static final ConstructingObjectParser
© 2015 - 2025 Weber Informatics LLC | Privacy Policy