Skip to content

Commit

Permalink
feat(hub-common): hubSearchItems filters aggregations requests by the…
Browse files Browse the repository at this point in the history
… provided query (#1327)

affects: @esri/hub-common
  • Loading branch information
sonofflynn89 authored Nov 14, 2023
1 parent e22ef09 commit df53fb9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import { getProp } from "../../../objects/get-prop";
import { IQuery } from "../../types/IHubCatalog";
import { IHubSearchOptions } from "../../types/IHubSearchOptions";
import { getOpenDataQueryParam } from "./getOpenDataQueryParam";
import { getFilterQueryParam } from "./getFilterQueryParam";

export function getOgcAggregationQueryParams(
query: IQuery,
options: IHubSearchOptions
) {
// TODO: use options.aggLimit once the OGC API supports it
const aggregations = `terms(fields=(${options.aggFields.join()}))`;
// TODO: Use the rest of `query` to filter aggregations once the OGC API supports it
const openData = getOpenDataQueryParam(query);
const filter = getFilterQueryParam(query);
const token = getProp(options, "requestOptions.authentication.token");

return {
aggregations,
openData,
filter,
token,
};
}

This file was deleted.

37 changes: 21 additions & 16 deletions packages/common/test/search/_internal/hubSearchItems.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,11 @@ describe("hubSearchItems Module |", () => {
const options: IHubSearchOptions = {
aggFields: ["type", "tags", "categories"],
};
const result = getOgcAggregationQueryParams(baseQuery, options);
const emptyQuery: IQuery = {
targetEntity: "item",
filters: [],
};
const result = getOgcAggregationQueryParams(emptyQuery, options);
const queryString = getQueryString(result);
expect(queryString).toEqual(
`?aggregations=${encodeURIComponent(
Expand All @@ -550,23 +554,20 @@ describe("hubSearchItems Module |", () => {
);
});

it("handles aggregations and openData flag", () => {
it("handles aggregations and filter", () => {
const options: IHubSearchOptions = {
aggFields: ["type", "tags", "categories"],
};
const opendataQuery = cloneObject(baseQuery);
opendataQuery.filters.push({ predicates: [{ openData: true }] });

const result = getOgcAggregationQueryParams(opendataQuery, options);
const result = getOgcAggregationQueryParams(baseQuery, options);
const queryString = getQueryString(result);
expect(queryString).toEqual(
`?aggregations=${encodeURIComponent(
"terms(fields=(type,tags,categories))"
)}&openData=true`
)}&filter=${encodeURIComponent("((type=typeA))")}`
);
});

it("handles aggregations, openData flag and token", () => {
it("handles aggregations, filter, and token", () => {
const options: IHubSearchOptions = {
aggFields: ["type", "tags", "categories"],
requestOptions: {
Expand All @@ -583,7 +584,9 @@ describe("hubSearchItems Module |", () => {
expect(queryString).toEqual(
`?aggregations=${encodeURIComponent(
"terms(fields=(type,tags,categories))"
)}&openData=true&token=abc`
)}&filter=${encodeURIComponent(
"((type=typeA)) AND ((openData=true))"
)}&token=abc`
);
});
});
Expand Down Expand Up @@ -744,7 +747,7 @@ describe("hubSearchItems Module |", () => {
total: 0,
results: [],
hasNext: false,
next: () => null,
next: async () => null as any,
};

let searchOgcItemsSpy: jasmine.Spy;
Expand Down Expand Up @@ -835,9 +838,9 @@ describe("hubSearchItems Module |", () => {
expect(nextResult).toBeNull();

// Test aggregation results
expect(formatted.aggregations.length).toEqual(2);
expect(formatted.aggregations?.length).toEqual(2);

const accessAgg = formatted.aggregations.find(
const accessAgg = formatted.aggregations?.find(
(a) => a.field === "access"
);
expect(accessAgg).toEqual({
Expand All @@ -851,7 +854,7 @@ describe("hubSearchItems Module |", () => {
],
});

const typeAgg = formatted.aggregations.find((a) => a.field === "type");
const typeAgg = formatted.aggregations?.find((a) => a.field === "type");
expect(typeAgg).toEqual({
mode: "terms",
field: "type",
Expand Down Expand Up @@ -1012,9 +1015,9 @@ describe("hubSearchItems Module |", () => {
expect(response.results).toEqual([]);
expect(response.hasNext).toEqual(false),
// Test aggregation results
expect(response.aggregations.length).toEqual(2);
expect(response.aggregations?.length).toEqual(2);

const accessAgg = response.aggregations.find(
const accessAgg = response.aggregations?.find(
(a) => a.field === "access"
);
expect(accessAgg).toEqual({
Expand All @@ -1028,7 +1031,9 @@ describe("hubSearchItems Module |", () => {
],
});

const typeAgg = response.aggregations.find((a) => a.field === "type");
const typeAgg = response.aggregations?.find(
(a) => a.field === "type"
);
expect(typeAgg).toEqual({
mode: "terms",
field: "type",
Expand Down

0 comments on commit df53fb9

Please sign in to comment.