elastic-harvester.js
function createBuckets(terms) {
            return _.map(terms, (term) => {
              // 1. see if there are other terms & if they have buckets.
              const retVal = { key: term.key, count: term.doc_count };

              _.each(term, (aggResponse, responseKey) => {
                if (responseKey === 'key' || responseKey === 'doc_count') {
                  return null;
                } else if (aggResponse.buckets) {
                  retVal[responseKey] = createBuckets(aggResponse.buckets);
                } else if (aggResponse.hits && aggResponse.hits.hits) {
                  // top_hits aggs result from nested query w/o reverse nesting.
                  retVal[responseKey] = getTopHitsResult(aggResponse, responseKey, _esResponse, aggregationObjects);
                  // to combine nested aggs w others, you have to un-nest them, & this takes up an aggregation-space.
                } else if (responseKey !== 'reverse_nesting' && aggResponse) { // stats & extended_stats aggs
                  // This means it's the result of a nested stats or extended stats query.
                  if (aggResponse[responseKey]) {
                    retVal[responseKey] = aggResponse[responseKey];
                  } else {
                    retVal[responseKey] = aggResponse;
                  }
                } else if (responseKey === 'reverse_nesting') {
                  _.each(aggResponse, (reverseNestedResponseProperty, reverseNestedResponseKey) => {
                    if (reverseNestedResponseKey === 'doc_count') {
                      return null;
                    } else if (reverseNestedResponseProperty.buckets) {
                      retVal[reverseNestedResponseKey] = createBuckets(reverseNestedResponseProperty.buckets);
                      // this gets a little complicated because reverse-nested then renested subdocuments are ..
                      // complicated (because the extra aggs for nesting throws things off).
                    } else if (reverseNestedResponseProperty[reverseNestedResponseKey] &&
                        reverseNestedResponseProperty[reverseNestedResponseKey].buckets) {
                      retVal[reverseNestedResponseKey] = createBuckets(
                        reverseNestedResponseProperty[reverseNestedResponseKey].buckets);

                      // this gets a little MORE complicated because of reverse-nested then renested top_hits aggs
                    } else if (reverseNestedResponseProperty.hits && reverseNestedResponseProperty.hits.hits) {
                      retVal[reverseNestedResponseKey] = getTopHitsResult(reverseNestedResponseProperty,
                        reverseNestedResponseKey, _esResponse, aggregationObjects);

                      // stats & extended_stats aggs
                    } else if (reverseNestedResponseProperty) {
                      // This means it's the result of a nested stats or extended stats query.
                      if (reverseNestedResponseProperty[reverseNestedResponseKey]) {
                        retVal[reverseNestedResponseKey] = reverseNestedResponseProperty[reverseNestedResponseKey];
                      } else {
                        retVal[reverseNestedResponseKey] = reverseNestedResponseProperty;
                      }
                    }
                    return null;
                  });
                }
                return null;
              });

              return retVal;
            });
          }
