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

Add the ability to disable the retrieval of the metadata fields #19918

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,11 @@ public SearchRequestBuilder setFetchSource(@Nullable String[] includes, @Nullabl
return this;
}

public SearchRequestBuilder setFetchMetadata(boolean fetch) {
sourceBuilder().fetchMetadata(fetch);
return this;
}

/**
* Adds a docvalue based field to load and return. The field does not have to be stored,
* but its recommended to use non analyzed or numeric fields.
Expand Down
19 changes: 18 additions & 1 deletion core/src/main/java/org/elasticsearch/search/SearchService.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import com.carrotsearch.hppc.ObjectFloatHashMap;
import org.apache.lucene.search.FieldDoc;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.ExceptionsHelper;
Expand Down Expand Up @@ -813,6 +812,24 @@ private void parseSource(DefaultSearchContext context, SearchSourceBuilder sourc
}
context.sliceBuilder(source.slice());
}

if (source.fetchMetadata() != null) {
if (source.fetchMetadata() == false) {
if (context.version()) {
throw new SearchContextException(context,
"`fetch_metadata` is required when version is requested");
}
if (context.hasFieldNames()) {
throw new SearchContextException(context,
"`fetch_metadata` is required when stored fields are requested");
}
if (context.sourceRequested() ||
(context.hasFetchSourceContext() == false && context.hasScriptFields())) {
throw new SearchContextException(context, "`fetch_metadata` cannot be false if _source is requested");
}
}
context.fetchMetadata(source.fetchMetadata());
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public final class SearchSourceBuilder extends ToXContentToBytes implements Writ
public static final ParseField VERSION_FIELD = new ParseField("version");
public static final ParseField EXPLAIN_FIELD = new ParseField("explain");
public static final ParseField _SOURCE_FIELD = new ParseField("_source");
public static final ParseField FETCH_METADATA_FIELD = new ParseField("fetch_metadata");
public static final ParseField FIELDS_FIELD = new ParseField("fields");
public static final ParseField STORED_FIELDS_FIELD = new ParseField("stored_fields");
public static final ParseField DOCVALUE_FIELDS_FIELD = new ParseField("docvalue_fields", "fielddata_fields");
Expand Down Expand Up @@ -152,6 +153,7 @@ public static HighlightBuilder highlight() {
private List<String> docValueFields;
private List<ScriptField> scriptFields;
private FetchSourceContext fetchSourceContext;
private Boolean fetchMetadata;

private AggregatorFactories.Builder aggregations;

Expand Down Expand Up @@ -183,6 +185,7 @@ public SearchSourceBuilder(StreamInput in) throws IOException {
aggregations = in.readOptionalWriteable(AggregatorFactories.Builder::new);
explain = in.readOptionalBoolean();
fetchSourceContext = in.readOptionalStreamable(FetchSourceContext::new);
fetchMetadata = in.readOptionalBoolean();
docValueFields = (List<String>) in.readGenericValue();
storedFieldNames = (List<String>) in.readGenericValue();
from = in.readVInt();
Expand Down Expand Up @@ -243,6 +246,7 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeOptionalWriteable(aggregations);
out.writeOptionalBoolean(explain);
out.writeOptionalStreamable(fetchSourceContext);
out.writeOptionalBoolean(fetchMetadata);
out.writeGenericValue(docValueFields);
out.writeGenericValue(storedFieldNames);
out.writeVInt(from);
Expand Down Expand Up @@ -705,6 +709,21 @@ public FetchSourceContext fetchSource() {
return fetchSourceContext;
}

/**
* Indicate if the metadata fields should be fetched
*/
public SearchSourceBuilder fetchMetadata(boolean fetch) {
this.fetchMetadata = fetch;
return this;
}

/**
* Returns true if the metadata fields should be fetched
*/
public Boolean fetchMetadata() {
return fetchMetadata;
}

