appbaseio/reactivesearch

View on GitHub
packages/web/src/components/list/utils.js

Summary

Maintainability
C
1 day
Test Coverage
import { getAggsOrder } from '@appbaseio/reactivecore/lib/utils/helper';

const getAggsQuery = (query, props, include) => {
    const clonedQuery = { ...query };
    const {
        dataField, size, sortBy, showMissing, missingLabel,
    } = props;

    let includesQuery = {};

    if (include) {
        includesQuery = {
            include,
        };
    }

    clonedQuery.size = 0;
    clonedQuery.aggs = {
        [dataField]: {
            terms: {
                field: dataField,
                size,
                order: getAggsOrder(sortBy || 'count'),
                ...(showMissing ? { missing: missingLabel } : {}),
                ...includesQuery,
            },
        },
    };

    if (props.nestedField) {
        clonedQuery.aggs = {
            reactivesearch_nested: {
                nested: {
                    path: props.nestedField,
                },
                aggs: clonedQuery.aggs,
            },
        };
    }

    return clonedQuery;
};

const getCompositeAggsQuery = (query, props, after) => {
    const clonedQuery = { ...query };
    // missing label not available in composite aggs
    const {
        dataField, size, sortBy, showMissing,
    } = props;

    // composite aggs only allows asc and desc
    const order = sortBy === 'count' ? {} : { order: sortBy };

    clonedQuery.aggs = {
        [dataField]: {
            composite: {
                sources: [
                    {
                        [dataField]: {
                            terms: {
                                field: dataField,
                                ...order,
                                ...(showMissing ? { missing_bucket: true } : {}),
                            },
                        },
                    },
                ],
                size,
                ...after,
            },
        },
    };

    if (props.nestedField) {
        clonedQuery.aggs = {
            reactivesearch_nested: {
                nested: {
                    path: props.nestedField,
                },
                aggs: clonedQuery.aggs,
            },
        };
    }

    return clonedQuery;
};

export { getAggsQuery, getCompositeAggsQuery };