function createBuckets(terms) {
            return _.map(terms, (term) => {
              // 1. see if there are other terms & if they have buckets.
              const retVal = { key: term.key, count: term.doc_count };

              _.each(term, (aggResponse, responseKey) => {
                if (responseKey === 'key' || responseKey === 'doc_count') {
                  return null;
                } else if (aggResponse.buckets) {
                  retVal[responseKey] = createBuckets(aggResponse.buckets);
                } else if (aggResponse.hits && aggResponse.hits.hits) {
                  // top_hits aggs result from nested query w/o reverse nesting.
                  retVal[responseKey] = getTopHitsResult(aggResponse, responseKey, _esResponse, aggregationObjects);
                  // to combine nested aggs w others, you have to un-nest them, & this takes up an aggregation-space.
                } else if (responseKey !== 'reverse_nesting' && aggResponse) { // stats & extended_stats aggs
                  // This means it's the result of a nested stats or extended stats query.
                  if (aggResponse[responseKey]) {
                    retVal[responseKey] = aggResponse[responseKey];
                  } else {
                    retVal[responseKey] = aggResponse;
                  }
                } else if (responseKey === 'reverse_nesting') {
                  _.each(aggResponse, (reverseNestedResponseProperty, reverseNestedResponseKey) => {
                    if (reverseNestedResponseKey === 'doc_count') {
                      return null;
                    } else if (reverseNestedResponseProperty.buckets) {
                      retVal[reverseNestedResponseKey] = createBuckets(reverseNestedResponseProperty.buckets);
                      // this gets a little complicated because reverse-nested then renested subdocuments are ..
                      // complicated (because the extra aggs for nesting throws things off).
                    } else if (reverseNestedResponseProperty[reverseNestedResponseKey] &&
                        reverseNestedResponseProperty[reverseNestedResponseKey].buckets) {
                      retVal[reverseNestedResponseKey] = createBuckets(
                        reverseNestedResponseProperty[reverseNestedResponseKey].buckets);

                      // this gets a little MORE complicated because of reverse-nested then renested top_hits aggs
                    } else if (reverseNestedResponseProperty.hits && reverseNestedResponseProperty.hits.hits) {
                      retVal[reverseNestedResponseKey] = getTopHitsResult(reverseNestedResponseProperty,
                        reverseNestedResponseKey, _esResponse, aggregationObjects);

                      // stats & extended_stats aggs
                    } else if (reverseNestedResponseProperty) {
                      // This means it's the result of a nested stats or extended stats query.
                      if (reverseNestedResponseProperty[reverseNestedResponseKey]) {
                        retVal[reverseNestedResponseKey] = reverseNestedResponseProperty[reverseNestedResponseKey];
                      } else {
                        retVal[reverseNestedResponseKey] = reverseNestedResponseProperty;
                      }
                    }
                    return null;
                  });
                }
                return null;
              });

              return retVal;
            });
          }
function createBuckets ( terms ) { return _ . map ( terms , ( term ) => { // 1. see if there are other terms & if they have buckets. const retVal = { key : term . key , count : term . doc_count } ; _ . each ( term , ( aggResponse , responseKey ) => { if ( responseKey === 'key' || responseKey === 'doc_count' ) { return null ; } else if ( aggResponse . buckets ) { retVal [ responseKey ] = createBuckets ( aggResponse . buckets ) ; } else if ( aggResponse . hits && aggResponse . hits . hits ) { // top_hits aggs result from nested query w/o reverse nesting. retVal [ responseKey ] = getTopHitsResult ( aggResponse , responseKey , _esResponse , aggregationObjects ) ; // to combine nested aggs w others, you have to un-nest them, & this takes up an aggregation-space. } else if ( responseKey !== 'reverse_nesting' && aggResponse ) { // stats & extended_stats aggs // This means it's the result of a nested stats or extended stats query. if ( aggResponse [ responseKey ] ) { retVal [ responseKey ] = aggResponse [ responseKey ] ; } else { retVal [ responseKey ] = aggResponse ; } } else if ( responseKey === 'reverse_nesting' ) { _ . each ( aggResponse , ( reverseNestedResponseProperty , reverseNestedResponseKey ) => { if ( reverseNestedResponseKey === 'doc_count' ) { return null ; } else if ( reverseNestedResponseProperty . buckets ) { retVal [ reverseNestedResponseKey ] = createBuckets ( reverseNestedResponseProperty . buckets ) ; // this gets a little complicated because reverse-nested then renested subdocuments are .. // complicated (because the extra aggs for nesting throws things off). } else if ( reverseNestedResponseProperty [ reverseNestedResponseKey ] && reverseNestedResponseProperty [ reverseNestedResponseKey ] . buckets ) { retVal [ reverseNestedResponseKey ] = createBuckets ( reverseNestedResponseProperty [ reverseNestedResponseKey ] . buckets ) ; // this gets a little MORE complicated because of reverse-nested then renested top_hits aggs } else if ( reverseNestedResponseProperty . hits && reverseNestedResponseProperty . hits . hits ) { retVal [ reverseNestedResponseKey ] = getTopHitsResult ( reverseNestedResponseProperty , reverseNestedResponseKey , _esResponse , aggregationObjects ) ; // stats & extended_stats aggs } else if ( reverseNestedResponseProperty ) { // This means it's the result of a nested stats or extended stats query. if ( reverseNestedResponseProperty [ reverseNestedResponseKey ] ) { retVal [ reverseNestedResponseKey ] = reverseNestedResponseProperty [ reverseNestedResponseKey ] ; } else { retVal [ reverseNestedResponseKey ] = reverseNestedResponseProperty ; } } return null ; } ) ; } return null ; } ) ; return retVal ; } ) ; }
Add in meta.aggregations field
Add in meta . aggregations field