From 979806a99feb03dc2f1ab3510b6e58ae9a6dbc45 Mon Sep 17 00:00:00 2001 From: Yan Savitski Date: Tue, 22 Oct 2024 23:51:10 +0200 Subject: [PATCH 1/3] Get text field for semantic field --- .../server/utils/get_value_for_selected_field.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/search_playground/server/utils/get_value_for_selected_field.ts b/x-pack/plugins/search_playground/server/utils/get_value_for_selected_field.ts index 695c56a571374..5556e407de979 100644 --- a/x-pack/plugins/search_playground/server/utils/get_value_for_selected_field.ts +++ b/x-pack/plugins/search_playground/server/utils/get_value_for_selected_field.ts @@ -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, ''); }; From fa1efcacf0e6dd97d3f45a7a2a3447cd0d869e44 Mon Sep 17 00:00:00 2001 From: Yan Savitski Date: Wed, 23 Oct 2024 22:09:14 +0200 Subject: [PATCH 2/3] Add test for semantic field --- .../utils/get_value_for_selected_field.test.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/search_playground/server/utils/get_value_for_selected_field.test.ts b/x-pack/plugins/search_playground/server/utils/get_value_for_selected_field.test.ts index cfdefc99f80ef..12da5164502b9 100644 --- a/x-pack/plugins/search_playground/server/utils/get_value_for_selected_field.test.ts +++ b/x-pack/plugins/search_playground/server/utils/get_value_for_selected_field.test.ts @@ -117,10 +117,23 @@ describe('getValueForSelectedField', () => { expect(getValueForSelectedField(hit as any, 'test')).toMatchInlineSnapshot(` "Over the course of several years - --- + --- two convicts form a friendship - --- + --- 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'); + }); }); From ded160a5494101740f5afc8c625c6867420f2e6f Mon Sep 17 00:00:00 2001 From: Yan Savitski Date: Thu, 24 Oct 2024 18:59:50 +0200 Subject: [PATCH 3/3] Update jest inline snapshot --- .../server/utils/get_value_for_selected_field.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/search_playground/server/utils/get_value_for_selected_field.test.ts b/x-pack/plugins/search_playground/server/utils/get_value_for_selected_field.test.ts index 12da5164502b9..7eae929cc70c0 100644 --- a/x-pack/plugins/search_playground/server/utils/get_value_for_selected_field.test.ts +++ b/x-pack/plugins/search_playground/server/utils/get_value_for_selected_field.test.ts @@ -117,9 +117,9 @@ describe('getValueForSelectedField', () => { expect(getValueForSelectedField(hit as any, 'test')).toMatchInlineSnapshot(` "Over the course of several years - --- + --- two convicts form a friendship - --- + --- seeking consolation and, eventually, redemption through basic compassion" `); });