Skip to content

Commit

Permalink
Return early
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecote committed Feb 19, 2021
1 parent 461016b commit 9c1b890
Showing 1 changed file with 19 additions and 24 deletions.
43 changes: 19 additions & 24 deletions src/core/server/saved_objects/service/lib/filter_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,39 +206,34 @@ export const hasFilterKeyError = (
};

export const fieldDefined = (indexMappings: IndexMapping, key: string): boolean => {
let isFieldDefined = false;

const mappingKey = 'properties.' + key.split('.').join('.properties.');
isFieldDefined = get(indexMappings, mappingKey) != null;
if (get(indexMappings, mappingKey) != null) {
return true;
}

// If the `mappingKey` does not match a valid path, before returning null,
// If the `mappingKey` does not match a valid path, before returning false,
// we want to check and see if the intended path was for a multi-field
// such as `x.attributes.field.text` where `field` is mapped to both text
// and keyword
if (!isFieldDefined) {
const propertiesAttribute = 'properties';
const indexOfLastProperties = mappingKey.lastIndexOf(propertiesAttribute);
const fieldMapping = mappingKey.substr(0, indexOfLastProperties);
const fieldType = mappingKey.substr(
mappingKey.lastIndexOf(propertiesAttribute) + `${propertiesAttribute}.`.length
);
const mapping = `${fieldMapping}fields.${fieldType}`;

isFieldDefined = get(indexMappings, mapping) != null;
const propertiesAttribute = 'properties';
const indexOfLastProperties = mappingKey.lastIndexOf(propertiesAttribute);
const fieldMapping = mappingKey.substr(0, indexOfLastProperties);
const fieldType = mappingKey.substr(
mappingKey.lastIndexOf(propertiesAttribute) + `${propertiesAttribute}.`.length
);
const mapping = `${fieldMapping}fields.${fieldType}`;
if (get(indexMappings, mapping) != null) {
return true;
}

// If the path is for a flattned type field, we'll assume the mappings are defined.
if (!isFieldDefined) {
const keys = key.split('.');
for (let i = 0; i < keys.length; i++) {
const path = `properties.${keys.slice(0, i + 1).join('.properties.')}`;
const mapping = get(indexMappings, path);
if (mapping?.type === 'flattened') {
isFieldDefined = true;
break;
}
const keys = key.split('.');
for (let i = 0; i < keys.length; i++) {
const path = `properties.${keys.slice(0, i + 1).join('.properties.')}`;
if (get(indexMappings, path)?.type === 'flattened') {
return true;
}
}

return isFieldDefined;
return false;
};

0 comments on commit 9c1b890

Please sign in to comment.