Skip to content

Commit

Permalink
Adding nested fields to the tests mapping.
Browse files Browse the repository at this point in the history
  • Loading branch information
afoucret committed Nov 8, 2024
1 parent 872314f commit c90c943
Showing 1 changed file with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@

package org.elasticsearch.xpack.kql.parser;

import org.elasticsearch.common.Strings;
import org.elasticsearch.common.compress.CompressedXContent;
import org.elasticsearch.common.regex.Regex;
import org.elasticsearch.core.Predicates;
import org.elasticsearch.core.SuppressForbidden;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.query.MatchPhraseQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
Expand All @@ -19,6 +22,7 @@
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.query.WildcardQueryBuilder;
import org.elasticsearch.test.AbstractBuilderTestCase;
import org.elasticsearch.xcontent.XContentBuilder;

import java.io.BufferedReader;
import java.io.IOException;
Expand All @@ -36,6 +40,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder;
import static org.hamcrest.Matchers.anEmptyMap;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
Expand All @@ -46,6 +51,43 @@ public abstract class AbstractKqlParserTestCase extends AbstractBuilderTestCase
protected static final String UNSUPPORTED_QUERY_FILE_PATH = "/unsupported-queries";
protected static final Predicate<String> BOOLEAN_QUERY_FILTER = (q) -> q.matches("(?i)[^{]*[^\\\\]*(NOT|AND|OR)[^}]*");

protected static final String NESTED_FIELD_NAME = "mapped_nested";

@Override
protected void initializeAdditionalMappings(MapperService mapperService) throws IOException {
XContentBuilder mapping = jsonBuilder().startObject().startObject("_doc").startObject("properties");

mapping.startObject(TEXT_FIELD_NAME).field("type", "text").endObject();
mapping.startObject(NESTED_FIELD_NAME);
{
mapping.field("type", "nested");
mapping.startObject("properties");
{
mapping.startObject(TEXT_FIELD_NAME).field("type", "text").endObject();
mapping.startObject(KEYWORD_FIELD_NAME).field("type", "keyword").endObject();
mapping.startObject(INT_FIELD_NAME).field("type", "integer").endObject();
mapping.startObject(NESTED_FIELD_NAME);
{
mapping.field("type", "nested");
mapping.startObject("properties");
{
mapping.startObject(TEXT_FIELD_NAME).field("type", "text").endObject();
mapping.startObject(KEYWORD_FIELD_NAME).field("type", "keyword").endObject();
mapping.startObject(INT_FIELD_NAME).field("type", "integer").endObject();
}
mapping.endObject();
}
mapping.endObject();
}
mapping.endObject();
}
mapping.endObject();

mapping.endObject().endObject().endObject();

mapperService.merge("_doc", new CompressedXContent(Strings.toString(mapping)), MapperService.MergeReason.MAPPING_UPDATE);
}

protected static String wrapWithRandomWhitespaces(String input) {
return String.join("", randomWhitespaces(), input, randomWhitespaces());
}
Expand Down Expand Up @@ -93,7 +135,18 @@ private static InputStream readFromJarUrl(URL source) throws IOException {
protected List<String> mappedLeafFields() {
return Stream.concat(
Arrays.stream(MAPPED_LEAF_FIELD_NAMES),
List.of(DATE_FIELD_NAME, INT_FIELD_NAME).stream().map(subfieldName -> OBJECT_FIELD_NAME + "." + subfieldName)
Stream.of(
// Adding mapped_object subfields
Strings.format("%s.%s", OBJECT_FIELD_NAME, INT_FIELD_NAME),
Strings.format("%s.%s", OBJECT_FIELD_NAME, DATE_FIELD_NAME),
// Adding mapped_nested subfields
Strings.format("%s.%s", NESTED_FIELD_NAME, TEXT_FIELD_NAME),
Strings.format("%s.%s", NESTED_FIELD_NAME, KEYWORD_FIELD_NAME),
Strings.format("%s.%s", NESTED_FIELD_NAME, INT_FIELD_NAME),
Strings.format("%s.%s.%s", NESTED_FIELD_NAME, NESTED_FIELD_NAME, TEXT_FIELD_NAME),
Strings.format("%s.%s.%s", NESTED_FIELD_NAME, NESTED_FIELD_NAME, KEYWORD_FIELD_NAME),
Strings.format("%s.%s.%s", NESTED_FIELD_NAME, NESTED_FIELD_NAME, INT_FIELD_NAME)
)
).toList();
}

Expand Down

0 comments on commit c90c943

Please sign in to comment.