improvement to string splitting caused fields= on get to return the source back, fix it and also optimize this case when using realtime get, closes #1164.

This commit is contained in:
Shay Banon 2011-07-26 17:12:34 +03:00
parent 47b0750171
commit cbde265ab8
4 changed files with 30 additions and 8 deletions

View File

@ -125,10 +125,11 @@ public class TransportGetAction extends TransportShardSingleOperationAction<GetR
} }
public static GetResponse load(ESLogger logger, ScriptService scriptService, IndexService indexService, IndexShard indexShard, String index, String type, String id, String[] gFields, boolean realtime) throws ElasticSearchException { public static GetResponse load(ESLogger logger, ScriptService scriptService, IndexService indexService, IndexShard indexShard, String index, String type, String id, String[] gFields, boolean realtime) throws ElasticSearchException {
boolean loadSource = gFields == null || gFields.length > 0;
Engine.GetResult get = null; Engine.GetResult get = null;
if (type == null || type.equals("_all")) { if (type == null || type.equals("_all")) {
for (String typeX : indexService.mapperService().types()) { for (String typeX : indexService.mapperService().types()) {
get = indexShard.get(new Engine.Get(realtime, UidFieldMapper.TERM_FACTORY.createTerm(Uid.createUid(typeX, id)))); get = indexShard.get(new Engine.Get(realtime, UidFieldMapper.TERM_FACTORY.createTerm(Uid.createUid(typeX, id))).loadSource(loadSource));
if (get.exists()) { if (get.exists()) {
type = typeX; type = typeX;
break; break;
@ -138,7 +139,7 @@ public class TransportGetAction extends TransportShardSingleOperationAction<GetR
return new GetResponse(index, type, id, -1, false, null, null); return new GetResponse(index, type, id, -1, false, null, null);
} }
} else { } else {
get = indexShard.get(new Engine.Get(realtime, UidFieldMapper.TERM_FACTORY.createTerm(Uid.createUid(type, id)))); get = indexShard.get(new Engine.Get(realtime, UidFieldMapper.TERM_FACTORY.createTerm(Uid.createUid(type, id))).loadSource(loadSource));
if (!get.exists()) { if (!get.exists()) {
return new GetResponse(index, type, id, -1, false, null, null); return new GetResponse(index, type, id, -1, false, null, null);
} }
@ -243,13 +244,17 @@ public class TransportGetAction extends TransportShardSingleOperationAction<GetR
return new GetResponse(index, type, id, get.version(), get.exists(), source == null ? null : new BytesHolder(source), fields); return new GetResponse(index, type, id, get.version(), get.exists(), source == null ? null : new BytesHolder(source), fields);
} else { } else {
BytesHolder source = get.source(); BytesHolder source = get.source();
assert source != null;
Map<String, GetField> fields = null; Map<String, GetField> fields = null;
boolean sourceRequested = false; boolean sourceRequested = false;
// we can only load scripts that can run against the source // we can only load scripts that can run against the source
if (gFields != null && gFields.length > 0) { if (gFields == null) {
sourceRequested = true;
} else if (gFields.length == 0) {
// no fields, and no source
sourceRequested = false;
} else {
Map<String, Object> sourceAsMap = SourceLookup.sourceAsMap(source.bytes(), source.offset(), source.length()); Map<String, Object> sourceAsMap = SourceLookup.sourceAsMap(source.bytes(), source.offset(), source.length());
SearchLookup searchLookup = null; SearchLookup searchLookup = null;
for (String field : gFields) { for (String field : gFields) {
@ -297,8 +302,6 @@ public class TransportGetAction extends TransportShardSingleOperationAction<GetR
} }
} }
} }
} else {
sourceRequested = true;
} }
return new GetResponse(index, type, id, get.version(), get.exists(), sourceRequested ? source : null, fields); return new GetResponse(index, type, id, get.version(), get.exists(), sourceRequested ? source : null, fields);

View File

@ -597,6 +597,7 @@ public interface Engine extends IndexShardComponent, CloseableComponent {
static class Get { static class Get {
private final boolean realtime; private final boolean realtime;
private final Term uid; private final Term uid;
private boolean loadSource = true;
public Get(boolean realtime, Term uid) { public Get(boolean realtime, Term uid) {
this.realtime = realtime; this.realtime = realtime;
@ -610,6 +611,15 @@ public interface Engine extends IndexShardComponent, CloseableComponent {
public Term uid() { public Term uid() {
return uid; return uid;
} }
public boolean loadSource() {
return this.loadSource;
}
public Get loadSource(boolean loadSource) {
this.loadSource = loadSource;
return this;
}
} }
static class GetResult { static class GetResult {
@ -621,7 +631,7 @@ public interface Engine extends IndexShardComponent, CloseableComponent {
public static final GetResult NOT_EXISTS = new GetResult(false, -1, null); public static final GetResult NOT_EXISTS = new GetResult(false, -1, null);
public GetResult(boolean exists, long version, BytesHolder source) { public GetResult(boolean exists, long version, @Nullable BytesHolder source) {
this.source = source; this.source = source;
this.exists = exists; this.exists = exists;
this.version = version; this.version = version;
@ -645,7 +655,7 @@ public interface Engine extends IndexShardComponent, CloseableComponent {
return this.version; return this.version;
} }
public BytesHolder source() { @Nullable public BytesHolder source() {
return source; return source;
} }

View File

@ -305,6 +305,9 @@ public class RobinEngine extends AbstractIndexShardComponent implements Engine {
if (versionValue.delete()) { if (versionValue.delete()) {
return GetResult.NOT_EXISTS; return GetResult.NOT_EXISTS;
} }
if (!get.loadSource()) {
return new GetResult(true, versionValue.version(), null);
}
byte[] data = translog.read(versionValue.translogLocation()); byte[] data = translog.read(versionValue.translogLocation());
if (data != null) { if (data != null) {
try { try {

View File

@ -26,6 +26,7 @@ import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.get.MultiGetRequest; import org.elasticsearch.action.get.MultiGetRequest;
import org.elasticsearch.action.get.MultiGetResponse; import org.elasticsearch.action.get.MultiGetResponse;
import org.elasticsearch.client.Client; import org.elasticsearch.client.Client;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.test.integration.AbstractNodesTests; import org.elasticsearch.test.integration.AbstractNodesTests;
import org.testng.annotations.AfterClass; import org.testng.annotations.AfterClass;
@ -73,6 +74,11 @@ public class GetActionTests extends AbstractNodesTests {
assertThat(response.sourceAsMap().get("field1").toString(), equalTo("value1")); assertThat(response.sourceAsMap().get("field1").toString(), equalTo("value1"));
assertThat(response.sourceAsMap().get("field2").toString(), equalTo("value2")); assertThat(response.sourceAsMap().get("field2").toString(), equalTo("value2"));
logger.info("--> realtime get 1 (no source)");
response = client.prepareGet("test", "type1", "1").setFields(Strings.EMPTY_ARRAY).execute().actionGet();
assertThat(response.exists(), equalTo(true));
assertThat(response.source(), nullValue());
logger.info("--> realtime get 1 (no type)"); logger.info("--> realtime get 1 (no type)");
response = client.prepareGet("test", null, "1").execute().actionGet(); response = client.prepareGet("test", null, "1").execute().actionGet();
assertThat(response.exists(), equalTo(true)); assertThat(response.exists(), equalTo(true));