[8.16] [Search] [Playground] Bug get text field for semantic field (#197345) (#197813)

# Backport

This will backport the following commits from `main` to `8.16`:
- [[Search] [Playground] Bug get text field for semantic field
(#197345)](https://github.com/elastic/kibana/pull/197345)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Yan
Savitski","email":"yan.savitski@elastic.co"},"sourceCommit":{"committedDate":"2024-10-25T11:54:16Z","message":"[Search]
[Playground] Bug get text field for semantic field (#197345)\n\n- Fix
[Object object] in retrieval documents when semantic text is
used\r\n<img width=\"825\"
alt=\"image\"\r\nsrc=\"https://github.com/user-attachments/assets/39b154ac-727b-40c2-8262-4ff6f892a634\">","sha":"a224f9e14bcfc94db669c7e73dc97fc923326e36","branchLabelMapping":{"^v9.0.0$":"main","^v8.17.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","Team:Search","backport:prev-minor","v8.16.0"],"title":"[Search]
[Playground] Bug get text field for semantic
field","number":197345,"url":"https://github.com/elastic/kibana/pull/197345","mergeCommit":{"message":"[Search]
[Playground] Bug get text field for semantic field (#197345)\n\n- Fix
[Object object] in retrieval documents when semantic text is
used\r\n<img width=\"825\"
alt=\"image\"\r\nsrc=\"https://github.com/user-attachments/assets/39b154ac-727b-40c2-8262-4ff6f892a634\">","sha":"a224f9e14bcfc94db669c7e73dc97fc923326e36"}},"sourceBranch":"main","suggestedTargetBranches":["8.16"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/197345","number":197345,"mergeCommit":{"message":"[Search]
[Playground] Bug get text field for semantic field (#197345)\n\n- Fix
[Object object] in retrieval documents when semantic text is
used\r\n<img width=\"825\"
alt=\"image\"\r\nsrc=\"https://github.com/user-attachments/assets/39b154ac-727b-40c2-8262-4ff6f892a634\">","sha":"a224f9e14bcfc94db669c7e73dc97fc923326e36"}},{"branch":"8.16","label":"v8.16.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Yan Savitski <yan.savitski@elastic.co>
This commit is contained in:
Kibana Machine 2024-10-26 00:37:30 +11:00 committed by GitHub
parent af2cd0c47d
commit f34afbe425
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 17 additions and 2 deletions

View file

@ -123,4 +123,17 @@ describe('getValueForSelectedField', () => {
seeking consolation and, eventually, redemption through basic compassion"
`);
});
test('should return when path is semantic field', () => {
const hit = {
_index: 'sample-index',
_id: '8jSNY48B6iHEi98DL1C-',
_score: 0.7789394,
_source: {
test: { text: 'The Shawshank Redemption' },
},
};
expect(getValueForSelectedField(hit, 'test')).toEqual('The Shawshank Redemption');
});
});

View file

@ -6,7 +6,7 @@
*/
import { SearchHit } from '@elastic/elasticsearch/lib/api/types';
import { get } from 'lodash';
import { get, has } from 'lodash';
export const getValueForSelectedField = (hit: SearchHit, path: string): string => {
if (!hit) {
@ -21,5 +21,7 @@ export const getValueForSelectedField = (hit: SearchHit, path: string): string =
.join('\n --- \n');
}
return get(hit._source, path, '');
return has(hit._source, `${path}.text`)
? get(hit._source, `${path}.text`, '')
: get(hit._source, path, '');
};