-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[App Search] Wired up existing promoted documents on suggestion view #113967
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
Changes from all commits
ce00e7e
e465f3e
8b36bd4
a084d0d
46544c1
15f23fd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,30 +6,35 @@ | |
| */ | ||
|
|
||
| import { kea, MakeLogicType } from 'kea'; | ||
| import { HttpSetup } from 'kibana/public'; | ||
|
|
||
| import { flashAPIErrors } from '../../../../../shared/flash_messages'; | ||
| import { HttpLogic } from '../../../../../shared/http'; | ||
| import { EngineLogic } from '../../../engine'; | ||
| import { Result } from '../../../result/types'; | ||
| import { CurationSuggestion } from '../../types'; | ||
| import { Curation, CurationSuggestion } from '../../types'; | ||
|
|
||
| interface CurationSuggestionValues { | ||
| dataLoading: boolean; | ||
| suggestion: CurationSuggestion | null; | ||
| suggestedPromotedDocuments: Result[]; | ||
| curation: Curation | null; | ||
| } | ||
|
|
||
| interface CurationSuggestionActions { | ||
| loadSuggestion(): void; | ||
| onSuggestionLoaded({ | ||
| suggestion, | ||
| suggestedPromotedDocuments, | ||
| curation, | ||
| }: { | ||
| suggestion: CurationSuggestion; | ||
| suggestedPromotedDocuments: Result[]; | ||
| curation: Curation; | ||
| }): { | ||
| suggestion: CurationSuggestion; | ||
| suggestedPromotedDocuments: Result[]; | ||
| curation: Curation; | ||
| }; | ||
| } | ||
|
|
||
|
|
@@ -43,9 +48,10 @@ export const CurationSuggestionLogic = kea< | |
| path: ['enterprise_search', 'app_search', 'curations', 'suggestion_logic'], | ||
| actions: () => ({ | ||
| loadSuggestion: true, | ||
| onSuggestionLoaded: ({ suggestion, suggestedPromotedDocuments }) => ({ | ||
| onSuggestionLoaded: ({ suggestion, suggestedPromotedDocuments, curation }) => ({ | ||
| suggestion, | ||
| suggestedPromotedDocuments, | ||
| curation, | ||
| }), | ||
| }), | ||
| reducers: () => ({ | ||
|
|
@@ -68,64 +74,92 @@ export const CurationSuggestionLogic = kea< | |
| onSuggestionLoaded: (_, { suggestedPromotedDocuments }) => suggestedPromotedDocuments, | ||
| }, | ||
| ], | ||
| curation: [ | ||
| null, | ||
| { | ||
| onSuggestionLoaded: (_, { curation }) => curation, | ||
| }, | ||
| ], | ||
| }), | ||
| listeners: ({ actions, props }) => ({ | ||
| loadSuggestion: async () => { | ||
| const { http } = HttpLogic.values; | ||
| const { engineName } = EngineLogic.values; | ||
|
|
||
| try { | ||
| const response = await http.post( | ||
| `/internal/app_search/engines/${engineName}/search_relevance_suggestions/${props.query}`, | ||
| { | ||
| body: JSON.stringify({ | ||
| page: { | ||
| current: 1, | ||
| size: 1, | ||
| }, | ||
| filters: { | ||
| status: ['pending'], | ||
| type: 'curation', | ||
| }, | ||
| }), | ||
| } | ||
| ); | ||
| const suggestion = await getSuggestions(http, engineName, props.query); | ||
| const promotedIds: string[] = suggestion.promoted; | ||
| const documentDetailsResopnse = getDocumentDetails(http, engineName, promotedIds); | ||
|
|
||
| const suggestion = response.results[0]; | ||
| let promises = [documentDetailsResopnse]; | ||
| if (suggestion.curation_id) { | ||
| promises = [...promises, getCuration(http, engineName, suggestion.curation_id)]; | ||
| } | ||
|
|
||
| const searchResponse = await http.post( | ||
| `/internal/app_search/engines/${engineName}/search`, | ||
| { | ||
| query: { query: '' }, | ||
| body: JSON.stringify({ | ||
| page: { | ||
| size: 100, | ||
| }, | ||
| filters: { | ||
| id: suggestion.promoted, | ||
| }, | ||
| }), | ||
| } | ||
| ); | ||
| const [documentDetails, curation] = await Promise.all(promises); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great solution! |
||
|
|
||
| // Filter out docs that were not found and maintain promoted order | ||
| const promotedIds: string[] = suggestion.promoted; | ||
| const documentDetails = searchResponse.results; | ||
| const suggestedPromotedDocuments = promotedIds.reduce((acc: Result[], id: string) => { | ||
| const found = documentDetails.find( | ||
| const found = documentDetails.results.find( | ||
| (documentDetail: Result) => documentDetail.id.raw === id | ||
| ); | ||
| if (!found) return acc; | ||
| return [...acc, found]; | ||
| }, []); | ||
|
|
||
| actions.onSuggestionLoaded({ | ||
| suggestion: suggestion as CurationSuggestion, | ||
| suggestion, | ||
| suggestedPromotedDocuments, | ||
| curation: curation || null, | ||
| }); | ||
| } catch (e) { | ||
| flashAPIErrors(e); | ||
| } | ||
| }, | ||
| }), | ||
| }); | ||
|
|
||
| const getSuggestions = async ( | ||
| http: HttpSetup, | ||
| engineName: string, | ||
| query: string | ||
| ): Promise<CurationSuggestion> => { | ||
| const response = await http.post( | ||
| `/internal/app_search/engines/${engineName}/search_relevance_suggestions/${query}`, | ||
| { | ||
| body: JSON.stringify({ | ||
| page: { | ||
| current: 1, | ||
| size: 1, | ||
| }, | ||
| filters: { | ||
| status: ['pending'], | ||
| type: 'curation', | ||
| }, | ||
| }), | ||
| } | ||
| ); | ||
|
|
||
| const suggestion = response.results[0] as CurationSuggestion; | ||
| return suggestion; | ||
| }; | ||
|
|
||
| const getDocumentDetails = async (http: HttpSetup, engineName: string, documentIds: string[]) => { | ||
| return http.post(`/internal/app_search/engines/${engineName}/search`, { | ||
| query: { query: '' }, | ||
| body: JSON.stringify({ | ||
| page: { | ||
| size: 100, | ||
| }, | ||
| filters: { | ||
| id: documentIds, | ||
| }, | ||
| }), | ||
| }); | ||
| }; | ||
|
|
||
| const getCuration = async (http: HttpSetup, engineName: string, curationId: string) => { | ||
| return http.get(`/internal/app_search/engines/${engineName}/curations/${curationId}`, { | ||
| query: { skip_record_analytics: 'true' }, | ||
| }); | ||
| }; | ||
|
Comment on lines
+147
to
+165
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Worth testing these?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nah, there's no logic to them, I just pulled them out for readability. |
||
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.
I'd rather this was in a SCSS file but 🤷