Skip to content

Commit

Permalink
search-core: add support for FunctionVertical (#271)
Browse files Browse the repository at this point in the history
This PR adds a Source enum type for Function Vertical, updates ResultFactory.ts to handle a function vertical response, and adds auto test.

Test: manual, auto

Tested with auto test and through test site, ensuring function vertical data appear in the response.

---------

Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
2 people authored and Fondryext committed Dec 9, 2024
1 parent 7026542 commit 74818b9
Show file tree
Hide file tree
Showing 14 changed files with 94 additions and 12 deletions.
13 changes: 13 additions & 0 deletions docs/search-core.source.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,20 @@ export declare enum Source

## Enumeration Members

<<<<<<< HEAD
<table><thead><tr><th>
=======
| Member | Value | Description |
| --- | --- | --- |
| Algolia | <code>&quot;ALGOLIA&quot;</code> | The result is from Algolia. |
| Bing | <code>&quot;BING_CSE&quot;</code> | The result is from Bing Search Engine. |
| Custom | <code>&quot;CUSTOM_SEARCHER&quot;</code> | The result was from a custom source. |
| DocumentVertical | <code>&quot;DOCUMENT_VERTICAL&quot;</code> | The result is from a document vertical. |
| FunctionVertical | <code>&quot;FUNCTION_VERTICAL&quot;</code> | The result is from a function vertical. |
| Google | <code>&quot;GOOGLE_CSE&quot;</code> | The result is from Google Custom Search Engine. |
| KnowledgeManager | <code>&quot;KNOWLEDGE_MANAGER&quot;</code> | The result is from a Knowledge Graph. |
| Zendesk | <code>&quot;ZENDESK&quot;</code> | The result is from Zendesk. |
>>>>>>> 97e068e (search-core: add support for FunctionVertical (#271))

Member

Expand Down
1 change: 1 addition & 0 deletions etc/search-core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,7 @@ export enum SortType {
export enum Source {
Custom = "CUSTOM_SEARCHER",
DocumentVertical = "DOCUMENT_VERTICAL",
FunctionVertical = "FUNCTION_VERTICAL",
Google = "GOOGLE_CSE",
KnowledgeManager = "KNOWLEDGE_MANAGER"
}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@yext/search-core",
"version": "2.6.0-beta.2",
"version": "2.6.0-beta.3",
"description": "Typescript Networking Library for the Yext Search API",
"main": "./dist/commonjs/src/index.js",
"module": "./dist/esm/src/index.js",
Expand Down
2 changes: 2 additions & 0 deletions src/models/searchservice/response/Source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ export enum Source {
Custom = 'CUSTOM_SEARCHER',
/** The result is from a document vertical. */
DocumentVertical = 'DOCUMENT_VERTICAL',
/** The result is from a function vertical. */
FunctionVertical = 'FUNCTION_VERTICAL',
}
12 changes: 11 additions & 1 deletion src/transformers/searchservice/ResultsFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export class ResultsFactory {
return this.fromGoogleCustomSearchEngine(result, resultIndex);
case Source.DocumentVertical:
return this.fromDocumentVertical(result, resultIndex);
case Source.FunctionVertical:
return this.fromFunctionVertical(result, resultIndex);
default:
return this.fromCustomSource(result, resultIndex);
}
Expand Down Expand Up @@ -72,10 +74,18 @@ export class ResultsFactory {
}

private static fromCustomSource(result: any, index: number): Result {
return this.fromCustomDataHelper(result, index, Source.Custom);
}

private static fromFunctionVertical(result: any, index: number): Result {
return this.fromCustomDataHelper(result, index, Source.FunctionVertical);
}

private static fromCustomDataHelper(result: any, index: number, source: Source): Result {
const rawData = result.data ?? result;
return {
rawData: rawData,
source: Source.Custom,
source: source,
index: index,
name: rawData.name,
description: rawData.description, // Do we want to truncate this like in the SDK?
Expand Down
1 change: 1 addition & 0 deletions test-site/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
<button onclick="verticalAutocomplete()">Vertical Autocomplete</button>
<button onclick="filterSearch()">Filter Search</button>
<button onclick="generativeDirectAnswer()">Generative Direct Answer</button>
<button onclick="functionVerticalSearch()">Function Vertical Search</button>
</body>
</html>
1 change: 1 addition & 0 deletions test-site/js-index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
<button onclick="verticalAutocomplete()">Vertical Autocomplete</button>
<button onclick="filterSearch()">Filter Search</button>
<button onclick="generativeDirectAnswer()">Generative Direct Answer</button>
<button onclick="functionVerticalSearch()">Function Vertical Search</button>
</body>
</html>
2 changes: 1 addition & 1 deletion test-site/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion test-site/src/js/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import { provideCore } from '@yext/search-core';
import verticalRequest from './requests/verticalRequest';
import { verticalRequest, functionVerticalRequest } from './requests/verticalRequest';
import universalRequest from './requests/universalRequest';
import questionRequest from './requests/questionRequest';
import { univeralAutocompleteRequest, verticalAutocompleteRequest, filterSearchRequest } from './requests/autocompleteRequests';
Expand Down Expand Up @@ -69,6 +69,13 @@ export async function generativeDirectAnswer() {
updateUI(data, startTime, 'Core Generative Direct Answer Response:');
}

export async function functionVerticalSearch() {
loadingSpinner();
const startTime = new Date().getTime();
const results = await globalCore.verticalSearch(functionVerticalRequest);
updateUI(results, startTime, 'Core Function Vertical Response:');
}

function loadingSpinner() {
element.textContent = 'Loading...';
}
Expand Down
7 changes: 5 additions & 2 deletions test-site/src/js/requests/verticalRequest.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { QueryTrigger } from '@yext/search-core';

const verticalRequest = {
export const verticalRequest = {
verticalKey: 'people',
query: 'virginia',
queryTrigger: QueryTrigger.Initialize,
Expand All @@ -12,4 +12,7 @@ const verticalRequest = {
referrerPageUrl: 'www.google.com/answers/not/ads'
};

export default verticalRequest;
export const functionVerticalRequest = {
verticalKey: 'function_vertical',
query: 'virginia',
};
11 changes: 9 additions & 2 deletions test-site/src/ts/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { provideCore, SearchConfig, SearchCore, UniversalSearchRequest, UniversalSearchResponse } from '@yext/search-core';
import verticalRequest from './requests/verticalRequest';
import { verticalRequest, functionVerticalRequest } from './requests/verticalRequest';
import universalRequest from './requests/universalRequest';
import questionRequest from './requests/questionRequest';
import { univeralAutocompleteRequest, verticalAutocompleteRequest, filterSearchRequest } from './requests/autocompleteRequests';
Expand All @@ -8,7 +8,7 @@ import initDirectAnswers from './initDirectAnswers';

const coreConfig: SearchConfig = {
apiKey: process.env.API_KEY,
experienceKey: 'slanswers',
experienceKey: 'developer-support-test',
locale: 'en',
experienceVersion: 'PRODUCTION',
};
Expand Down Expand Up @@ -44,6 +44,13 @@ export async function verticalSearch(): Promise<void> {
updateUI(results, startTime, 'Core Vertical Response:');
}

export async function functionVerticalSearch(): Promise<void> {
loadingSpinner();
const startTime = new Date().getTime();
const results = await globalCore.verticalSearch(functionVerticalRequest);
updateUI(results, startTime, 'Core Function Vertical Response:');
}

export async function submitQuestion(): Promise<void> {
loadingSpinner();
const startTime = new Date().getTime();
Expand Down
7 changes: 5 additions & 2 deletions test-site/src/ts/requests/verticalRequest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { QueryTrigger, VerticalSearchRequest } from '@yext/search-core';

const verticalRequest: VerticalSearchRequest = {
export const verticalRequest: VerticalSearchRequest = {
verticalKey: 'people',
query: 'virginia',
queryTrigger: QueryTrigger.Initialize,
Expand All @@ -17,4 +17,7 @@ const verticalRequest: VerticalSearchRequest = {
}
};

export default verticalRequest;
export const functionVerticalRequest: VerticalSearchRequest = {
verticalKey: 'function_vertical',
query: 'virginia',
};
34 changes: 34 additions & 0 deletions tests/transformers/searchservice/ResultsFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,37 @@ it('properly transforms backend results from custom source', () => {
const actualResults = ResultsFactory.create(genericData, Source.Custom);
expect(expectedResults).toMatchObject(actualResults);
});

it('properly transforms backend results from function vertical', () => {
const genericData = [{
data: {
answer: 'You should still self\-quarantine for 14 days since your last exposure.',
c_organization: 'CDC',
id: 'iwasaroundsomeonewhohascovid19andmycovid19testcame',
website: 'https://www.cdc.gov/coronavirus/2019-ncov/faq.html',
name: 'Do I still need to quarantine for 14 days?',
description: 'COVID question 2'
},
highlightedFields: {}
}];

const expectedResults = [{
description: 'COVID question 2',
id: 'iwasaroundsomeonewhohascovid19andmycovid19testcame',
index: 1,
link: 'https://www.cdc.gov/coronavirus/2019-ncov/faq.html',
name: 'Do I still need to quarantine for 14 days?',
rawData: {
answer: 'You should still self\-quarantine for 14 days since your last exposure.',
c_organization: 'CDC',
id: 'iwasaroundsomeonewhohascovid19andmycovid19testcame',
website: 'https://www.cdc.gov/coronavirus/2019-ncov/faq.html',
name: 'Do I still need to quarantine for 14 days?',
description: 'COVID question 2'
},
source: Source.FunctionVertical
}];

const actualResults = ResultsFactory.create(genericData, Source.FunctionVertical);
expect(expectedResults).toMatchObject(actualResults);
});

0 comments on commit 74818b9

Please sign in to comment.