Skip to content

Commit d4046b5

Browse files
committed
hack 'fields' support into flattened field type.
1 parent 5c148c0 commit d4046b5

File tree

4 files changed

+74
-12
lines changed

4 files changed

+74
-12
lines changed

server/src/main/java/org/elasticsearch/index/mapper/KeywordFieldMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1320,7 +1320,7 @@ protected void indexScriptValues(
13201320
this.fieldType().scriptValues.valuesForDoc(searchLookup, readerContext, doc, value -> indexValue(documentParserContext, value));
13211321
}
13221322

1323-
private boolean indexValue(DocumentParserContext context, String value) {
1323+
public boolean indexValue(DocumentParserContext context, String value) {
13241324
return indexValue(context, new Text(value));
13251325
}
13261326

server/src/main/java/org/elasticsearch/index/mapper/flattened/FlattenedFieldMapper.java

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@
3333
import org.apache.lucene.util.automaton.Automaton;
3434
import org.apache.lucene.util.automaton.CompiledAutomaton;
3535
import org.apache.lucene.util.automaton.Operations;
36+
import org.elasticsearch.cluster.metadata.IndexMetadata;
3637
import org.elasticsearch.common.Strings;
3738
import org.elasticsearch.common.lucene.Lucene;
3839
import org.elasticsearch.common.lucene.search.AutomatonQueries;
40+
import org.elasticsearch.common.settings.Settings;
3941
import org.elasticsearch.common.unit.Fuzziness;
4042
import org.elasticsearch.common.util.BigArrays;
4143
import org.elasticsearch.core.Nullable;
@@ -89,8 +91,10 @@
8991
import java.io.IOException;
9092
import java.io.UncheckedIOException;
9193
import java.util.ArrayList;
94+
import java.util.Arrays;
9295
import java.util.Collections;
9396
import java.util.HashMap;
97+
import java.util.Iterator;
9498
import java.util.List;
9599
import java.util.Map;
96100
import java.util.Objects;
@@ -285,7 +289,28 @@ public FlattenedFieldMapper build(MapperBuilderContext context) {
285289
nullValue.get(),
286290
context.isSourceSynthetic()
287291
);
288-
return new FlattenedFieldMapper(leafName(), ft, builderParams(this, context), this);
292+
293+
// HACK:
294+
Settings settings = Settings.builder()
295+
.put(IndexMetadata.SETTING_VERSION_CREATED, indexCreatedVersion)
296+
// .put(IndexSettings.MODE.getKey(), indexMode)
297+
.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1)
298+
.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1)
299+
.build();
300+
IndexSettings indexSettings = new IndexSettings(IndexMetadata.builder("bla").settings(settings).build(), settings);
301+
var hostName = new KeywordFieldMapper.Builder(leafName() + ".host.name", indexSettings).indexed(false)
302+
.docValues(true)
303+
.build(context);
304+
var serviceName = new KeywordFieldMapper.Builder(leafName() + ".service.name", indexSettings).indexed(false)
305+
.docValues(true)
306+
.build(context);
307+
// resource.attributes.log.file.path
308+
var logFilePath = new KeywordFieldMapper.Builder(leafName() + ".log.file.path", indexSettings).indexed(false)
309+
.docValues(true)
310+
.build(context);
311+
// END HACK
312+
313+
return new FlattenedFieldMapper(leafName(), ft, builderParams(this, context), this, hostName, serviceName, logFilePath);
289314
}
290315
}
291316

