Skip to content

Commit

Permalink
Migrate LuceneDocument.getFields(String) to a List (#94830)
Browse files Browse the repository at this point in the history
  • Loading branch information
thecoop authored Mar 29, 2023
1 parent 1a14b0e commit 56d53da
Show file tree
Hide file tree
Showing 62 changed files with 860 additions and 861 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void testPostParse() throws IOException {
}));

ParsedDocument doc = docMapper.parse(source(b -> b.field("@timestamp", "2020-12-12")));
assertThat(doc.rootDoc().getFields("@timestamp").length, equalTo(1));
assertThat(doc.rootDoc().getFields("@timestamp").size(), equalTo(1));

Exception e = expectThrows(MapperException.class, () -> docMapper.parse(source(b -> b.field("@timestamp1", "2020-12-12"))));
assertThat(e.getCause().getMessage(), equalTo("data stream timestamp field [@timestamp] is missing"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -639,8 +639,8 @@ public void testGeoShapeArrayParsing() throws Exception {
b.endArray();
}));
assertThat(document.docs(), hasSize(1));
IndexableField[] fields = document.docs().get(0).getFields("field");
assertThat(fields.length, equalTo(2));
List<IndexableField> fields = document.docs().get(0).getFields("field");
assertThat(fields.size(), equalTo(2));
assertFieldWarnings("tree", "strategy");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ public void testDefaults() throws IOException {
assertEquals(Strings.toString(fieldMapping(this::minimalMapping)), mapper.mappingSource().toString());

ParsedDocument doc = mapper.parse(source(b -> b.field("field", "1234")));
IndexableField[] fields = doc.rootDoc().getFields("field");
assertEquals(1, fields.length);
assertEquals("1234", fields[0].stringValue());
IndexableFieldType fieldType = fields[0].fieldType();
List<IndexableField> fields = doc.rootDoc().getFields("field");
assertEquals(1, fields.size());
assertEquals("1234", fields.get(0).stringValue());
IndexableFieldType fieldType = fields.get(0).fieldType();
assertThat(fieldType.omitNorms(), equalTo(true));
assertTrue(fieldType.tokenized());
assertFalse(fieldType.stored());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ public void testDefaults() throws Exception {
assertEquals(Strings.toString(fieldMapping(this::minimalMapping)), mapper.mappingSource().toString());

ParsedDocument doc1 = mapper.parse(source(b -> b.field("field", 10)));
IndexableField[] fields = doc1.rootDoc().getFields("_feature");
assertEquals(1, fields.length);
assertThat(fields[0], instanceOf(FeatureField.class));
FeatureField featureField1 = (FeatureField) fields[0];
List<IndexableField> fields = doc1.rootDoc().getFields("_feature");
assertEquals(1, fields.size());
assertThat(fields.get(0), instanceOf(FeatureField.class));
FeatureField featureField1 = (FeatureField) fields.get(0);

ParsedDocument doc2 = mapper.parse(source(b -> b.field("field", 12)));
FeatureField featureField2 = (FeatureField) doc2.rootDoc().getFields("_feature")[0];
FeatureField featureField2 = (FeatureField) doc2.rootDoc().getFields("_feature").get(0);

int freq1 = getFrequency(featureField1.tokenStream(null, null));
int freq2 = getFrequency(featureField2.tokenStream(null, null));
Expand All @@ -112,13 +112,13 @@ public void testNegativeScoreImpact() throws Exception {
);

ParsedDocument doc1 = mapper.parse(source(b -> b.field("field", 10)));
IndexableField[] fields = doc1.rootDoc().getFields("_feature");
assertEquals(1, fields.length);
assertThat(fields[0], instanceOf(FeatureField.class));
FeatureField featureField1 = (FeatureField) fields[0];
List<IndexableField> fields = doc1.rootDoc().getFields("_feature");
assertEquals(1, fields.size());
assertThat(fields.get(0), instanceOf(FeatureField.class));
FeatureField featureField1 = (FeatureField) fields.get(0);

ParsedDocument doc2 = mapper.parse(source(b -> b.field("field", 12)));
FeatureField featureField2 = (FeatureField) doc2.rootDoc().getFields("_feature")[0];
FeatureField featureField2 = (FeatureField) doc2.rootDoc().getFields("_feature").get(0);

int freq1 = getFrequency(featureField1.tokenStream(null, null));
int freq2 = getFrequency(featureField2.tokenStream(null, null));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ public void testDefaults() throws Exception {

ParsedDocument doc1 = mapper.parse(source(this::writeField));

IndexableField[] fields = doc1.rootDoc().getFields("field");
assertEquals(2, fields.length);
assertThat(fields[0], Matchers.instanceOf(FeatureField.class));
List<IndexableField> fields = doc1.rootDoc().getFields("field");
assertEquals(2, fields.size());
assertThat(fields.get(0), Matchers.instanceOf(FeatureField.class));
FeatureField featureField1 = null;
FeatureField featureField2 = null;
for (IndexableField field : fields) {
Expand All @@ -106,9 +106,9 @@ public void testNegativeScoreImpact() throws Exception {

ParsedDocument doc1 = mapper.parse(source(this::writeField));

IndexableField[] fields = doc1.rootDoc().getFields("field");
assertEquals(2, fields.length);
assertThat(fields[0], Matchers.instanceOf(FeatureField.class));
List<IndexableField> fields = doc1.rootDoc().getFields("field");
assertEquals(2, fields.size());
assertThat(fields.get(0), Matchers.instanceOf(FeatureField.class));
FeatureField featureField1 = null;
FeatureField featureField2 = null;
for (IndexableField field : fields) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

import static java.util.Collections.singletonList;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;

Expand Down Expand Up @@ -93,9 +94,9 @@ public void testDefaults() throws Exception {
assertEquals(Strings.toString(mapping), mapper.mappingSource().toString());

ParsedDocument doc = mapper.parse(source(b -> b.field("field", 123)));
IndexableField[] fields = doc.rootDoc().getFields("field");
assertEquals(1, fields.length);
assertEquals("LongField <field:1230>", fields[0].toString());
List<IndexableField> fields = doc.rootDoc().getFields("field");
assertEquals(1, fields.size());
assertEquals("LongField <field:1230>", fields.get(0).toString());
}

public void testMissingScalingFactor() {
Expand Down Expand Up @@ -127,9 +128,9 @@ public void testNotIndexed() throws Exception {
)
);

IndexableField[] fields = doc.rootDoc().getFields("field");
assertEquals(1, fields.length);
IndexableField dvField = fields[0];
List<IndexableField> fields = doc.rootDoc().getFields("field");
assertEquals(1, fields.size());
IndexableField dvField = fields.get(0);
assertEquals(DocValuesType.SORTED_NUMERIC, dvField.fieldType().docValuesType());
assertEquals(1230, dvField.numericValue().longValue());
}
Expand All @@ -147,9 +148,9 @@ public void testNoDocValues() throws Exception {
)
);

IndexableField[] fields = doc.rootDoc().getFields("field");
assertEquals(1, fields.length);
IndexableField pointField = fields[0];
List<IndexableField> fields = doc.rootDoc().getFields("field");
assertEquals(1, fields.size());
IndexableField pointField = fields.get(0);
assertEquals(1, pointField.fieldType().pointDimensionCount());
assertEquals(1230, pointField.numericValue().longValue());
}
Expand All @@ -167,10 +168,10 @@ public void testStore() throws Exception {
)
);

IndexableField[] fields = doc.rootDoc().getFields("field");
assertEquals(2, fields.length);
assertEquals("LongField <field:1230>", fields[0].toString());
IndexableField storedField = fields[1];
List<IndexableField> fields = doc.rootDoc().getFields("field");
assertEquals(2, fields.size());
assertEquals("LongField <field:1230>", fields.get(0).toString());
IndexableField storedField = fields.get(1);
assertTrue(storedField.fieldType().stored());
assertEquals(1230, storedField.numericValue().longValue());
}
Expand All @@ -184,9 +185,9 @@ public void testCoerce() throws Exception {
XContentType.JSON
)
);
IndexableField[] fields = doc.rootDoc().getFields("field");
assertEquals(1, fields.length);
assertEquals("LongField <field:1230>", fields[0].toString());
List<IndexableField> fields = doc.rootDoc().getFields("field");
assertEquals(1, fields.size());
assertEquals("LongField <field:1230>", fields.get(0).toString());

