-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]> Co-authored-by: Eunjae Lee <[email protected]>
- Loading branch information
1 parent
d5cb2ad
commit 8d962ea
Showing
12 changed files
with
186 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { encode } from '@algolia/client-common'; | ||
import { MethodEnum } from '@algolia/requester-common'; | ||
import { RequestOptions } from '@algolia/transporter'; | ||
|
||
import { FindAnswersOptions, FindAnswersResponse, SearchIndex } from '../..'; | ||
|
||
export const findAnswers = (base: SearchIndex) => { | ||
return <TObject>( | ||
query: string, | ||
queryLanguages: readonly string[], | ||
requestOptions?: RequestOptions & FindAnswersOptions | ||
): Readonly<Promise<FindAnswersResponse<TObject>>> => { | ||
return base.transporter.read( | ||
{ | ||
method: MethodEnum.Post, | ||
path: encode('1/answers/%s/prediction', base.indexName), | ||
data: { | ||
query, | ||
queryLanguages, | ||
}, | ||
cacheable: true, | ||
}, | ||
requestOptions | ||
); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { SearchOptions } from './SearchOptions'; | ||
|
||
export type FindAnswersOptions = { | ||
readonly attributesForPrediction?: readonly string[]; | ||
readonly nbHits?: number; | ||
readonly threshold?: number; | ||
readonly params?: SearchOptions; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Hit } from './Hit'; | ||
import { SearchResponse } from './SearchResponse'; | ||
|
||
export type FindAnswersResponse<TObject = {}> = SearchResponse<TObject> & { | ||
/** | ||
* The hits returned by the search. | ||
* | ||
* Hits are ordered according to the ranking or sorting of the index being queried. | ||
*/ | ||
hits: Array< | ||
Hit< | ||
TObject & { | ||
_answer?: { | ||
extract: string; | ||
score: number; | ||
extractAttribute: string; | ||
}; | ||
} | ||
> | ||
>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"singleQuote": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import algolia from "../packages/algoliasearch/dist/algoliasearch"; | ||
declare const algoliasearch: typeof algolia; | ||
|
||
declare const browser: { | ||
executeAsync<TArg, TResult>( | ||
cb: (arg: TArg, done: (res: TResult) => TResult) => void, | ||
arg: TArg | ||
): TResult; | ||
|
||
url(to: string): void; | ||
}; | ||
|
||
const credentials = { | ||
appId: "CKOEQ4XGMU", | ||
apiKey: "6560d3886292a5aec86d63b9a2cba447" | ||
// TODO: change these credentials to the main ones once enabled on our app | ||
// appId: `${process.env.ALGOLIA_APPLICATION_ID_1}`, | ||
// apiKey: `${process.env.ALGOLIA_SEARCH_KEY_1}` | ||
}; | ||
|
||
describe("answers features - algoliasearch.com", () => { | ||
beforeEach(async () => browser.url("algoliasearch.com")); | ||
|
||
it("searchIndex::findAnswers", async () => { | ||
const results: any = await browser.executeAsync(function( | ||
credentials, | ||
done | ||
) { | ||
const client = algoliasearch(credentials.appId, credentials.apiKey); | ||
|
||
// TODO: remove this customization once the engine accepts url encoded query params | ||
client.transporter.userAgent.value = "answers-test"; | ||
|
||
const index = client.initIndex("ted"); | ||
|
||
Promise.all([ | ||
index.findAnswers("sir ken robinson", ["en"]), | ||
index.findAnswers("what", ["en"]), | ||
index.findAnswers("sarah jones", ["en"], { | ||
nbHits: 2, | ||
attributesForPrediction: ["main_speaker"], | ||
params: { | ||
highlightPreTag: "_pre_", | ||
highlightPostTag: "_post_" | ||
} | ||
}) | ||
]).then(function(responses) { | ||
done({ | ||
kenRobinson: responses[0], | ||
what: responses[1], | ||
sarah: responses[2] | ||
}); | ||
}); | ||
}, | ||
credentials); | ||
|
||
expect(results.kenRobinson.nbHits).toBe(10); | ||
|
||
expect(results.what.nbHits).toBe(0); | ||
|
||
expect(results.sarah.nbHits).toBe(1); | ||
expect(results.sarah.hits[0]._highlightResult.main_speaker.value).toBe( | ||
"_pre_Sarah_post_ _pre_Jones_post_" | ||
); | ||
|
||
expect(results.sarah.hits[0]._answer.extract).toBe( | ||
"_pre_Sarah_post_ _pre_Jones_post_" | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters