Skip to content

Commit 560d937

Browse files
committed
Use text matcher for match_only_text
1 parent f96a1a8 commit 560d937

File tree

2 files changed

+18
-63
lines changed

2 files changed

+18
-63
lines changed

test/framework/src/main/java/org/elasticsearch/datageneration/matchers/source/FieldSpecificMatcher.java

Lines changed: 16 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -604,13 +604,13 @@ public MatchResult match(
604604
}
605605
}
606606

607-
class MatchOnlyTextMatcher implements FieldSpecificMatcher {
607+
class TextMatcher implements FieldSpecificMatcher {
608608
private final XContentBuilder actualMappings;
609609
private final Settings.Builder actualSettings;
610610
private final XContentBuilder expectedMappings;
611611
private final Settings.Builder expectedSettings;
612612

613-
MatchOnlyTextMatcher(
613+
TextMatcher(
614614
XContentBuilder actualMappings,
615615
Settings.Builder actualSettings,
616616
XContentBuilder expectedMappings,
@@ -622,6 +622,10 @@ class MatchOnlyTextMatcher implements FieldSpecificMatcher {
622622
this.expectedSettings = expectedSettings;
623623
}
624624

625+
public String type() {
626+
return "text";
627+
}
628+
625629
@Override
626630
@SuppressWarnings("unchecked")
627631
public MatchResult match(
@@ -633,11 +637,13 @@ public MatchResult match(
633637
var expectedNormalized = normalize(expected);
634638
var actualNormalized = normalize(actual);
635639

636-
// Match simply as match_only_text first.
640+
// Match simply as text first.
637641
if (actualNormalized.equals(expectedNormalized)) {
638642
return MatchResult.match();
639643
}
640-
//
644+
645+
// In some cases synthetic source for text fields is synthesized using the keyword multi field.
646+
// So in this case it's appropriate to match it using keyword matching logic (mainly to cover `null_value`).
641647
var multiFields = (Map<String, Object>) getMappingParameter("fields", actualMapping, expectedMapping);
642648
if (multiFields != null) {
643649
var keywordMatcher = new KeywordMatcher(actualMappings, actualSettings, expectedMappings, expectedSettings);
@@ -655,7 +661,7 @@ public MatchResult match(
655661
actualSettings,
656662
expectedMappings,
657663
expectedSettings,
658-
"Values of type [match_only_text] don't match, " + prettyPrintCollections(actual, expected)
664+
"Values of type [" + type() + "] don't match, " + prettyPrintCollections(actual, expected)
659665
)
660666
);
661667
}
@@ -669,70 +675,19 @@ private Set<Object> normalize(List<Object> values) {
669675
}
670676
}
671677

672-
class TextMatcher implements FieldSpecificMatcher {
673-
private final XContentBuilder actualMappings;
674-
private final Settings.Builder actualSettings;
675-
private final XContentBuilder expectedMappings;
676-
private final Settings.Builder expectedSettings;
677-
678-
TextMatcher(
678+
class MatchOnlyTextMatcher extends TextMatcher {
679+
MatchOnlyTextMatcher(
679680
XContentBuilder actualMappings,
680681
Settings.Builder actualSettings,
681682
XContentBuilder expectedMappings,
682683
Settings.Builder expectedSettings
683684
) {
684-
this.actualMappings = actualMappings;
685-
this.actualSettings = actualSettings;
686-
this.expectedMappings = expectedMappings;
687-
this.expectedSettings = expectedSettings;
685+
super(actualMappings, actualSettings, expectedMappings, expectedSettings);
688686
}
689687

690688
@Override
691-
@SuppressWarnings("unchecked")
692-
public MatchResult match(
693-
List<Object> actual,
694-
List<Object> expected,
695-
Map<String, Object> actualMapping,
696-
Map<String, Object> expectedMapping
697-
) {
698-
var expectedNormalized = normalize(expected);
699-
var actualNormalized = normalize(actual);
700-
701-
// Match simply as text first.
702-
if (actualNormalized.equals(expectedNormalized)) {
703-
return MatchResult.match();
704-
}
705-
706-
// In some cases synthetic source for text fields is synthesized using the keyword multi field.
707-
// So in this case it's appropriate to match it using keyword matching logic (mainly to cover `null_value`).
708-
var multiFields = (Map<String, Object>) getMappingParameter("fields", actualMapping, expectedMapping);
709-
if (multiFields != null) {
710-
var keywordMatcher = new KeywordMatcher(actualMappings, actualSettings, expectedMappings, expectedSettings);
711-
712-
var keywordFieldMapping = (Map<String, Object>) multiFields.get("subfield_keyword");
713-
var keywordMatchResult = keywordMatcher.match(actual, expected, keywordFieldMapping, keywordFieldMapping);
714-
if (keywordMatchResult.isMatch()) {
715-
return MatchResult.match();
716-
}
717-
}
718-
719-
return MatchResult.noMatch(
720-
formatErrorMessage(
721-
actualMappings,
722-
actualSettings,
723-
expectedMappings,
724-
expectedSettings,
725-
"Values of type [text] don't match, " + prettyPrintCollections(actual, expected)
726-
)
727-
);
728-
}
729-
730-
private Set<Object> normalize(List<Object> values) {
731-
if (values == null) {
732-
return Set.of();
733-
}
734-
735-
return values.stream().filter(Objects::nonNull).collect(Collectors.toSet());
689+
public String type() {
690+
return "match_only_text";
736691
}
737692
}
738693

test/framework/src/main/java/org/elasticsearch/datageneration/queries/LeafQueryGenerator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public interface LeafQueryGenerator {
2525
* @param type the type to build a query for
2626
* @return a generator that can build queries for this type
2727
*/
28-
static LeafQueryGenerator buildForType(String type, MappingPredicates mappingContextHelper) {
28+
static LeafQueryGenerator buildForType(String type, MappingPredicates mappingPredicates) {
2929
LeafQueryGenerator noQueries = (Map<String, Object> fieldMapping, String path, Object value) -> List.of();
3030

3131
FieldType fieldType = FieldType.tryParse(type);
@@ -37,7 +37,7 @@ static LeafQueryGenerator buildForType(String type, MappingPredicates mappingCon
3737
case KEYWORD -> new KeywordQueryGenerator();
3838
case WILDCARD -> new WildcardQueryGenerator();
3939
case TEXT -> new TextQueryGenerator();
40-
case MATCH_ONLY_TEXT -> new MatchOnlyTextQueryGenerator(mappingContextHelper);
40+
case MATCH_ONLY_TEXT -> new MatchOnlyTextQueryGenerator(mappingPredicates);
4141
default -> noQueries;
4242
};
4343
}

0 commit comments

Comments
 (0)