DocumentMapper mapper2 = createDocumentMapper(
fieldMapping(b -> b.field("type", "scaled_float").field("scaling_factor", 10.0).field("coerce", false))
Expand Down Expand Up @@ -226,7 +227,7 @@ public void testNullValue() throws IOException {
XContentType.JSON
)
);
assertArrayEquals(new IndexableField[0], doc.rootDoc().getFields("field"));
assertThat(doc.rootDoc().getFields("field"), empty());

mapper = createDocumentMapper(
fieldMapping(b -> b.field("type", "scaled_float").field("scaling_factor", 10.0).field("null_value", 2.5))
Expand All @@ -238,9 +239,9 @@ public void testNullValue() throws IOException {
XContentType.JSON
)
);
IndexableField[] fields = doc.rootDoc().getFields("field");
assertEquals(1, fields.length);
assertEquals("LongField <field:25>", fields[0].toString());
List<IndexableField> fields = doc.rootDoc().getFields("field");
assertEquals(1, fields.size());
assertEquals("LongField <field:25>", fields.get(0).toString());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -157,9 +156,9 @@ public void testIndexing() throws IOException {
DocumentMapper mapper = createDocumentMapper(fieldMapping(this::minimalMapping));
ParsedDocument doc = mapper.parse(source(b -> b.field("field", "new york city")));
for (String field : new String[] { "field", "field._index_prefix", "field._2gram", "field._3gram" }) {
IndexableField[] fields = doc.rootDoc().getFields(field);
assertEquals(1, fields.length);
assertEquals("new york city", fields[0].stringValue());
List<IndexableField> fields = doc.rootDoc().getFields(field);
assertEquals(1, fields.size());
assertEquals("new york city", fields.get(0).stringValue());
}
}

