elastic-harvester.js
function getTopHitsResult(aggResponse, aggName, esResponse, aggregationObjects) {
    const aggLookup = {};
    const linked = {}; // keeps track of all linked objects. type->id->true
    const typeLookup = {};

    function getAggLookup(_aggLookup, _aggregationObjects) {
      _.each(_aggregationObjects, (aggObj) => {
        _aggLookup[aggObj.name] = aggObj;
        aggObj.aggregations && getAggLookup(_aggLookup, aggObj.aggregations);
      });
    }

    getAggLookup(aggLookup, aggregationObjects);

    // dedupes already-linked entities.
    if (aggLookup[aggName] && aggLookup[aggName].include) {
      _.each(aggLookup[aggName].include.split(','), (linkProperty) => {
        if (_this.collectionLookup[linkProperty]) {
          const _type = inflect.pluralize(_this.collectionLookup[linkProperty]);
          typeLookup[linkProperty] = _type;

          esResponse.linked && _.each(esResponse.linked[_type] || [], (resource) => {
            linked[_type] = linked[_type] || {};
            linked[_type][resource.id] = true;
          });
        }
      });
    }

    return _.map(aggResponse.hits.hits, (esReponseObj) => {
      if (aggLookup[aggName] && aggLookup[aggName].include) {
        _.each(aggLookup[aggName].include.split(','), (linkProperty) => {
          if (typeLookup[linkProperty]) {
            const _type = typeLookup[linkProperty];

            // if this isn't already linked, link it.
            // TODO: links may be an array of objects, so treat it that way at all times.
            const hasLinks = !!(esReponseObj._source.links) && !!(esReponseObj._source.links[linkProperty]);
            if (hasLinks) {
              const entitiesToInclude = [].concat(unexpandSubentity(esReponseObj._source.links[linkProperty]));

              _.each(entitiesToInclude, (entityToInclude) => {
                const entityIsAlreadyIncluded = !!(linked[_type]) && !!(linked[_type][entityToInclude.id]);
                if (!entityIsAlreadyIncluded) {
                  esResponse.linked = esResponse.linked || {};
                  esResponse.linked[_type] = esResponse.linked[_type] || [];

                  esResponse.linked[_type] = esResponse.linked[_type].concat(entityToInclude);
                  linked[_type] = linked[_type] || {};
                  linked[_type][entityToInclude.id] = true;
                }
              });
            }
          } else {
            console.warn(`[Elastic-Harvest] ${linkProperty} is not in collectionLookup. ${linkProperty} was either ` +
              'incorrectly specified by the end-user, or dev failed to include the relevant key in the lookup ' +
              'provided to initialize elastic-harvest.');
          }
        });
      }

      return unexpandEntity(esReponseObj._source);
    });
  }
function getTopHitsResult(aggResponse, aggName, esResponse, aggregationObjects) {
    const aggLookup = {};
    const linked = {}; // keeps track of all linked objects. type->id->true
    const typeLookup = {};

    function getAggLookup(_aggLookup, _aggregationObjects) {
      _.each(_aggregationObjects, (aggObj) => {
        _aggLookup[aggObj.name] = aggObj;
        aggObj.aggregations && getAggLookup(_aggLookup, aggObj.aggregations);
      });
    }

    getAggLookup(aggLookup, aggregationObjects);

    // dedupes already-linked entities.
    if (aggLookup[aggName] && aggLookup[aggName].include) {
      _.each(aggLookup[aggName].include.split(','), (linkProperty) => {
        if (_this.collectionLookup[linkProperty]) {
          const _type = inflect.pluralize(_this.collectionLookup[linkProperty]);
          typeLookup[linkProperty] = _type;

          esResponse.linked && _.each(esResponse.linked[_type] || [], (resource) => {
            linked[_type] = linked[_type] || {};
            linked[_type][resource.id] = true;
          });
        }
      });
    }

    return _.map(aggResponse.hits.hits, (esReponseObj) => {
      if (aggLookup[aggName] && aggLookup[aggName].include) {
        _.each(aggLookup[aggName].include.split(','), (linkProperty) => {
          if (typeLookup[linkProperty]) {
            const _type = typeLookup[linkProperty];

            // if this isn't already linked, link it.
            // TODO: links may be an array of objects, so treat it that way at all times.
            const hasLinks = !!(esReponseObj._source.links) && !!(esReponseObj._source.links[linkProperty]);
            if (hasLinks) {
              const entitiesToInclude = [].concat(unexpandSubentity(esReponseObj._source.links[linkProperty]));

              _.each(entitiesToInclude, (entityToInclude) => {
                const entityIsAlreadyIncluded = !!(linked[_type]) && !!(linked[_type][entityToInclude.id]);
                if (!entityIsAlreadyIncluded) {
                  esResponse.linked = esResponse.linked || {};
                  esResponse.linked[_type] = esResponse.linked[_type] || [];

                  esResponse.linked[_type] = esResponse.linked[_type].concat(entityToInclude);
                  linked[_type] = linked[_type] || {};
                  linked[_type][entityToInclude.id] = true;
                }
              });
            }
          } else {
            console.warn(`[Elastic-Harvest] ${linkProperty} is not in collectionLookup. ${linkProperty} was either ` +
              'incorrectly specified by the end-user, or dev failed to include the relevant key in the lookup ' +
              'provided to initialize elastic-harvest.');
          }
        });
      }

      return unexpandEntity(esReponseObj._source);
    });
  }