@@ -1095,10 +1120,23 @@ public void validateMatchedRoutingPath(final String routingPath) {
10951120

10961121
private final FlattenedFieldParser fieldParser;
10971122
private final Builder builder;
1098-
1099-
private FlattenedFieldMapper(String leafName, MappedFieldType mappedFieldType, BuilderParams builderParams, Builder builder) {
1123+
private final Map<String, KeywordFieldMapper> mappedFields;
1124+
1125+
private FlattenedFieldMapper(
1126+
String leafName,
1127+
MappedFieldType mappedFieldType,
1128+
BuilderParams builderParams,
1129+
Builder builder,
1130+
KeywordFieldMapper hostName,
1131+
KeywordFieldMapper serviceName,
1132+
KeywordFieldMapper logFilePath
1133+
) {
11001134
super(leafName, mappedFieldType, builderParams);
11011135
this.builder = builder;
1136+
this.mappedFields = new HashMap<>();
1137+
mappedFields.put(hostName.leafName().replace(leafName + ".", ""), hostName);
1138+
mappedFields.put(serviceName.leafName().replace(leafName + ".", ""), serviceName);
1139+
mappedFields.put(logFilePath.leafName().replace(leafName + ".", ""), logFilePath);
11021140
this.fieldParser = new FlattenedFieldParser(
11031141
mappedFieldType.name(),
11041142
mappedFieldType.name() + KEYED_FIELD_SUFFIX,
@@ -1108,7 +1146,8 @@ private FlattenedFieldMapper(String leafName, MappedFieldType mappedFieldType, B
11081146
builder.ignoreAbove.get(),
11091147
builder.nullValue.get(),
11101148
builder.usesBinaryDocValues,
1111-
builder.storeRoot.get()
1149+
builder.storeRoot.get(),
1150+
mappedFields
11121151
);
11131152
}
11141153

@@ -1174,6 +1213,7 @@ public FieldMapper.Builder getMergeBuilder() {
11741213
@Override
11751214
protected SyntheticSourceSupport syntheticSourceSupport() {
11761215
if (fieldType().hasDocValues()) {
1216+
// TODO: include mapped fields
11771217
return new SyntheticSourceSupport.Native(
11781218
() -> new FlattenedDocValuesSyntheticFieldLoader(
11791219
fullPath(),
@@ -1187,4 +1227,9 @@ protected SyntheticSourceSupport syntheticSourceSupport() {
11871227

11881228
return super.syntheticSourceSupport();
11891229
}
1230+
1231+
@Override
1232+
public Iterator<Mapper> iterator() {
1233+
return mappedFields.values().stream().map(m -> (Mapper) m).iterator();
1234+
}
11901235
}

server/src/main/java/org/elasticsearch/index/mapper/flattened/FlattenedFieldParser.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818
import org.elasticsearch.common.xcontent.XContentParserUtils;
1919
import org.elasticsearch.index.mapper.ContentPath;
2020
import org.elasticsearch.index.mapper.DocumentParserContext;
21+
import org.elasticsearch.index.mapper.KeywordFieldMapper;
2122
import org.elasticsearch.index.mapper.MappedFieldType;
2223
import org.elasticsearch.index.mapper.MultiValuedBinaryDocValuesField;
2324
import org.elasticsearch.xcontent.XContentParser;
2425

2526
import java.io.IOException;
27+
import java.util.Map;
2628

2729
/**
2830
* A helper class for {@link FlattenedFieldMapper} parses a JSON object
@@ -44,6 +46,8 @@ class FlattenedFieldParser {
4446
private final boolean usesBinaryDocValues;
4547
private final boolean storeRoot;
4648

49+
private final Map<String, KeywordFieldMapper> mappedFields;
50+
4751
FlattenedFieldParser(
4852
String rootFieldFullPath,
4953
String keyedFieldFullPath,
@@ -53,8 +57,8 @@ class FlattenedFieldParser {
5357
int ignoreAbove,
5458
String nullValue,
5559
boolean usesBinaryDocValues,
56-
boolean storeRoot
57-
) {
60+
boolean storeRoot,
61+
Map<String, KeywordFieldMapper> mappedFields) {
5862
this.rootFieldFullPath = rootFieldFullPath;
5963
this.keyedFieldFullPath = keyedFieldFullPath;
6064
this.keyedIgnoredValuesFieldFullPath = keyedIgnoredValuesFieldFullPath;
@@ -64,6 +68,7 @@ class FlattenedFieldParser {
6468
this.nullValue = nullValue;
6569
this.usesBinaryDocValues = usesBinaryDocValues;
6670
this.storeRoot = storeRoot;
71+
this.mappedFields = mappedFields;
6772
}
6873

6974
public void parse(final DocumentParserContext documentParserContext) throws IOException {
@@ -136,6 +141,12 @@ private void addField(Context context, ContentPath path, String currentName, Str
136141
);
137142
}
138143

144+
KeywordFieldMapper keywordFieldMapper = mappedFields.get(key);
145+
if (keywordFieldMapper != null) {
146+
keywordFieldMapper.indexValue(context.documentParserContext(), value);
147+
return;
148+
}
149+
139150
String keyedValue = createKeyedValue(key, value);
140151
BytesRef bytesKeyedValue = new BytesRef(keyedValue);
141152

server/src/test/java/org/elasticsearch/index/mapper/flattened/FlattenedFieldParserTests.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import java.io.IOException;
2828
import java.util.List;
29+
import java.util.Map;
2930

3031
public class FlattenedFieldParserTests extends ESTestCase {
3132
private FlattenedFieldParser parser;
@@ -42,7 +43,8 @@ public void setUp() throws Exception {
4243
Integer.MAX_VALUE,
4344
null,
4445
false,
45-
true
46+
true,
47+
Map.of()
4648
);
4749
}
4850

@@ -307,7 +309,8 @@ public void testDepthLimit() throws Exception {
307309
Integer.MAX_VALUE,
308310
null,
309311
false,
310-
true
312+
true,
313+
Map.of()
311314
);
312315

313316
TestDocumentParserContext context = new TestDocumentParserContext(xContentParser);
@@ -333,7 +336,8 @@ public void testDepthLimitBoundary() throws Exception {
333336
Integer.MAX_VALUE,
334337
null,
335338
false,
336-
true
339+
true,
340+
Map.of()
337341
);
338342

339343
TestDocumentParserContext context = new TestDocumentParserContext(xContentParser);
@@ -354,7 +358,8 @@ public void testIgnoreAbove() throws Exception {
354358
10,
355359
null,
356360
false,
357-
true
361+
true,
362+
Map.of()
358363
);
359364

360365
TestDocumentParserContext context = new TestDocumentParserContext(xContentParser);
@@ -381,7 +386,8 @@ public void testNullValues() throws Exception {
381386
Integer.MAX_VALUE,
382387
"placeholder",
383388
false,
384-
true
389+
true,
390+
Map.of()
385391
);
386392

387393
TestDocumentParserContext configuredContext = new TestDocumentParserContext(createXContentParser(input));

0 commit comments

Comments
 (0)