-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
WIP: Support unmapped fields in search 'fields' option #64651
Changes from 2 commits
37265db
33e1a8b
9f39f91
f50bdc2
c94b191
9d6fd43
976fde1
822fb02
9511279
7ec4341
4d74f2d
4b5232b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
|
||
import org.apache.lucene.index.LeafReaderContext; | ||
import org.elasticsearch.common.document.DocumentField; | ||
import org.elasticsearch.common.xcontent.support.XContentMapValues; | ||
import org.elasticsearch.index.mapper.MappedFieldType; | ||
import org.elasticsearch.index.mapper.ValueFetcher; | ||
import org.elasticsearch.index.query.QueryShardContext; | ||
|
@@ -30,10 +31,12 @@ | |
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* A helper class to {@link FetchFieldsPhase} that's initialized with a list of field patterns to fetch. | ||
|
@@ -42,32 +45,53 @@ | |
public class FieldFetcher { | ||
public static FieldFetcher create(QueryShardContext context, | ||
SearchLookup searchLookup, | ||
Collection<FieldAndFormat> fieldAndFormats) { | ||
Collection<FieldAndFormat> fieldAndFormats, | ||
boolean includeUnmapped) { | ||
|
||
List<FieldContext> fieldContexts = new ArrayList<>(); | ||
List<String> originalPattern = new ArrayList<>(); | ||
List<String> excludeForUnmappedFetch = new ArrayList<>(); | ||
|
||
for (FieldAndFormat fieldAndFormat : fieldAndFormats) { | ||
String fieldPattern = fieldAndFormat.field; | ||
String format = fieldAndFormat.format; | ||
|
||
Collection<String> concreteFields = context.simpleMatchToIndexNames(fieldPattern); | ||
originalPattern.add(fieldAndFormat.field); | ||
for (String field : concreteFields) { | ||
MappedFieldType ft = context.getFieldType(field); | ||
if (ft == null || context.isMetadataField(field)) { | ||
continue; | ||
} | ||
ValueFetcher valueFetcher = ft.valueFetcher(context, searchLookup, format); | ||
excludeForUnmappedFetch.add(field); | ||
fieldContexts.add(new FieldContext(field, valueFetcher)); | ||
} | ||
if (fieldPattern.charAt(fieldPattern.length() - 1) != '*') { | ||
// not a prefix pattern, exclude potential sub-fields when fetching unmapped fields | ||
excludeForUnmappedFetch.add(fieldPattern + ".*"); | ||
} | ||
} | ||
Function<Map<String, ?>, Map<String, Object>> filter = XContentMapValues.filter( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Re-using some source filtering logic seems to be a good fit. A couple ideas:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the feedback, I'll need to better understand at how XContentMapValues#filter works to see if and how this could work. |
||
originalPattern.toArray(new String[originalPattern.size()]), | ||
excludeForUnmappedFetch.toArray(new String[excludeForUnmappedFetch.size()]) | ||
); | ||
|
||
return new FieldFetcher(fieldContexts); | ||
return new FieldFetcher(fieldContexts, includeUnmapped, filter); | ||
} | ||
|
||
private final List<FieldContext> fieldContexts; | ||
|
||
private FieldFetcher(List<FieldContext> fieldContexts) { | ||
private final boolean includeUnmapped; | ||
private final Function<Map<String, ?>, Map<String, Object>> filter; | ||
|
||
private FieldFetcher( | ||
List<FieldContext> fieldContexts, | ||
boolean includeUnmapped, | ||
Function<Map<String, ?>, Map<String, Object>> filter | ||
) { | ||
this.fieldContexts = fieldContexts; | ||
this.includeUnmapped = includeUnmapped; | ||
this.filter = filter; | ||
} | ||
|
||
public Map<String, DocumentField> fetch(SourceLookup sourceLookup, Set<String> ignoredFields) throws IOException { | ||
|
@@ -85,9 +109,37 @@ public Map<String, DocumentField> fetch(SourceLookup sourceLookup, Set<String> i | |
documentFields.put(field, new DocumentField(field, parsedValues)); | ||
} | ||
} | ||
if (includeUnmapped) { | ||
Map<String, Object> unmappedFieldsToAdd = filter.apply(sourceLookup.loadSourceIfNeeded()); | ||
collectLeafValues(unmappedFieldsToAdd, documentFields); | ||
} | ||
return documentFields; | ||
} | ||
|
||
static void collectLeafValues(Map<String, Object> map, Map<String, DocumentField> documentFields) { | ||
collectAllPaths("", map, documentFields); | ||
} | ||
|
||
private static void collectAllPaths(String prefix, Map<String, Object> source, Map<String, DocumentField> documentFields) { | ||
for (String key : source.keySet()) { | ||
Object value = source.get(key); | ||
String currentPath = prefix + key; | ||
if (value instanceof Map) { | ||
collectAllPaths(currentPath + ".", (Map<String, Object>) value, documentFields); | ||
} else { | ||
DocumentField f; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this might miss lists of objects? Looking at There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will take a look but though I have a test for this (essentially array values?) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, do you mean walking e.g. Lists of objects to their leaf values? e.g.
How does the current "fields" lookup work for this, e.g. what does the path look like for the "f1" value= "foo.1.f1" ? Maybe I'm just confused here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry for the confusion, I read the logic too quickly before. We won't miss anything, but the result structure could be surprising. For mapped fields, the 'fields' option always flattens arrays of objects. For example, given a document like
a request for
With this logic, for unmapped fields we'll return
Perhaps this behavior is inconsistent, I think we'll still want to flatten arrays of objects (especially if we already flatten objects when they're not in array?) I'm definitely up for discussing this though, it's a question we noted on the original issue. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand now and totally agree. I think I ran into this working on improvements on Friday anyway, should be part of my next update here. |
||
if (value instanceof List) { | ||
f = new DocumentField(currentPath, (List) value); | ||
} else { | ||
f = new DocumentField(currentPath, Collections.singletonList(value)); | ||
} | ||
if (f.getValue() != null) { | ||
documentFields.put(currentPath, f); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public void setNextReader(LeafReaderContext readerContext) { | ||
for (FieldContext field : fieldContexts) { | ||
field.valueFetcher.setNextReader(readerContext); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something to think about: if we end up making this configurable through a flag like
include_unmapped
, we could introduce a top-level parameter as we do here, or instead support the flag alongside each field pattern (so it'd be part ofFieldAndFormat
).