Expand Down Expand Up @@ -297,9 +296,9 @@ private void assertMultiField(int shingleSize, MapperService mapperService, Stri

ParsedDocument doc = mapperService.documentMapper().parse(source(b -> b.field("field", "new york city")));
for (String field : fields) {
IndexableField[] indexFields = doc.rootDoc().getFields(field);
assertEquals(1, indexFields.length);
assertEquals("new york city", indexFields[0].stringValue());
List<IndexableField> indexFields = doc.rootDoc().getFields(field);
assertEquals(1, indexFields.size());
assertEquals("new york city", indexFields.get(0).stringValue());
}
}

Expand Down Expand Up @@ -665,12 +664,12 @@ private void documentParsingTestCase(Collection<String> values) throws IOExcepti
}
}));

IndexableField[] rootFields = parsedDocument.rootDoc().getFields("field");
IndexableField[] prefixFields = parsedDocument.rootDoc().getFields("field._index_prefix");
IndexableField[] shingle2Fields = parsedDocument.rootDoc().getFields("field._2gram");
IndexableField[] shingle3Fields = parsedDocument.rootDoc().getFields("field._3gram");
for (IndexableField[] fields : new IndexableField[][] { rootFields, prefixFields, shingle2Fields, shingle3Fields }) {
Set<String> expectedValues = Arrays.stream(fields).map(IndexableField::stringValue).collect(Collectors.toSet());
List<IndexableField> rootFields = parsedDocument.rootDoc().getFields("field");
List<IndexableField> prefixFields = parsedDocument.rootDoc().getFields("field._index_prefix");
List<IndexableField> shingle2Fields = parsedDocument.rootDoc().getFields("field._2gram");
List<IndexableField> shingle3Fields = parsedDocument.rootDoc().getFields("field._3gram");
for (List<IndexableField> fields : List.of(rootFields, prefixFields, shingle2Fields, shingle3Fields)) {
Set<String> expectedValues = fields.stream().map(IndexableField::stringValue).collect(Collectors.toSet());
assertThat(values, equalTo(expectedValues));
}
}
Expand Down
Loading

0 comments on commit 56d53da

Please sign in to comment.