/**
* Adds a stored field to load and return as part of the
* search request. If none are specified, the source of the document will be
Expand Down Expand Up @@ -911,6 +930,7 @@ private SearchSourceBuilder shallowCopy(QueryBuilder queryBuilder, QueryBuilder
rewrittenBuilder.explain = explain;
rewrittenBuilder.ext = ext;
rewrittenBuilder.fetchSourceContext = fetchSourceContext;
rewrittenBuilder.fetchMetadata = fetchMetadata;
rewrittenBuilder.docValueFields = docValueFields;
rewrittenBuilder.storedFieldNames = storedFieldNames;
rewrittenBuilder.from = from;
Expand Down Expand Up @@ -972,6 +992,8 @@ public void parseXContent(QueryParseContext context, AggregatorParsers aggParser
trackScores = parser.booleanValue();
} else if (context.getParseFieldMatcher().match(currentFieldName, _SOURCE_FIELD)) {
fetchSourceContext = FetchSourceContext.parse(context);
} else if (context.getParseFieldMatcher().match(currentFieldName, FETCH_METADATA_FIELD)) {
fetchMetadata = parser.booleanValue();
} else if (context.getParseFieldMatcher().match(currentFieldName, STORED_FIELDS_FIELD)) {
storedField(parser.text());
} else if (context.getParseFieldMatcher().match(currentFieldName, SORT_FIELD)) {
Expand Down Expand Up @@ -1141,6 +1163,10 @@ public void innerToXContent(XContentBuilder builder, Params params) throws IOExc
builder.field(_SOURCE_FIELD.getPreferredName(), fetchSourceContext);
}

if (fetchMetadata != null) {
builder.field(FETCH_METADATA_FIELD.getPreferredName(), fetchMetadata);
}

if (storedFieldNames != null) {
if (storedFieldNames.size() == 1) {
builder.field(STORED_FIELDS_FIELD.getPreferredName(), storedFieldNames.get(0));
Expand Down Expand Up @@ -1349,7 +1375,7 @@ public boolean equals(Object obj) {

@Override
public int hashCode() {
return Objects.hash(aggregations, explain, fetchSourceContext, docValueFields, storedFieldNames, from,
return Objects.hash(aggregations, explain, fetchSourceContext, fetchMetadata, docValueFields, storedFieldNames, from,
highlightBuilder, indexBoost, minScore, postQueryBuilder, queryBuilder, rescoreBuilders, scriptFields,
size, sorts, searchAfterBuilder, sliceBuilder, stats, suggestBuilder, terminateAfter, timeout, trackScores, version, profile);
}
Expand All @@ -1366,6 +1392,7 @@ public boolean equals(Object obj) {
return Objects.equals(aggregations, other.aggregations)
&& Objects.equals(explain, other.explain)
&& Objects.equals(fetchSourceContext, other.fetchSourceContext)
&& Objects.equals(fetchMetadata, other.fetchMetadata)
&& Objects.equals(docValueFields, other.docValueFields)
&& Objects.equals(storedFieldNames, other.storedFieldNames)
&& Objects.equals(from, other.from)
Expand Down
36 changes: 26 additions & 10 deletions core/src/main/java/org/elasticsearch/search/fetch/FetchPhase.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,21 @@ public void execute(SearchContext context) {
List<String> fieldNamePatterns = null;
if (!context.hasFieldNames()) {
// no fields specified, default to return source if no explicit indication
if (!context.hasScriptFields() && !context.hasFetchSourceContext()) {
if (!context.hasScriptFields() && !context.hasFetchSourceContext() && context.fetchMetadata()) {
context.fetchSourceContext(new FetchSourceContext(true));
}
fieldsVisitor = new FieldsVisitor(context.sourceRequested());
if (context.fetchMetadata() == false) {
assert context.sourceRequested() == false;
fieldsVisitor = null;
} else {
fieldsVisitor = new FieldsVisitor(context.sourceRequested());
}
} else if (context.fieldNames().isEmpty()) {
fieldsVisitor = new FieldsVisitor(context.sourceRequested());
if (context.fetchMetadata() == false && context.sourceRequested() == false) {
fieldsVisitor = null;
} else {
fieldsVisitor = new FieldsVisitor(context.sourceRequested());
}
} else {
for (String fieldName : context.fieldNames()) {
if (fieldName.equals(SourceFieldMapper.NAME)) {
Expand Down Expand Up @@ -182,6 +191,9 @@ private int findRootDocumentIfNested(SearchContext context, LeafReaderContext su
}

private InternalSearchHit createSearchHit(SearchContext context, FieldsVisitor fieldsVisitor, int docId, int subDocId, LeafReaderContext subReaderContext) {
if (fieldsVisitor == null) {
return new InternalSearchHit(docId, null, null, null);
}
loadStoredFields(context, subReaderContext, fieldsVisitor, subDocId);
fieldsVisitor.postProcess(context.mapperService());

Expand All @@ -193,14 +205,18 @@ private InternalSearchHit createSearchHit(SearchContext context, FieldsVisitor f
}
}

DocumentMapper documentMapper = context.mapperService().documentMapper(fieldsVisitor.uid().type());
Text typeText;
if (documentMapper == null) {
typeText = new Text(fieldsVisitor.uid().type());
} else {
typeText = documentMapper.typeText();
Text typeText = null;
String id = null;
if (context.fetchMetadata()) {
DocumentMapper documentMapper = context.mapperService().documentMapper(fieldsVisitor.uid().type());
if (documentMapper == null) {
typeText = new Text(fieldsVisitor.uid().type());
} else {
typeText = documentMapper.typeText();
}
id = fieldsVisitor.uid().id();
}
InternalSearchHit searchHit = new InternalSearchHit(docId, fieldsVisitor.uid().id(), typeText, searchFields);
InternalSearchHit searchHit = new InternalSearchHit(docId, id, typeText, searchFields);
// Set _source if requested.
SourceLookup sourceLookup = context.lookup().source();
sourceLookup.setSegmentAndDocument(subReaderContext, subDocId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public final class ParentFieldSubFetchPhase implements FetchSubPhase {

@Override
public void hitExecute(SearchContext context, HitContext hitContext) {
if (context.fetchMetadata() == false) {
return ;
}
ParentFieldMapper parentFieldMapper = context.mapperService().documentMapper(hitContext.hit().type()).parentFieldMapper();
if (parentFieldMapper.active() == false) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public class DefaultSearchContext extends SearchContext {
private List<String> fieldNames;
private ScriptFieldsContext scriptFields;
private FetchSourceContext fetchSourceContext;
private boolean fetchMetadata = true;
private int from = -1;
private int size = -1;
private SortAndFormats sort;
Expand Down Expand Up @@ -469,6 +470,18 @@ public SearchContext fetchSourceContext(FetchSourceContext fetchSourceContext) {
return this;
}

@Override
public boolean fetchMetadata() {
return fetchMetadata;
}

@Override
public SearchContext fetchMetadata(boolean fetch) {
this.fetchMetadata = fetch;
return this;
}


@Override
public ContextIndexSearcher searcher() {
return this.searcher;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,16 @@ public SearchContext fetchSourceContext(FetchSourceContext fetchSourceContext) {
return in.fetchSourceContext(fetchSourceContext);
}

@Override
public boolean fetchMetadata() {
return in.fetchMetadata();
}

@Override
public SearchContext fetchMetadata(boolean fetch) {
return in.fetchMetadata(fetch);
}

@Override
public ContextIndexSearcher searcher() {
return in.searcher();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,16 @@ private InternalSearchHit() {

}

public InternalSearchHit(int docId, String id, Text type, Map<String, SearchHitField> fields) {
public InternalSearchHit(int docId, @Nullable String id, @Nullable Text type, @Nullable Map<String, SearchHitField> fields) {
this.docId = docId;
this.id = new Text(id);
this.id = id != null ? new Text(id) : null;
this.type = type;
this.fields = fields;
}

public InternalSearchHit(int nestedTopDocId, String id, Text type, InternalNestedIdentity nestedIdentity, Map<String, SearchHitField> fields) {
this.docId = nestedTopDocId;
this.id = new Text(id);
this.id = id != null ? new Text(id) : null;
this.type = type;
this.nestedIdentity = nestedIdentity;
this.fields = fields;
Expand Down Expand Up @@ -168,7 +168,7 @@ public String getIndex() {

@Override
public String id() {
return id.string();
return id != null ? id.string() : null;
}

@Override
Expand All @@ -178,7 +178,7 @@ public String getId() {

@Override
public String type() {
return type.string();
return type != null ? type.string() : null;
}

@Override
Expand Down Expand Up @@ -444,8 +444,12 @@ public XContentBuilder toInnerXContent(XContentBuilder builder, Params params) t
if (shard != null) {
builder.field(Fields._INDEX, shard.indexText());
}
builder.field(Fields._TYPE, type);
builder.field(Fields._ID, id);
if (type != null) {
builder.field(Fields._TYPE, type);
}
if (id != null) {
builder.field(Fields._ID, id);
}
}
if (version != -1) {
builder.field(Fields._VERSION, version);
Expand Down Expand Up @@ -555,8 +559,8 @@ public void readFrom(StreamInput in) throws IOException {

public void readFrom(StreamInput in, InternalSearchHits.StreamContext context) throws IOException {
score = in.readFloat();
id = in.readText();
type = in.readText();
id = in.readOptionalText();
type = in.readOptionalText();
nestedIdentity = in.readOptionalStreamable(InternalNestedIdentity::new);
version = in.readLong();
source = in.readBytesReference();
Expand Down Expand Up @@ -664,8 +668,8 @@ public void writeTo(StreamOutput out) throws IOException {

public void writeTo(StreamOutput out, InternalSearchHits.StreamContext context) throws IOException {
out.writeFloat(score);
out.writeText(id);
out.writeText(type);
out.writeOptionalText(id);
out.writeOptionalText(type);
out.writeOptionalStreamable(nestedIdentity);
out.writeLong(version);
out.writeBytesReference(source);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicBoolean;

public abstract class SearchContext implements Releasable {
Expand Down Expand Up @@ -200,6 +199,10 @@ public InnerHitsContext innerHits() {

public abstract SearchContext fetchSourceContext(FetchSourceContext fetchSourceContext);

public abstract boolean fetchMetadata();

public abstract SearchContext fetchMetadata(boolean fetch);

public abstract ContextIndexSearcher searcher();

public abstract IndexShard indexShard();
Expand Down
Loading