Skip to content

Commit

Permalink
feat(hub-common): add default predicate for portal search (#1315)
Browse files Browse the repository at this point in the history
* feat(hub-common): add default predicate for portal search

* fix(hub-common): move function to add default predicate to util

* fix(hub-common): update tests to get full coverage

* fix(hub-common): remove check for item entity when adding default search predicates

* fix(hub-common): remove test to check if default predicates are added for non-item
  • Loading branch information
sansth1010 authored Nov 6, 2023
1 parent f3cf4a5 commit 6a42e63
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
5 changes: 3 additions & 2 deletions packages/common/src/search/_internal/portalSearchItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
IPredicate,
IQuery,
} from "../types";
import { getNextFunction } from "../utils";
import { addDefaultItemSearchPredicates, getNextFunction } from "../utils";
import { convertPortalAggregations } from "./portalSearchUtils";
import { expandPredicate } from "./expandPredicate";
import HubError from "../../HubError";
Expand All @@ -37,7 +37,8 @@ export async function portalSearchItems(
query: IQuery,
options: IHubSearchOptions
): Promise<IHubSearchResponse<IHubSearchResult>> {
const so = processSearchParams(options, query);
const queryWithDefaultPredicates = addDefaultItemSearchPredicates(query);
const so = processSearchParams(options, queryWithDefaultPredicates);
return searchPortalAsHubSearchResult(so);
}

Expand Down
19 changes: 19 additions & 0 deletions packages/common/src/search/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,22 @@ export function getResultSiteRelativeLink(
}
return siteRelativeLink;
}

/**
* Adds default predicates for item target entity
*
* @param query IQuery to search items
* @returns a cloned copy of the query object with default item search predicates
*/
export function addDefaultItemSearchPredicates(query: IQuery): IQuery {
const queryWithDefaultItemPredicates: IQuery = cloneObject(query);
const defaultPredicates = {
// 'Code Attachment' is an old AGO type that has
// been defunct for some time, so add this predicate
// to all catalog filter to omit 'Code Attachment' items
// from search results
predicates: [{ type: { not: ["Code Attachment"] } }],
};
queryWithDefaultItemPredicates.filters.push(defaultPredicates);
return queryWithDefaultItemPredicates;
}
15 changes: 10 additions & 5 deletions packages/common/test/search/_internal/portalSearchItems.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ describe("portalSearchItems Module:", () => {
expect(searchItemsSpy.calls.count()).toBe(1, "should call searchItems");
const [expectedParams] = searchItemsSpy.calls.argsFor(0);
expect(expectedParams.portal).toEqual(opts.requestOptions?.portal);
expect(expectedParams.q).toEqual("(water)");
// 'Code Attachment' comes from default item predicate
expect(expectedParams.q).toEqual('(water) AND (-type:"Code Attachment")');
expect(expectedParams.countFields).not.toBeDefined();
});
it("simple search with bbox", async () => {
Expand Down Expand Up @@ -107,7 +108,8 @@ describe("portalSearchItems Module:", () => {
expect(searchItemsSpy.calls.count()).toBe(1, "should call searchItems");
const [expectedParams] = searchItemsSpy.calls.argsFor(0);
expect(expectedParams.portal).toEqual(opts.requestOptions?.portal);
expect(expectedParams.q).toEqual("(water)");
// 'Code Attachment' comes from default item predicate
expect(expectedParams.q).toEqual('(water) AND (-type:"Code Attachment")');
expect(expectedParams.params.bbox).toEqual("1,2,3,4");
expect(expectedParams.countFields).not.toBeDefined();
});
Expand Down Expand Up @@ -139,7 +141,8 @@ describe("portalSearchItems Module:", () => {
expect(searchItemsSpy.calls.count()).toBe(1, "should call searchItems");
const [expectedParams] = searchItemsSpy.calls.argsFor(0);
expect(expectedParams.portal).toBeUndefined();
expect(expectedParams.q).toEqual("(water)");
// 'Code Attachment' comes from default item predicate
expect(expectedParams.q).toEqual('(water) AND (-type:"Code Attachment")');
expect(expectedParams.authentication).toEqual(
opts.requestOptions?.authentication
);
Expand Down Expand Up @@ -173,7 +176,8 @@ describe("portalSearchItems Module:", () => {
expect(searchItemsSpy.calls.count()).toBe(1, "should call searchItems");
const [expectedParams] = searchItemsSpy.calls.argsFor(0);
// verify q
expect(expectedParams.q).toEqual("(water)");
// 'Code Attachment' comes from default item predicate
expect(expectedParams.q).toEqual('(water) AND (-type:"Code Attachment")');
expect(expectedParams.countFields).toEqual("tags");
expect(expectedParams.countSize).toEqual(10);
});
Expand Down Expand Up @@ -206,7 +210,8 @@ describe("portalSearchItems Module:", () => {
expect(searchItemsSpy.calls.count()).toBe(1, "should call searchItems");
const [expectedParams] = searchItemsSpy.calls.argsFor(0);
// verify q
expect(expectedParams.q).toEqual("(water)");
// 'Code Attachment' comes from default item predicate
expect(expectedParams.q).toEqual('(water) AND (-type:"Code Attachment")');
expect(expectedParams.countFields).toEqual("tags");
expect(expectedParams.countSize).toEqual(100);
expect(expectedParams.portal).toEqual(
Expand Down

0 comments on commit 6a42e63

Please sign in to comment.