/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
import React from 'react';
import {
EuiButtonIcon,
EuiLink,
EuiPanel,
EuiText,
EuiFlexGroup,
EuiFlexItem,
EuiTitle,
EuiIcon,
EuiIconTip,
} from '@elastic/eui';
import _, { uniqueId } from 'lodash';
import { IDocType, SearchResults, Document } from '../../../../types/index';
import { DocumentRank } from '../../../../contexts/utils';
import { useSearchRelevanceContext } from '../../../../contexts';
import './result_grid.scss';
interface ResultGridComponentProps {
comparedDocumentsRank: DocumentRank;
queryResult: SearchResults;
resultNumber: number;
}
export const ResultGridComponent = ({
comparedDocumentsRank,
queryResult,
resultNumber,
}: ResultGridComponentProps) => {
const { selectedIndex1, selectedIndex2 } = useSearchRelevanceContext();
const getExpColapTd = () => {
return (
|
);
};
const getDlTmpl = (doc: IDocType) => {
return (
{_.toPairs(doc).map((entry: string[]) => {
return (
<>
- {`${entry[0]}:`}
-
{_.isObject(entry[1]) ? JSON.stringify(entry[1]) : entry[1]}
>
);
})}
);
};
const getTdTmpl = (conf: { clsName: string; content: React.ReactDOM | string }) => {
const { clsName, content } = conf;
return (
{typeof content === 'boolean' ? String(content) : content}
|
);
};
const getRankComparison = (documentId: string, documentRank: number) => {
const comparedRank = comparedDocumentsRank[documentId];
// No match result in comparison set
if (typeof comparedRank !== 'number') {
return (
Not applicable
{' '}
Not in Results {resultNumber === 1 ? 2 : 1}
) : (
No cross-references when using different indexes
)
}
/>
);
}
const rankDifference = documentRank - comparedRank;
if (rankDifference === 0) {
return (
No change
);
} else if (rankDifference < 0) {
return (
Up {Math.abs(rankDifference)}
);
} else if (rankDifference > 0) {
return (
Down {rankDifference}
);
}
};
const getRankColumn = (documentId: string, documentRank: number) => {
return (
{documentRank}
{getRankComparison(documentId, documentRank)}
|
);
};
const getTds = (document: Document, documentRank: number) => {
const cols = [];
const fieldClsName = 'eui-textBreakAll eui-textBreakWord';
const timestampClsName = 'eui-textNoWrap';
// Get rank index column
cols.push(getRankColumn(document._id, documentRank));
// No field is selected
const _sourceLikeDOM = getDlTmpl(document._source);
cols.push(
getTdTmpl({
clsName: fieldClsName,
content: _sourceLikeDOM,
})
);
// // Add detail toggling column
// // cols.unshift(getExpColapTd());
// cols.push(getExpColapTd());
return cols;
};
const resultGrid = () => {
return (
<>
{queryResult.hits.hits.map((document: Document, documentRank: number) => {
return (
{getTds(document, documentRank + 1)}
);
})}
>
);
};
// useEffect(() => {
// console.log('query result changed');
// if (!_.isEmpty(queryResult))
// setResultGrid(
// queryResult.hits.hits.map((doc: any, id: number) => {
// return (
// <>
// {getTds(doc._source)}
// >
// );
// })
// );
// }, [queryResult]);
return (
);
};