function getTopHitsResult ( aggResponse , aggName , esResponse , aggregationObjects ) { const aggLookup = { } ; const linked = { } ; // keeps track of all linked objects. type->id->true const typeLookup = { } ; function getAggLookup ( _aggLookup , _aggregationObjects ) { _ . each ( _aggregationObjects , ( aggObj ) => { _aggLookup [ aggObj . name ] = aggObj ; aggObj . aggregations && getAggLookup ( _aggLookup , aggObj . aggregations ) ; } ) ; } getAggLookup ( aggLookup , aggregationObjects ) ; // dedupes already-linked entities. if ( aggLookup [ aggName ] && aggLookup [ aggName ] . include ) { _ . each ( aggLookup [ aggName ] . include . split ( ',' ) , ( linkProperty ) => { if ( _this . collectionLookup [ linkProperty ] ) { const _type = inflect . pluralize ( _this . collectionLookup [ linkProperty ] ) ; typeLookup [ linkProperty ] = _type ; esResponse . linked && _ . each ( esResponse . linked [ _type ] || [ ] , ( resource ) => { linked [ _type ] = linked [ _type ] || { } ; linked [ _type ] [ resource . id ] = true ; } ) ; } } ) ; } return _ . map ( aggResponse . hits . hits , ( esReponseObj ) => { if ( aggLookup [ aggName ] && aggLookup [ aggName ] . include ) { _ . each ( aggLookup [ aggName ] . include . split ( ',' ) , ( linkProperty ) => { if ( typeLookup [ linkProperty ] ) { const _type = typeLookup [ linkProperty ] ; // if this isn't already linked, link it. // TODO: links may be an array of objects, so treat it that way at all times. const hasLinks = ! ! ( esReponseObj . _source . links ) && ! ! ( esReponseObj . _source . links [ linkProperty ] ) ; if ( hasLinks ) { const entitiesToInclude = [ ] . concat ( unexpandSubentity ( esReponseObj . _source . links [ linkProperty ] ) ) ; _ . each ( entitiesToInclude , ( entityToInclude ) => { const entityIsAlreadyIncluded = ! ! ( linked [ _type ] ) && ! ! ( linked [ _type ] [ entityToInclude . id ] ) ; if ( ! entityIsAlreadyIncluded ) { esResponse . linked = esResponse . linked || { } ; esResponse . linked [ _type ] = esResponse . linked [ _type ] || [ ] ; esResponse . linked [ _type ] = esResponse . linked [ _type ] . concat ( entityToInclude ) ; linked [ _type ] = linked [ _type ] || { } ; linked [ _type ] [ entityToInclude . id ] = true ; } } ) ; } } else { console . warn ( ` ${ linkProperty } ${ linkProperty } ` + 'incorrectly specified by the end-user, or dev failed to include the relevant key in the lookup ' + 'provided to initialize elastic-harvest.' ) ; } } ) ; } return unexpandEntity ( esReponseObj . _source ) ; } ) ; }
Note that this is not currently named well - it also provides the "includes" functionality to top_hits.
Note that this is not currently named well - it also provides the includes functionality to top_hits .