|
15 | 15 | import org.elasticsearch.common.regex.Regex;
|
16 | 16 | import org.elasticsearch.index.IndexSettings;
|
17 | 17 | import org.elasticsearch.index.mapper.BooleanFieldMapper;
|
| 18 | +import org.elasticsearch.index.mapper.ObjectMapper; |
18 | 19 | import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
|
19 | 20 | import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsConfig;
|
20 | 21 | import org.elasticsearch.xpack.core.ml.dataframe.analyses.DataFrameAnalysis;
|
|
40 | 41 | import java.util.Set;
|
41 | 42 | import java.util.TreeSet;
|
42 | 43 | import java.util.stream.Collectors;
|
| 44 | +import java.util.stream.Stream; |
43 | 45 |
|
44 | 46 | public class ExtractedFieldsDetector {
|
45 | 47 |
|
@@ -82,6 +84,7 @@ private Set<String> getIncludedFields(Set<FieldSelection> fieldSelection) {
|
82 | 84 | Set<String> fields = new TreeSet<>(fieldCapabilitiesResponse.get().keySet());
|
83 | 85 | fields.removeAll(IGNORE_FIELDS);
|
84 | 86 | removeFieldsUnderResultsField(fields);
|
| 87 | + removeObjects(fields); |
85 | 88 | applySourceFiltering(fields);
|
86 | 89 | FetchSourceContext analyzedFields = config.getAnalyzedFields();
|
87 | 90 |
|
@@ -112,6 +115,17 @@ private void removeFieldsUnderResultsField(Set<String> fields) {
|
112 | 115 | fields.removeIf(field -> field.startsWith(resultsField + "."));
|
113 | 116 | }
|
114 | 117 |
|
| 118 | + private void removeObjects(Set<String> fields) { |
| 119 | + Iterator<String> fieldsIterator = fields.iterator(); |
| 120 | + while (fieldsIterator.hasNext()) { |
| 121 | + String field = fieldsIterator.next(); |
| 122 | + Set<String> types = getMappingTypes(field); |
| 123 | + if (isObject(types)) { |
| 124 | + fieldsIterator.remove(); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
115 | 129 | private void applySourceFiltering(Set<String> fields) {
|
116 | 130 | Iterator<String> fieldsIterator = fields.iterator();
|
117 | 131 | while (fieldsIterator.hasNext()) {
|
@@ -178,6 +192,9 @@ private void includeAndExcludeFields(Set<String> fields, Set<FieldSelection> fie
|
178 | 192 | if (analyzedFields == null) {
|
179 | 193 | return;
|
180 | 194 | }
|
| 195 | + |
| 196 | + checkIncludesExcludesAreNotObjects(analyzedFields); |
| 197 | + |
181 | 198 | String includes = analyzedFields.includes().length == 0 ? "*" : Strings.arrayToCommaDelimitedString(analyzedFields.includes());
|
182 | 199 | String excludes = Strings.arrayToCommaDelimitedString(analyzedFields.excludes());
|
183 | 200 |
|
@@ -205,6 +222,16 @@ private void includeAndExcludeFields(Set<String> fields, Set<FieldSelection> fie
|
205 | 222 | }
|
206 | 223 | }
|
207 | 224 |
|
| 225 | + private void checkIncludesExcludesAreNotObjects(FetchSourceContext analyzedFields) { |
| 226 | + List<String> objectFields = Stream.concat(Arrays.stream(analyzedFields.includes()), Arrays.stream(analyzedFields.excludes())) |
| 227 | + .filter(field -> isObject(getMappingTypes(field))) |
| 228 | + .collect(Collectors.toList()); |
| 229 | + if (objectFields.isEmpty() == false) { |
| 230 | + throw ExceptionsHelper.badRequestException("{} must not include or exclude object fields: {}", |
| 231 | + DataFrameAnalyticsConfig.ANALYZED_FIELDS.getPreferredName(), objectFields); |
| 232 | + } |
| 233 | + } |
| 234 | + |
208 | 235 | private void applyIncludesExcludes(Set<String> fields, Set<String> includes, Set<String> excludes,
|
209 | 236 | Set<FieldSelection> fieldSelection) {
|
210 | 237 | Iterator<String> fieldsIterator = fields.iterator();
|
@@ -394,4 +421,8 @@ static Set<String> getCategoricalFields(ExtractedFields extractedFields, DataFra
|
394 | 421 | private static boolean isBoolean(Set<String> types) {
|
395 | 422 | return types.size() == 1 && types.contains(BooleanFieldMapper.CONTENT_TYPE);
|
396 | 423 | }
|
| 424 | + |
| 425 | + private boolean isObject(Set<String> types) { |
| 426 | + return types.size() == 1 && types.contains(ObjectMapper.CONTENT_TYPE); |
| 427 | + } |
397 | 428 | }
|
0 commit comments