|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.index.mapper; |
| 10 | + |
| 11 | +import org.apache.lucene.document.Document; |
| 12 | +import org.apache.lucene.index.DirectoryReader; |
| 13 | +import org.apache.lucene.index.IndexReader; |
| 14 | +import org.apache.lucene.index.IndexWriter; |
| 15 | +import org.apache.lucene.index.IndexWriterConfig; |
| 16 | +import org.apache.lucene.index.LeafReaderContext; |
| 17 | +import org.apache.lucene.store.Directory; |
| 18 | +import org.opensearch.common.lucene.Lucene; |
| 19 | +import org.opensearch.core.index.Index; |
| 20 | +import org.opensearch.index.query.QueryShardContext; |
| 21 | +import org.opensearch.search.lookup.SourceLookup; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.HashMap; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | + |
| 29 | +import static org.mockito.Mockito.when; |
| 30 | + |
| 31 | +public class FieldTypeInferenceTests extends MapperServiceTestCase { |
| 32 | + |
| 33 | + private static final Map<String, List<Object>> documentMap; |
| 34 | + static { |
| 35 | + List<Object> listWithNull = new ArrayList<>(); |
| 36 | + listWithNull.add(null); |
| 37 | + documentMap = new HashMap<>(); |
| 38 | + documentMap.put("text_field", List.of("The quick brown fox jumps over the lazy dog.")); |
| 39 | + documentMap.put("int_field", List.of(789)); |
| 40 | + documentMap.put("float_field", List.of(123.45)); |
| 41 | + documentMap.put("date_field_1", List.of("2024-05-12T15:45:00Z")); |
| 42 | + documentMap.put("date_field_2", List.of("2024-05-12")); |
| 43 | + documentMap.put("boolean_field", List.of(true)); |
| 44 | + documentMap.put("null_field", listWithNull); |
| 45 | + documentMap.put("array_field_int", List.of(100, 200, 300, 400, 500)); |
| 46 | + documentMap.put("array_field_text", List.of("100", "200")); |
| 47 | + documentMap.put("object_type", List.of(Map.of("foo", Map.of("bar", 10)))); |
| 48 | + } |
| 49 | + |
| 50 | + public void testJsonSupportedTypes() throws IOException { |
| 51 | + MapperService mapperService = createMapperService(topMapping(b -> {})); |
| 52 | + QueryShardContext queryShardContext = createQueryShardContext(mapperService); |
| 53 | + when(queryShardContext.index()).thenReturn(new Index("test_index", "uuid")); |
| 54 | + int totalDocs = 10000; |
| 55 | + int docsPerLeafCount = 1000; |
| 56 | + try (Directory dir = newDirectory()) { |
| 57 | + IndexWriter iw = new IndexWriter(dir, new IndexWriterConfig(Lucene.STANDARD_ANALYZER)); |
| 58 | + Document d = new Document(); |
| 59 | + for (int i = 0; i < totalDocs; i++) { |
| 60 | + iw.addDocument(d); |
| 61 | + if ((i + 1) % docsPerLeafCount == 0) { |
| 62 | + iw.commit(); |
| 63 | + } |
| 64 | + } |
| 65 | + try (IndexReader reader = DirectoryReader.open(iw)) { |
| 66 | + iw.close(); |
| 67 | + FieldTypeInference typeInference = new FieldTypeInference("test_index", queryShardContext.getMapperService(), reader); |
| 68 | + String[] fieldName = { "text_field" }; |
| 69 | + Mapper mapper = typeInference.infer(lookup -> documentMap.get(fieldName[0])); |
| 70 | + assertEquals("text", mapper.typeName()); |
| 71 | + |
| 72 | + fieldName[0] = "int_field"; |
| 73 | + mapper = typeInference.infer(lookup -> documentMap.get(fieldName[0])); |
| 74 | + assertEquals("long", mapper.typeName()); |
| 75 | + |
| 76 | + fieldName[0] = "float_field"; |
| 77 | + mapper = typeInference.infer(lookup -> documentMap.get(fieldName[0])); |
| 78 | + assertEquals("float", mapper.typeName()); |
| 79 | + |
| 80 | + fieldName[0] = "date_field_1"; |
| 81 | + mapper = typeInference.infer(lookup -> documentMap.get(fieldName[0])); |
| 82 | + assertEquals("date", mapper.typeName()); |
| 83 | + |
| 84 | + fieldName[0] = "date_field_2"; |
| 85 | + mapper = typeInference.infer(lookup -> documentMap.get(fieldName[0])); |
| 86 | + assertEquals("date", mapper.typeName()); |
| 87 | + |
| 88 | + fieldName[0] = "boolean_field"; |
| 89 | + mapper = typeInference.infer(lookup -> documentMap.get(fieldName[0])); |
| 90 | + assertEquals("boolean", mapper.typeName()); |
| 91 | + |
| 92 | + fieldName[0] = "array_field_int"; |
| 93 | + mapper = typeInference.infer(lookup -> documentMap.get(fieldName[0])); |
| 94 | + assertEquals("long", mapper.typeName()); |
| 95 | + |
| 96 | + fieldName[0] = "array_field_text"; |
| 97 | + mapper = typeInference.infer(lookup -> documentMap.get(fieldName[0])); |
| 98 | + assertEquals("text", mapper.typeName()); |
| 99 | + |
| 100 | + fieldName[0] = "object_type"; |
| 101 | + mapper = typeInference.infer(lookup -> documentMap.get(fieldName[0])); |
| 102 | + assertEquals("object", mapper.typeName()); |
| 103 | + |
| 104 | + fieldName[0] = "null_field"; |
| 105 | + mapper = typeInference.infer(lookup -> documentMap.get(fieldName[0])); |
| 106 | + assertNull(mapper); |
| 107 | + |
| 108 | + // If field is missing ensure that sample docIDs generated for inference are ordered and are in bounds |
| 109 | + fieldName[0] = "missing_field"; |
| 110 | + List<List<Integer>> docsEvaluated = new ArrayList<>(); |
| 111 | + int[] totalDocsEvaluated = { 0 }; |
| 112 | + typeInference.setSampleSize(50); |
| 113 | + mapper = typeInference.infer(new ValueFetcher() { |
| 114 | + @Override |
| 115 | + public List<Object> fetchValues(SourceLookup lookup) throws IOException { |
| 116 | + docsEvaluated.get(docsEvaluated.size() - 1).add(lookup.docId()); |
| 117 | + totalDocsEvaluated[0]++; |
| 118 | + return documentMap.get(fieldName[0]); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public void setNextReader(LeafReaderContext leafReaderContext) { |
| 123 | + docsEvaluated.add(new ArrayList<>()); |
| 124 | + } |
| 125 | + }); |
| 126 | + assertNull(mapper); |
| 127 | + assertEquals(typeInference.getSampleSize(), totalDocsEvaluated[0]); |
| 128 | + for (List<Integer> docsPerLeaf : docsEvaluated) { |
| 129 | + for (int j = 0; j < docsPerLeaf.size() - 1; j++) { |
| 130 | + assertTrue(docsPerLeaf.get(j) < docsPerLeaf.get(j + 1)); |
| 131 | + } |
| 132 | + if (!docsPerLeaf.isEmpty()) { |
| 133 | + assertTrue(docsPerLeaf.get(0) >= 0 && docsPerLeaf.get(docsPerLeaf.size() - 1) < docsPerLeafCount); |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + public void testDeleteAllDocs() throws IOException { |
| 141 | + MapperService mapperService = createMapperService(topMapping(b -> {})); |
| 142 | + QueryShardContext queryShardContext = createQueryShardContext(mapperService); |
| 143 | + when(queryShardContext.index()).thenReturn(new Index("test_index", "uuid")); |
| 144 | + int totalDocs = 10000; |
| 145 | + int docsPerLeafCount = 1000; |
| 146 | + try (Directory dir = newDirectory()) { |
| 147 | + IndexWriter iw = new IndexWriter(dir, new IndexWriterConfig(Lucene.STANDARD_ANALYZER)); |
| 148 | + Document d = new Document(); |
| 149 | + for (int i = 0; i < totalDocs; i++) { |
| 150 | + iw.addDocument(d); |
| 151 | + if ((i + 1) % docsPerLeafCount == 0) { |
| 152 | + iw.commit(); |
| 153 | + } |
| 154 | + } |
| 155 | + iw.deleteAll(); |
| 156 | + iw.commit(); |
| 157 | + |
| 158 | + try (IndexReader reader = DirectoryReader.open(iw)) { |
| 159 | + iw.close(); |
| 160 | + FieldTypeInference typeInference = new FieldTypeInference("test_index", queryShardContext.getMapperService(), reader); |
| 161 | + String[] fieldName = { "text_field" }; |
| 162 | + Mapper mapper = typeInference.infer(lookup -> documentMap.get(fieldName[0])); |
| 163 | + assertNull(mapper); |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + public void testZeroDoc() throws IOException { |
| 169 | + MapperService mapperService = createMapperService(topMapping(b -> {})); |
| 170 | + QueryShardContext queryShardContext = createQueryShardContext(mapperService); |
| 171 | + when(queryShardContext.index()).thenReturn(new Index("test_index", "uuid")); |
| 172 | + try (Directory dir = newDirectory()) { |
| 173 | + IndexWriter iw = new IndexWriter(dir, new IndexWriterConfig(Lucene.STANDARD_ANALYZER)); |
| 174 | + try (IndexReader reader = DirectoryReader.open(iw)) { |
| 175 | + iw.close(); |
| 176 | + FieldTypeInference typeInference = new FieldTypeInference("test_index", queryShardContext.getMapperService(), reader); |
| 177 | + String[] fieldName = { "text_field" }; |
| 178 | + Mapper mapper = typeInference.infer(lookup -> documentMap.get(fieldName[0])); |
| 179 | + assertNull(mapper); |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + public void testSampleGeneration() throws IOException { |
| 185 | + MapperService mapperService = createMapperService(topMapping(b -> {})); |
| 186 | + QueryShardContext queryShardContext = createQueryShardContext(mapperService); |
| 187 | + when(queryShardContext.index()).thenReturn(new Index("test_index", "uuid")); |
| 188 | + int totalDocs = 10000; |
| 189 | + int docsPerLeafCount = 1000; |
| 190 | + try (Directory dir = newDirectory()) { |
| 191 | + IndexWriter iw = new IndexWriter(dir, new IndexWriterConfig(Lucene.STANDARD_ANALYZER)); |
| 192 | + Document d = new Document(); |
| 193 | + for (int i = 0; i < totalDocs; i++) { |
| 194 | + iw.addDocument(d); |
| 195 | + if ((i + 1) % docsPerLeafCount == 0) { |
| 196 | + iw.commit(); |
| 197 | + } |
| 198 | + } |
| 199 | + try (IndexReader reader = DirectoryReader.open(iw)) { |
| 200 | + iw.close(); |
| 201 | + FieldTypeInference typeInference = new FieldTypeInference("test_index", queryShardContext.getMapperService(), reader); |
| 202 | + typeInference.setSampleSize(1000 - 1); |
| 203 | + typeInference.infer(lookup -> documentMap.get("unknown_field")); |
| 204 | + assertThrows(IllegalArgumentException.class, () -> typeInference.setSampleSize(1000 + 1)); |
| 205 | + typeInference.setSampleSize(1000); |
| 206 | + typeInference.infer(lookup -> documentMap.get("unknown_field")); |
| 207 | + } |
| 208 | + } |
| 209 | + } |
| 210 | +} |
0 commit comments