NCIOCPL / r4r-api

API to support the Resources for Researchers application
0 stars 2 forks source link

Facet information should be from configuration #4

Closed bryanpizzillo closed 6 years ago

bryanpizzillo commented 6 years ago

In the resources controller the facets code should be more generic where we have:


// Perform query for Research Areas and Research Types aggregations
            KeyLabelAggResult[] raAggResults = _aggService.GetKeyLabelAggregation("researchAreas", resourceQuery);
            KeyLabelAggResult[] rtAggResults = _aggService.GetKeyLabelAggregation("researchTypes", resourceQuery);
            KeyLabelAggResult[] ttAggResults = _aggService.GetKeyLabelAggregation("toolTypes", resourceQuery);

            // Convert aggregations into facet items/facets
            List<Facet> facets = new List<Facet>();
            if (raAggResults != null)
            {
                Facet raFacet = new Facet();
                raFacet.Param = "researchAreas";
                raFacet.Title = "Research Areas";

                var raFacetItems = raAggResults.Select(i => new FacetItem { Key = i.Key, Label = i.Label, Count = Convert.ToInt32(i.Count) });
                raFacet.Items = raFacetItems.ToArray();

                facets.Add(raFacet);
            }
            //... more facets

Should be something more like:

  if (includeFacets && !this.validateFacetListFromConfig())
      throw error

   //...

  string[] facetsToFetch = includeFacets ?? apiconfig.defaultIncludeFacets;

  List<Facet> facets = new List<Facet>();

  foreach(var facetToFetch in facetsToFetch) {
         KeyLabelAggResult[] res = _aggService.GetKeyLabelAggregation(facetToFetch, resourceQuery)
         if (res != null && res.length > 0) {
             facets.Add(
                new Facet() {
                    Param = facetToFetch,
                    Title = this.GetFacetTitleFromConfig(facetToFetch)
                    Items = res.Select(i => new FacetItem { Key = i.Key, Label = i.Label, Count = Convert.ToInt32(i.Count) }).ToArray()
               }
            )
         }
   }

The facet config should look something like:

  "R4RAPI": {
    "AliasName": "r4r_v1",
    "DefaultIncludeFacets": ["toolTypes", "toolSubtypes", "researchAreas", "researchTypes" ],
    "AvailableFacets": {  //This should be a dictionary!
       "toolTypes": {
          "Label": "Tool Types",
          "FacetType": "single"
       },
       "toolSubtypes{
         "Label": "Tool Sub-types",
         "RequiresFilter": "toolTypes",
         "FacetType": "multiple"
       },
        ..., 
       "docs": {
          "Label": "Divisions, Offices, and Centers",
          "FacetType": "multiple"
       }
    }
  }