Skip to content

Commit

Permalink
Type fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
efegurkan committed Oct 1, 2024
1 parent d8353b3 commit c3a1692
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { i18n } from '@kbn/i18n';

import { FormattedMessage, FormattedNumber } from '@kbn/i18n-react';

import { resultMetaData, resultToField } from './result/result_metadata';
import { resultMetaData, resultToFieldFromMappingResponse } from './result/result_metadata';

import { Result } from '..';
import { type ResultProps } from './result/result';
Expand Down Expand Up @@ -106,7 +106,7 @@ export const DocumentList: React.FC<DocumentListProps> = ({
return (
<React.Fragment key={doc._id}>
<Result
fields={resultToField(doc, mappings)}
fields={resultToFieldFromMappingResponse(doc, mappings)}
metaData={resultMetaData(doc)}
onDocumentClick={onDocumentClick ? () => onDocumentClick(doc) : undefined}
{...resultProps}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@
*/

export { Result } from './result';
export { resultMetaData, resultToField } from './result_metadata';
export {
resultMetaData,
resultToFieldFromMappingResponse,
resultToFieldFromMappings as resultToField,
} from './result_metadata';
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import type { IndicesGetMappingResponse, SearchHit } from '@elastic/elasticsearch/lib/api/types';
import type {
IndicesGetMappingResponse,
MappingProperty,
SearchHit,
} from '@elastic/elasticsearch/lib/api/types';
import type { MetaDataProps } from './result_types';

const TITLE_KEYS = ['title', 'name'];
Expand Down Expand Up @@ -40,7 +44,10 @@ export const resultMetaData = (result: SearchHit): MetaDataProps => ({
score: result._score,
});

export const resultToField = (result: SearchHit, mappings?: IndicesGetMappingResponse) => {
export const resultToFieldFromMappingResponse = (
result: SearchHit,
mappings?: IndicesGetMappingResponse
) => {
if (mappings && mappings[result._index] && result._source && !Array.isArray(result._source)) {
if (typeof result._source === 'object') {
return Object.entries(result._source).map(([key, value]) => {
Expand All @@ -54,3 +61,19 @@ export const resultToField = (result: SearchHit, mappings?: IndicesGetMappingRes
}
return [];
};

export const resultToFieldFromMappings = (
result: SearchHit,
mappings?: Record<string, MappingProperty>
) => {
if (mappings && result._source && !Array.isArray(result._source)) {
return Object.entries(result._source).map(([key, value]) => {
return {
fieldName: key,
fieldType: mappings[key]?.type ?? 'object',
fieldValue: JSON.stringify(value, null, 2),
};
});
}
return [];
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import React from 'react';
import { MappingProperty, SearchHit } from '@elastic/elasticsearch/lib/api/types';

import { Result, resultToField, resultMetaData } from '@kbn/search-index-documents';
import { Result, resultMetaData, resultToField } from '@kbn/search-index-documents';

import { EuiSpacer } from '@elastic/eui';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ export const SearchMode: React.FC = () => {
mappings={mappingData}
pagination={pagination}
onPaginationChange={onPagination}
searchQuery={searchQuery.query}
/>
) : (
<EuiEmptyPrompt
Expand Down

0 comments on commit c3a1692

Please sign in to comment.