Merge pull request #20424 from javanna/enhancement/error_fetch_source_disabled
Throw error when trying to fetch fields from source and source is disabled
This commit is contained in:
commit
119d198cc5
|
@ -36,9 +36,6 @@ public final class FetchSourceSubPhase implements FetchSubPhase {
|
|||
return;
|
||||
}
|
||||
SourceLookup source = context.lookup().source();
|
||||
if (source.internalSourceRef() == null) {
|
||||
return; // source disabled in the mapping
|
||||
}
|
||||
FetchSourceContext fetchSourceContext = context.fetchSourceContext();
|
||||
assert fetchSourceContext.fetchSource();
|
||||
if (fetchSourceContext.includes().length == 0 && fetchSourceContext.excludes().length == 0) {
|
||||
|
@ -46,6 +43,11 @@ public final class FetchSourceSubPhase implements FetchSubPhase {
|
|||
return;
|
||||
}
|
||||
|
||||
if (source.internalSourceRef() == null) {
|
||||
throw new IllegalArgumentException("unable to fetch fields from _source field: _source is disabled in the mappings " +
|
||||
"for index [" + context.indexShard().shardId().getIndexName() + "]");
|
||||
}
|
||||
|
||||
Object value = source.filter(fetchSourceContext.includes(), fetchSourceContext.excludes());
|
||||
try {
|
||||
final int initialCapacity = Math.min(1024, source.internalSourceRef().length());
|
||||
|
|
|
@ -23,6 +23,8 @@ import org.elasticsearch.common.Strings;
|
|||
import org.elasticsearch.common.bytes.BytesReference;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||
import org.elasticsearch.index.shard.IndexShard;
|
||||
import org.elasticsearch.index.shard.ShardId;
|
||||
import org.elasticsearch.search.fetch.FetchSubPhase;
|
||||
import org.elasticsearch.search.internal.InternalSearchHit;
|
||||
import org.elasticsearch.search.internal.SearchContext;
|
||||
|
@ -33,37 +35,11 @@ import org.elasticsearch.test.TestSearchContext;
|
|||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class FetchSourceSubPhaseTests extends ESTestCase {
|
||||
|
||||
static class FetchSourceSubPhaseTestSearchContext extends TestSearchContext {
|
||||
|
||||
FetchSourceContext context;
|
||||
BytesReference source;
|
||||
|
||||
FetchSourceSubPhaseTestSearchContext(FetchSourceContext context, BytesReference source) {
|
||||
super(null);
|
||||
this.context = context;
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean sourceRequested() {
|
||||
return context != null && context.fetchSource();
|
||||
}
|
||||
|
||||
@Override
|
||||
public FetchSourceContext fetchSourceContext() {
|
||||
return context;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SearchLookup lookup() {
|
||||
SearchLookup lookup = super.lookup();
|
||||
lookup.source().setSource(source);
|
||||
return lookup;
|
||||
}
|
||||
}
|
||||
|
||||
public void testFetchSource() throws IOException {
|
||||
XContentBuilder source = XContentFactory.jsonBuilder().startObject()
|
||||
.field("field", "value")
|
||||
|
@ -109,11 +85,14 @@ public class FetchSourceSubPhaseTests extends ESTestCase {
|
|||
hitContext = hitExecute(null, false, null, null);
|
||||
assertNull(hitContext.hit().sourceAsMap());
|
||||
|
||||
hitContext = hitExecute(null, true, "field1", null);
|
||||
assertNull(hitContext.hit().sourceAsMap());
|
||||
IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> hitExecute(null, true, "field1", null));
|
||||
assertEquals("unable to fetch fields from _source field: _source is disabled in the mappings " +
|
||||
"for index [index]", exception.getMessage());
|
||||
|
||||
hitContext = hitExecuteMultiple(null, true, new String[]{"*"}, new String[]{"field2"});
|
||||
assertNull(hitContext.hit().sourceAsMap());
|
||||
exception = expectThrows(IllegalArgumentException.class,
|
||||
() -> hitExecuteMultiple(null, true, new String[]{"*"}, new String[]{"field2"}));
|
||||
assertEquals("unable to fetch fields from _source field: _source is disabled in the mappings " +
|
||||
"for index [index]", exception.getMessage());
|
||||
}
|
||||
|
||||
private FetchSubPhase.HitContext hitExecute(XContentBuilder source, boolean fetchSource, String include, String exclude) {
|
||||
|
@ -131,4 +110,40 @@ public class FetchSourceSubPhaseTests extends ESTestCase {
|
|||
phase.hitExecute(searchContext, hitContext);
|
||||
return hitContext;
|
||||
}
|
||||
|
||||
private static class FetchSourceSubPhaseTestSearchContext extends TestSearchContext {
|
||||
final FetchSourceContext context;
|
||||
final BytesReference source;
|
||||
final IndexShard indexShard;
|
||||
|
||||
FetchSourceSubPhaseTestSearchContext(FetchSourceContext context, BytesReference source) {
|
||||
super(null);
|
||||
this.context = context;
|
||||
this.source = source;
|
||||
this.indexShard = mock(IndexShard.class);
|
||||
when(indexShard.shardId()).thenReturn(new ShardId("index", "index", 1));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean sourceRequested() {
|
||||
return context != null && context.fetchSource();
|
||||
}
|
||||
|
||||
@Override
|
||||
public FetchSourceContext fetchSourceContext() {
|
||||
return context;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SearchLookup lookup() {
|
||||
SearchLookup lookup = super.lookup();
|
||||
lookup.source().setSource(source);
|
||||
return lookup;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IndexShard indexShard() {
|
||||
return indexShard;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,7 +19,6 @@
|
|||
package org.elasticsearch.search.fetch.subphase.highlight;
|
||||
|
||||
import com.carrotsearch.randomizedtesting.generators.RandomPicks;
|
||||
|
||||
import org.apache.lucene.search.join.ScoreMode;
|
||||
import org.elasticsearch.Version;
|
||||
import org.elasticsearch.action.index.IndexRequestBuilder;
|
||||
|
@ -51,8 +50,8 @@ import org.hamcrest.Matcher;
|
|||
import org.hamcrest.Matchers;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
|
@ -97,7 +96,7 @@ public class HighlighterSearchIT extends ESIntegTestCase {
|
|||
|
||||
@Override
|
||||
protected Collection<Class<? extends Plugin>> nodePlugins() {
|
||||
return Arrays.asList(InternalSettingsPlugin.class);
|
||||
return Collections.singletonList(InternalSettingsPlugin.class);
|
||||
}
|
||||
|
||||
public void testHighlightingWithWildcardName() throws IOException {
|
||||
|
|
|
@ -80,7 +80,7 @@ public class SearchFieldsIT extends ESIntegTestCase {
|
|||
|
||||
@Override
|
||||
protected Collection<Class<? extends Plugin>> nodePlugins() {
|
||||
return Arrays.asList(CustomScriptPlugin.class);
|
||||
return Collections.singletonList(CustomScriptPlugin.class);
|
||||
}
|
||||
|
||||
public static class CustomScriptPlugin extends MockScriptPlugin {
|
||||
|
|
Loading…
Reference in New Issue