Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always return metadata in get/search APIs. #11816

Merged
merged 1 commit into from Jun 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 0 additions & 10 deletions core/src/main/java/org/elasticsearch/index/engine/Engine.java
Expand Up @@ -995,7 +995,6 @@ public long endTime() {
public static class Get {
private final boolean realtime;
private final Term uid;
private boolean loadSource = true;
private long version = Versions.MATCH_ANY;
private VersionType versionType = VersionType.INTERNAL;

Expand All @@ -1012,15 +1011,6 @@ public Term uid() {
return uid;
}

public boolean loadSource() {
return this.loadSource;
}

public Get loadSource(boolean loadSource) {
this.loadSource = loadSource;
return this;
}

public long version() {
return version;
}
Expand Down
Expand Up @@ -305,9 +305,6 @@ public GetResult get(Get get) throws EngineException {
Uid uid = Uid.createUid(get.uid().text());
throw new VersionConflictEngineException(shardId, uid.type(), uid.id(), versionValue.version(), get.version());
}
if (!get.loadSource()) {
return new GetResult(true, versionValue.version(), null);
}
Translog.Operation op = translog.read(versionValue.translogLocation());
if (op != null) {
return new GetResult(true, versionValue.version(), op.getSource());
Expand Down
Expand Up @@ -27,6 +27,7 @@
public class AllFieldsVisitor extends FieldsVisitor {

public AllFieldsVisitor() {
super(true);
}

@Override
Expand Down
Expand Up @@ -19,8 +19,6 @@
package org.elasticsearch.index.fieldvisitor;

import org.apache.lucene.index.FieldInfo;
import org.elasticsearch.index.mapper.internal.SourceFieldMapper;
import org.elasticsearch.index.mapper.internal.UidFieldMapper;

import java.io.IOException;
import java.util.Set;
Expand All @@ -32,21 +30,16 @@
*/
public class CustomFieldsVisitor extends FieldsVisitor {

private final boolean loadSource;
private final Set<String> fields;

public CustomFieldsVisitor(Set<String> fields, boolean loadSource) {
this.loadSource = loadSource;
super(loadSource);
this.fields = fields;
}

@Override
public Status needsField(FieldInfo fieldInfo) throws IOException {

if (loadSource && SourceFieldMapper.NAME.equals(fieldInfo.name)) {
return Status.YES;
}
if (UidFieldMapper.NAME.equals(fieldInfo.name)) {
if (super.needsField(fieldInfo) == Status.YES) {
return Status.YES;
}

Expand Down
Expand Up @@ -19,6 +19,7 @@
package org.elasticsearch.index.fieldvisitor;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;

import org.apache.lucene.index.FieldInfo;
import org.apache.lucene.index.StoredFieldVisitor;
Expand All @@ -27,30 +28,63 @@
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.FieldMappers;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.mapper.Uid;
import org.elasticsearch.index.mapper.internal.ParentFieldMapper;
import org.elasticsearch.index.mapper.internal.RoutingFieldMapper;
import org.elasticsearch.index.mapper.internal.SourceFieldMapper;
import org.elasticsearch.index.mapper.internal.TTLFieldMapper;
import org.elasticsearch.index.mapper.internal.TimestampFieldMapper;
import org.elasticsearch.index.mapper.internal.UidFieldMapper;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static com.google.common.collect.Maps.newHashMap;

/**
* Base {@link StoredFieldsVisitor} that retrieves all non-redundant metadata.
*/
public abstract class FieldsVisitor extends StoredFieldVisitor {

public class FieldsVisitor extends StoredFieldVisitor {

private static final Set<String> BASE_REQUIRED_FIELDS = ImmutableSet.of(
UidFieldMapper.NAME,
TimestampFieldMapper.NAME,
TTLFieldMapper.NAME,
RoutingFieldMapper.NAME,
ParentFieldMapper.NAME
);

private final boolean loadSource;
private final Set<String> requiredFields;
protected BytesReference source;
protected Uid uid;
protected Map<String, List<Object>> fieldsValues;

public FieldsVisitor(boolean loadSource) {
this.loadSource = loadSource;
requiredFields = new HashSet<>();
reset();
}

@Override
public Status needsField(FieldInfo fieldInfo) throws IOException {
if (requiredFields.remove(fieldInfo.name)) {
return Status.YES;
}
// All these fields are single-valued so we can stop when the set is
// empty
return requiredFields.isEmpty()
? Status.STOP
: Status.NO;
}

public void postProcess(MapperService mapperService) {
if (uid != null) {
DocumentMapper documentMapper = mapperService.documentMapper(uid.type());
Expand Down Expand Up @@ -133,6 +167,18 @@ public Uid uid() {
return uid;
}

public String routing() {
if (fieldsValues == null) {
return null;
}
List<Object> values = fieldsValues.get(RoutingFieldMapper.NAME);
if (values == null || values.isEmpty()) {
return null;
}
assert values.size() == 1;
return values.get(0).toString();
}

public Map<String, List<Object>> fields() {
return fieldsValues != null
? fieldsValues
Expand All @@ -143,6 +189,11 @@ public void reset() {
if (fieldsValues != null) fieldsValues.clear();
source = null;
uid = null;

requiredFields.addAll(BASE_REQUIRED_FIELDS);
if (loadSource) {
requiredFields.add(SourceFieldMapper.NAME);
}
}

void addValue(String name, Object value) {
Expand Down

This file was deleted.

Expand Up @@ -27,6 +27,10 @@
*/
public class JustUidFieldsVisitor extends FieldsVisitor {

public JustUidFieldsVisitor() {
super(false);
}

@Override
public Status needsField(FieldInfo fieldInfo) throws IOException {
if (UidFieldMapper.NAME.equals(fieldInfo.name)) {
Expand Down
Expand Up @@ -35,6 +35,7 @@ public class SingleFieldsVisitor extends FieldsVisitor {
private String field;

public SingleFieldsVisitor(String field) {
super(false);
this.field = field;
}

Expand Down

This file was deleted.

This file was deleted.