-
Notifications
You must be signed in to change notification settings - Fork 224
feat(findAnswers): implement the new method #1219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4705495
feat(searchForAnswers): implement the new method
Haroenv 5e3196f
make test pass
Haroenv 8d26b01
chore: Rename to findAnswers
PLNech fc5047b
fix(test): Remove superfluous newline in url
PLNech faf79b6
chore: Order keys after renaming [ci skip]
PLNech 68630b2
add newline
Haroenv a7c5a20
restructure tests
Haroenv 7052bc2
update bundlesize
Haroenv a185aa4
chore: don't omit any search parameters for now
Haroenv 663adbf
Merge branch 'master' into feat/answers
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Haroenv to be clear, are we waiting for the engine fix, so that we can get rid of this line before merging this PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @eunjae-lee, the engine fix won't be happening soon, so I'd rather merge this and prepare a v1.1 PR for when the engine has catched up :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
got it. thanks for clarification!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI, issue opened here :)