-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Add random tests with match_only_text multi-field #132380
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
Changes from 19 commits
fc524df
0861dcc
f4ce957
63098b0
770ad5d
f96a1a8
560d937
934b764
5446ac4
789222b
be6da22
501f385
787f4ae
4c2843c
1760919
f136489
401c9e8
7715c83
5ce0810
0037c8f
cafc130
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 |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.datageneration.datasource; | ||
|
||
import org.elasticsearch.datageneration.FieldType; | ||
import org.elasticsearch.test.ESTestCase; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
public class MultifieldAddonHandler implements DataSourceHandler { | ||
|
||
private static final String PLACEHOLDER_NAME = "_an_improbable_placeholder_name"; | ||
private static final float DEFAULT_CHANCE_OF_CHILD_FIELD = 0.5f; | ||
private final Map<FieldType, List<FieldType>> subfieldTypes; | ||
private final float chanceOfChildField; | ||
|
||
private static final List<FieldType> STRING_TYPES = List.of( | ||
FieldType.TEXT, | ||
FieldType.KEYWORD, | ||
FieldType.MATCH_ONLY_TEXT, | ||
FieldType.WILDCARD | ||
); | ||
public static MultifieldAddonHandler STRING_TYPE_HANDLER = new MultifieldAddonHandler( | ||
STRING_TYPES.stream().collect(Collectors.toMap(t -> t, t -> STRING_TYPES.stream().filter(s -> s != t).toList())) | ||
); | ||
|
||
public MultifieldAddonHandler(Map<FieldType, List<FieldType>> subfieldTypes, float chanceOfChildField) { | ||
this.subfieldTypes = subfieldTypes; | ||
this.chanceOfChildField = chanceOfChildField; | ||
} | ||
|
||
public MultifieldAddonHandler(Map<FieldType, List<FieldType>> subfieldTypes) { | ||
this(subfieldTypes, DEFAULT_CHANCE_OF_CHILD_FIELD); | ||
} | ||
|
||
@Override | ||
public DataSourceResponse.LeafMappingParametersGenerator handle(DataSourceRequest.LeafMappingParametersGenerator request) { | ||
|
||
// Need to delegate creation of the same type of field to other handlers. So skip request | ||
// if it's for the placeholder name used when creating the child and parent fields. | ||
if (request.fieldName().equals(PLACEHOLDER_NAME)) { | ||
return null; | ||
} | ||
|
||
FieldType parentType = FieldType.tryParse(request.fieldType()); | ||
List<FieldType> childTypes = subfieldTypes.get(parentType); | ||
if (childTypes == null) { | ||
return null; | ||
} | ||
|
||
return new DataSourceResponse.LeafMappingParametersGenerator(() -> { | ||
assert parentType != null; | ||
var parent = getMappingForType(parentType, request); | ||
if (ESTestCase.randomFloat() > chanceOfChildField) { | ||
return parent; | ||
} | ||
|
||
var childType = ESTestCase.randomFrom(childTypes); | ||
var child = getChildMappingForType(childType, request); | ||
|
||
child.put("type", childType.toString()); | ||
String childName = "subfield_" + childType; | ||
parent.put("fields", Map.of(childName, child)); | ||
return parent; | ||
}); | ||
} | ||
|
||
private static Map<String, Object> getChildMappingForType(FieldType type, DataSourceRequest.LeafMappingParametersGenerator request) { | ||
Map<String, Object> mapping = getMappingForType(type, request); | ||
if (type == FieldType.KEYWORD) { | ||
mapping.remove("copy_to"); | ||
|
||
} | ||
return mapping; | ||
} | ||
|
||
private static Map<String, Object> getMappingForType(FieldType type, DataSourceRequest.LeafMappingParametersGenerator request) { | ||
return request.dataSource() | ||
.get( | ||
new DataSourceRequest.LeafMappingParametersGenerator( | ||
request.dataSource(), | ||
PLACEHOLDER_NAME, | ||
type.toString(), | ||
request.eligibleCopyToFields(), | ||
request.dynamicMapping() | ||
) | ||
) | ||
.mappingGenerator() | ||
.get(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.datageneration.fields.leaf; | ||
|
||
import org.elasticsearch.datageneration.FieldDataGenerator; | ||
import org.elasticsearch.datageneration.datasource.DataSource; | ||
|
||
import java.util.Map; | ||
|
||
public class MatchOnlyTextFieldDataGenerator implements FieldDataGenerator { | ||
private final FieldDataGenerator textGenerator; | ||
|
||
public MatchOnlyTextFieldDataGenerator(DataSource dataSource) { | ||
this.textGenerator = new TextFieldDataGenerator(dataSource); | ||
} | ||
|
||
@Override | ||
public Object generateValue(Map<String, Object> fieldMapping) { | ||
return textGenerator.generateValue(fieldMapping); | ||
} | ||
} |
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.
I think it's easy to implement a reserved prefix, you need to update
generateFieldName()
inDefaultObjectGenerationHandler
and that's it i believe. Should we do that?