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

Extend support of allowedFields to getMatchingFieldNames and getAllFields #106862

Merged
merged 3 commits into from
Mar 28, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/changelog/106862.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 106862
summary: Extend support of `allowedFields` to `getMatchingFieldNames` and `getAllFields`
area: "Mapping"
type: bug
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.function.BooleanSupplier;
import java.util.function.LongSupplier;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
* Context object used to rewrite {@link QueryBuilder} instances into simplified version.
Expand Down Expand Up @@ -318,35 +319,45 @@ public boolean indexMatches(String pattern) {
* @param pattern the field name pattern
*/
public Set<String> getMatchingFieldNames(String pattern) {
Set<String> matches;
if (runtimeMappings.isEmpty()) {
return mappingLookup.getMatchingFieldNames(pattern);
}
Set<String> matches = new HashSet<>(mappingLookup.getMatchingFieldNames(pattern));
if ("*".equals(pattern)) {
matches.addAll(runtimeMappings.keySet());
} else if (Regex.isSimpleMatchPattern(pattern) == false) {
// no wildcard
if (runtimeMappings.containsKey(pattern)) {
matches.add(pattern);
}
matches = mappingLookup.getMatchingFieldNames(pattern);
} else {
for (String name : runtimeMappings.keySet()) {
if (Regex.simpleMatch(pattern, name)) {
matches.add(name);
matches = new HashSet<>(mappingLookup.getMatchingFieldNames(pattern));
if ("*".equals(pattern)) {
matches.addAll(runtimeMappings.keySet());
} else if (Regex.isSimpleMatchPattern(pattern) == false) {
// no wildcard
if (runtimeMappings.containsKey(pattern)) {
matches.add(pattern);
}
} else {
for (String name : runtimeMappings.keySet()) {
if (Regex.simpleMatch(pattern, name)) {
matches.add(name);
}
}
}
}
return matches;
// If the field is not allowed, behave as if it is not mapped
return allowedFields == null ? matches : matches.stream().filter(allowedFields).collect(Collectors.toSet());
Copy link
Contributor

@salvatore-campagna salvatore-campagna Mar 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see here and below we always need to check for allowedFields to be null or not to decide if we need to filter one or more fields. I am wondering if we should require it to be not null and force the caller to use an identity predicate lambda (fieldName -> true) in case no field is filtered out.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not too sure what is best here. Skip the filtering entirely when the predicate is null, or always filter although the predicate always returns true? I went for the former, which leaves things as close as they are outside of api keys queries (when setAllowedFields is not called).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that, maybe, performance-wise not applying the filter at all might be better if there is nothing to filter out...which is the reason why I said it looks good to me...it is just that all those null checks are annoying for me.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand correctly this is just the same we were doing before + an additional filter on allowed fields based n field names...maybe we can just extract the existing method into something like a private getMatchingFieldNamesInternal to which we apply the filter?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the gain compared to a single method?

}

/**
* @return An {@link Iterable} with key the field name and value the MappedFieldType
*/
public Iterable<Map.Entry<String, MappedFieldType>> getAllFields() {
var allFromMapping = mappingLookup.getFullNameToFieldType();
// runtime mappings and non-runtime fields don't overlap, so we can simply concatenate the iterables here
return runtimeMappings.isEmpty()
Map<String, MappedFieldType> allFromMapping = mappingLookup.getFullNameToFieldType();
Set<Map.Entry<String, MappedFieldType>> allEntrySet = allowedFields == null
? allFromMapping.entrySet()
: () -> Iterators.concat(allFromMapping.entrySet().iterator(), runtimeMappings.entrySet().iterator());
: allFromMapping.entrySet().stream().filter(entry -> allowedFields.test(entry.getKey())).collect(Collectors.toSet());
if (runtimeMappings.isEmpty()) {
return allEntrySet;
}
Set<Map.Entry<String, MappedFieldType>> runtimeEntrySet = allowedFields == null
? runtimeMappings.entrySet()
: runtimeMappings.entrySet().stream().filter(entry -> allowedFields.test(entry.getKey())).collect(Collectors.toSet());
// runtime mappings and non-runtime fields don't overlap, so we can simply concatenate the iterables here
return () -> Iterators.concat(allEntrySet.iterator(), runtimeEntrySet.iterator());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
import java.util.stream.Collectors;

import static java.util.Collections.singletonMap;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.notNullValue;
Expand Down Expand Up @@ -401,6 +402,84 @@ public void testSyntheticSourceSearchLookup() throws IOException {
assertEquals("meow", source.source().get("cat"));
}

public void testAllowedFields() {
Map<String, Object> runtimeMappings = Map.ofEntries(
Map.entry("runtimecat", Map.of("type", "keyword")),
Map.entry("runtimedog", Map.of("type", "long"))
);
SearchExecutionContext context = createSearchExecutionContext(
"uuid",
null,
SearchExecutionContextTests.createMappingLookup(
List.of(new MockFieldMapper.FakeFieldType("pig"), new MockFieldMapper.FakeFieldType("cat")),
List.of(new TestRuntimeField("runtime", "long"))
),
runtimeMappings
);

assertNotNull(context.getFieldType("pig"));
assertNotNull(context.getFieldType("cat"));
assertNotNull(context.getFieldType("runtimecat"));
assertNotNull(context.getFieldType("runtimedog"));
assertNotNull(context.getFieldType("runtime"));
assertEquals(3, context.getMatchingFieldNames("runtime*").size());
assertEquals(2, context.getMatchingFieldNames("*cat").size());
assertThat(getFieldNames(context.getAllFields()), containsInAnyOrder("pig", "cat", "runtimecat", "runtimedog", "runtime"));

context.setAllowedFields(s -> true);
assertNotNull(context.getFieldType("pig"));
assertTrue(context.isFieldMapped("pig"));
assertNotNull(context.getFieldType("cat"));
assertTrue(context.isFieldMapped("cat"));
assertNotNull(context.getFieldType("runtimecat"));
assertTrue(context.isFieldMapped("runtimecat"));
assertNotNull(context.getFieldType("runtimedog"));
assertTrue(context.isFieldMapped("runtimedog"));
assertNotNull(context.getFieldType("runtime"));
assertTrue(context.isFieldMapped("runtime"));
assertEquals(3, context.getMatchingFieldNames("runtime*").size());
assertEquals(2, context.getMatchingFieldNames("*cat").size());
assertThat(getFieldNames(context.getAllFields()), containsInAnyOrder("pig", "cat", "runtimecat", "runtimedog", "runtime"));

context.setAllowedFields(s -> s.equals("cat"));
assertNull(context.getFieldType("pig"));
assertFalse(context.isFieldMapped("pig"));
assertNotNull(context.getFieldType("cat"));
assertTrue(context.isFieldMapped("cat"));
assertNull(context.getFieldType("runtimecat"));
assertFalse(context.isFieldMapped("runtimecat"));
assertNull(context.getFieldType("runtimedog"));
assertFalse(context.isFieldMapped("runtimedog"));
assertNull(context.getFieldType("runtime"));
assertFalse(context.isFieldMapped("runtime"));
assertEquals(0, context.getMatchingFieldNames("runtime*").size());
assertEquals(1, context.getMatchingFieldNames("*cat").size());
assertThat(getFieldNames(context.getAllFields()), containsInAnyOrder("cat"));

context.setAllowedFields(s -> s.contains("dog") == false);
assertNotNull(context.getFieldType("pig"));
assertTrue(context.isFieldMapped("pig"));
assertNotNull(context.getFieldType("cat"));
assertTrue(context.isFieldMapped("cat"));
assertNotNull(context.getFieldType("runtimecat"));
assertTrue(context.isFieldMapped("runtimecat"));
assertNull(context.getFieldType("runtimedog"));
assertFalse(context.isFieldMapped("runtimedog"));
assertNotNull(context.getFieldType("runtime"));
assertTrue(context.isFieldMapped("runtime"));
assertEquals(2, context.getMatchingFieldNames("runtime*").size());
assertEquals(2, context.getMatchingFieldNames("*cat").size());
assertThat(getFieldNames(context.getAllFields()), containsInAnyOrder("pig", "cat", "runtimecat", "runtime"));
}

private static List<String> getFieldNames(Iterable<Map.Entry<String, MappedFieldType>> fields) {
List<String> fieldNames = new ArrayList<>();
for (Map.Entry<String, MappedFieldType> field : fields) {
fieldNames.add(field.getKey());
}
return fieldNames;
}

public static SearchExecutionContext createSearchExecutionContext(String indexUuid, String clusterAlias) {
return createSearchExecutionContext(indexUuid, clusterAlias, MappingLookup.EMPTY, Map.of());
}
Expand Down