Skip to content

Commit 8d962ea

Browse files
HaroenvPLNechEunjae Lee
authored
feat(findAnswers): implement the new method (experimental) (#1219)
* feat(searchForAnswers): implement the new method - adds searchForAnswers to node / browser (not lite for now) - fix small typo in SearchOptions - add an integration test * make test pass * chore: Rename to findAnswers * fix(test): Remove superfluous newline in url * chore: Order keys after renaming [ci skip] * add newline * restructure tests * update bundlesize * chore: don't omit any search parameters for now Co-authored-by: Paul-Louis NECH <pln@algolia.com> Co-authored-by: Eunjae Lee <eunjae.lee@algolia.com>
1 parent d5cb2ad commit 8d962ea

File tree

12 files changed

+186
-15
lines changed

12 files changed

+186
-15
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
"bundlesize": [
9999
{
100100
"path": "packages/algoliasearch/dist/algoliasearch.umd.js",
101-
"maxSize": "7.57KB"
101+
"maxSize": "7.65KB"
102102
},
103103
{
104104
"path": "packages/algoliasearch/dist/algoliasearch-lite.umd.js",

packages/algoliasearch/src/builds/browser.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ import {
6868
deleteSynonym,
6969
DeleteSynonymOptions,
7070
exists,
71+
findAnswers,
72+
FindAnswersOptions,
73+
FindAnswersResponse,
7174
findObject,
7275
FindObjectOptions,
7376
FindObjectResponse,
@@ -237,6 +240,7 @@ export default function algoliasearch(
237240
methods: {
238241
batch,
239242
delete: deleteIndex,
243+
findAnswers,
240244
getObject,
241245
getObjects,
242246
saveObject,
@@ -345,6 +349,11 @@ export type SearchIndex = BaseSearchIndex & {
345349
query: string,
346350
requestOptions?: RequestOptions & SearchOptions
347351
) => Readonly<Promise<SearchResponse<TObject>>>;
352+
readonly findAnswers: (
353+
query: string,
354+
queryLanguages: readonly string[],
355+
requestOptions?: RequestOptions & FindAnswersOptions
356+
) => Readonly<Promise<FindAnswersResponse>>;
348357
readonly searchForFacetValues: (
349358
facetName: string,
350359
facetQuery: string,

packages/algoliasearch/src/builds/node.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ import {
6767
deleteSynonym,
6868
DeleteSynonymOptions,
6969
exists,
70+
findAnswers,
71+
FindAnswersOptions,
72+
FindAnswersResponse,
7073
findObject,
7174
FindObjectOptions,
7275
FindObjectResponse,
@@ -240,6 +243,7 @@ export default function algoliasearch(
240243
methods: {
241244
batch,
242245
delete: deleteIndex,
246+
findAnswers,
243247
getObject,
244248
getObjects,
245249
saveObject,
@@ -353,6 +357,11 @@ export type SearchIndex = BaseSearchIndex & {
353357
facetQuery: string,
354358
requestOptions?: RequestOptions & SearchOptions
355359
) => Readonly<Promise<SearchForFacetValuesResponse>>;
360+
readonly findAnswers: (
361+
query: string,
362+
queryLanguages: readonly string[],
363+
requestOptions?: RequestOptions & FindAnswersOptions
364+
) => Readonly<Promise<FindAnswersResponse>>;
356365
readonly batch: (
357366
requests: readonly BatchRequest[],
358367
requestOptions?: RequestOptions
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { encode } from '@algolia/client-common';
2+
import { MethodEnum } from '@algolia/requester-common';
3+
import { RequestOptions } from '@algolia/transporter';
4+
5+
import { FindAnswersOptions, FindAnswersResponse, SearchIndex } from '../..';
6+
7+
export const findAnswers = (base: SearchIndex) => {
8+
return <TObject>(
9+
query: string,
10+
queryLanguages: readonly string[],
11+
requestOptions?: RequestOptions & FindAnswersOptions
12+
): Readonly<Promise<FindAnswersResponse<TObject>>> => {
13+
return base.transporter.read(
14+
{
15+
method: MethodEnum.Post,
16+
path: encode('1/answers/%s/prediction', base.indexName),
17+
data: {
18+
query,
19+
queryLanguages,
20+
},
21+
cacheable: true,
22+
},
23+
requestOptions
24+
);
25+
};
26+
};

packages/client-search/src/methods/index/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export * from './deleteObjects';
1717
export * from './deleteRule';
1818
export * from './deleteSynonym';
1919
export * from './exists';
20+
export * from './findAnswers';
2021
export * from './findObject';
2122
export * from './getObject';
2223
export * from './getObjectPosition';
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { SearchOptions } from './SearchOptions';
2+
3+
export type FindAnswersOptions = {
4+
readonly attributesForPrediction?: readonly string[];
5+
readonly nbHits?: number;
6+
readonly threshold?: number;
7+
readonly params?: SearchOptions;
8+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Hit } from './Hit';
2+
import { SearchResponse } from './SearchResponse';
3+
4+
export type FindAnswersResponse<TObject = {}> = SearchResponse<TObject> & {
5+
/**
6+
* The hits returned by the search.
7+
*
8+
* Hits are ordered according to the ranking or sorting of the index being queried.
9+
*/
10+
hits: Array<
11+
Hit<
12+
TObject & {
13+
_answer?: {
14+
extract: string;
15+
score: number;
16+
extractAttribute: string;
17+
};
18+
}
19+
>
20+
>;
21+
};

packages/client-search/src/types/SearchOptions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ export type SearchOptions = {
312312
/**
313313
* List of supported languages with their associated language ISO code.
314314
*
315-
* Apply a set of natural language best practices such asignorePlurals,
315+
* Apply a set of natural language best practices such as ignorePlurals,
316316
* removeStopWords, removeWordsIfNoResults, analyticsTags and ruleContexts.
317317
*/
318318
readonly naturalLanguages?: readonly string[];

packages/client-search/src/types/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ export * from './DeleteByFiltersOptions';
2525
export * from './DeleteResponse';
2626
export * from './DeleteSynonymOptions';
2727
export * from './FacetHit';
28+
export * from './FindAnswersOptions';
29+
export * from './FindAnswersResponse';
2830
export * from './FindObjectOptions';
2931
export * from './FindObjectResponse';
3032
export * from './GetApiKeyResponse';

specs/.prettierrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"singleQuote": false
3+
}

0 commit comments

Comments
 (0)