graphql-compose-elasticsearch
Version:
Elastic search via GraphQL
176 lines • 6.09 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getFieldConfigMap = exports.getFieldNamesType = exports.getFieldNamesByElasticType = exports.getAllAsFieldConfigMap = exports.getAllFields = exports.getTermFields = exports.getPercolatorFields = exports.getIpFields = exports.getNestedFields = exports.getGeoShapeAsFieldConfigMap = exports.getGeoPointAsFieldConfigMap = exports.getGeoPointFields = exports.getBooleanFields = exports.getDateFields = exports.getNumericFields = exports.getKeywordAsFieldConfigMap = exports.getAnalyzedAsFieldConfigMap = exports.getAnalyzedFields = exports.getStringAsFieldConfigMap = exports.getStringFields = void 0;
const graphql_compose_1 = require("graphql-compose");
const utils_1 = require("../../utils");
function getStringFields(opts) {
return getFieldNamesType(opts, ['text', 'keyword', 'string'], 'String');
}
exports.getStringFields = getStringFields;
function getStringAsFieldConfigMap(opts, fc) {
return getFieldConfigMap(opts, ['text', 'keyword', 'string'], fc);
}
exports.getStringAsFieldConfigMap = getStringAsFieldConfigMap;
function getAnalyzedFields(opts) {
return getFieldNamesType(opts, ['text', 'string'], 'String', true);
}
exports.getAnalyzedFields = getAnalyzedFields;
function getAnalyzedAsFieldConfigMap(opts, fc) {
return getFieldConfigMap(opts, ['text', 'string'], fc, true);
}
exports.getAnalyzedAsFieldConfigMap = getAnalyzedAsFieldConfigMap;
function getKeywordAsFieldConfigMap(opts, fc) {
return getFieldConfigMap(opts, ['keyword'], fc);
}
exports.getKeywordAsFieldConfigMap = getKeywordAsFieldConfigMap;
function getNumericFields(opts) {
return getFieldNamesType(opts, [
'byte',
'short',
'integer',
'long',
'double',
'float',
'half_float',
'scaled_float',
'token_count',
], 'Numeric');
}
exports.getNumericFields = getNumericFields;
function getDateFields(opts) {
return getFieldNamesType(opts, ['date'], 'Date');
}
exports.getDateFields = getDateFields;
function getBooleanFields(opts) {
return getFieldNamesType(opts, ['boolean'], 'Boolean');
}
exports.getBooleanFields = getBooleanFields;
function getGeoPointFields(opts) {
return getFieldNamesType(opts, ['geo_point'], 'GeoPoint');
}
exports.getGeoPointFields = getGeoPointFields;
function getGeoPointAsFieldConfigMap(opts, fc) {
return getFieldConfigMap(opts, ['geo_point'], fc);
}
exports.getGeoPointAsFieldConfigMap = getGeoPointAsFieldConfigMap;
function getGeoShapeAsFieldConfigMap(opts, fc) {
return getFieldConfigMap(opts, ['geo_shape'], fc);
}
exports.getGeoShapeAsFieldConfigMap = getGeoShapeAsFieldConfigMap;
function getNestedFields(opts) {
return getFieldNamesType(opts, ['nested'], 'Nested');
}
exports.getNestedFields = getNestedFields;
function getIpFields(opts) {
return getFieldNamesType(opts, ['ip'], 'Ip');
}
exports.getIpFields = getIpFields;
function getPercolatorFields(opts) {
return getFieldNamesType(opts, ['percolator'], 'Percolator');
}
exports.getPercolatorFields = getPercolatorFields;
function getTermFields(opts) {
return getFieldNamesType(opts, [
'keyword',
'date',
'boolean',
'ip',
'byte',
'short',
'integer',
'long',
'double',
'float',
'half_float',
'scaled_float',
'token_count',
], 'Term');
}
exports.getTermFields = getTermFields;
function getAllFields(opts) {
return getFieldNamesType(opts, ['_all'], 'All');
}
exports.getAllFields = getAllFields;
function getAllAsFieldConfigMap(opts, fc) {
return getFieldConfigMap(opts, ['_all'], fc);
}
exports.getAllAsFieldConfigMap = getAllAsFieldConfigMap;
function getFieldNamesByElasticType(fieldMap, types) {
const fieldNames = [];
types.forEach((type) => {
if (typeof fieldMap[type] === 'object') {
Object.keys(fieldMap[type]).forEach((fieldName) => {
fieldNames.push(fieldName);
});
}
});
return fieldNames;
}
exports.getFieldNamesByElasticType = getFieldNamesByElasticType;
function getFieldNamesType(opts, types, typePrefix, addAll = false) {
if (!opts || !opts.fieldMap) {
return 'String';
}
if (!types) {
types = ['_all'];
typePrefix = 'All';
}
if (!typePrefix) {
types.sort();
typePrefix = types.map((t) => (0, graphql_compose_1.upperFirst)(t)).join('');
}
const name = (0, utils_1.getTypeName)(`${typePrefix}Fields`, opts);
const description = (0, utils_1.desc)(`Available fields from mapping.`);
return opts.schemaComposer.getOrSet(name, () => {
if (!opts || !opts.fieldMap) {
return opts.schemaComposer.getSTC('String');
}
const values = getEnumValues(opts.fieldMap, types, addAll);
if (Object.keys(values).length === 0) {
return opts.schemaComposer.getSTC('String');
}
return opts.schemaComposer.createEnumTC({
name,
description,
values,
});
});
}
exports.getFieldNamesType = getFieldNamesType;
function getEnumValues(fieldMap, types, addAll = false) {
const values = {};
if (addAll) {
values._all = {
value: '_all',
};
}
getFieldNamesByElasticType(fieldMap, types).forEach((fieldName) => {
values[fieldName] = {
value: fieldName.replace(/__/g, '.'),
};
});
return values;
}
function getFieldConfigMap(opts, types, fc, addAll = false) {
if (!fc)
fc = 'JSON';
if (!opts || !opts.fieldMap) {
return 'JSON';
}
if (!types) {
types = ['_all'];
}
const fcMap = {};
if (addAll) {
fcMap._all = fc;
}
getFieldNamesByElasticType(opts.fieldMap, types).forEach((fieldName) => {
fcMap[fieldName] = fc;
});
if (Object.keys(fcMap).length === 0) {
return 'JSON';
}
return fcMap;
}
exports.getFieldConfigMap = getFieldConfigMap;
//# sourceMappingURL=FieldNames.js.map