Skip to content

Commit

Permalink
refactor(search_playground): updated to exclude empty field values fr…
Browse files Browse the repository at this point in the history
…om the context documents
  • Loading branch information
TattdCodeMonkey committed Feb 14, 2025
1 parent e988500 commit 96f1989
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ describe('conversational chain', () => {
expectedDocs: [
{
documents: [
{ metadata: { _id: '1', _index: 'index' }, pageContent: 'field: ' },
{ metadata: { _id: '1', _index: 'index' }, pageContent: '' },
{
metadata: { _id: '1', _index: 'website' },
pageContent:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,46 @@ describe('contextDocumentHitMapper', () => {
},
});
});
it('should not include empty field values', () => {
const hit = {
_index: 'test-index',
_score: 1,
_id: 'id',
_source: {
text: 'foo bar baz',
other: '',
},
};
const contentField = { 'test-index': ['text', 'other'] };
const document = contextDocumentHitMapper(contentField)(hit);
expect(document).toEqual({
pageContent: 'text: foo bar baz',
metadata: {
_score: 1,
_id: 'id',
_index: 'test-index',
},
});
});
it('should handle all empty field values', () => {
const hit = {
_index: 'test-index',
_score: 1,
_id: 'id',
_source: {
text: '',
other: '',
},
};
const contentField = { 'test-index': ['text', 'other'] };
const document = contextDocumentHitMapper(contentField)(hit);
expect(document).toEqual({
pageContent: '',
metadata: {
_score: 1,
_id: 'id',
_index: 'test-index',
},
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,21 @@ export const contextDocumentHitMapper =
(contentField: ElasticsearchRetrieverContentField) =>
(hit: SearchHit): Document => {
let pageContent: string = '';
const makePageContentForField = (field: string) =>
`${field}: ${getValueForSelectedField(hit, field)}`;
const makePageContentForField = (field: string) => {
const fieldValue = getValueForSelectedField(hit, field);
return fieldValue.length > 0 ? `${field}: ${fieldValue}` : '';
};
if (typeof contentField === 'string') {
pageContent = makePageContentForField(contentField);
} else {
const pageContentFieldKey = contentField[hit._index];
if (typeof pageContentFieldKey === 'string') {
pageContent = makePageContentForField(pageContentFieldKey);
} else {
pageContent = pageContentFieldKey.map((field) => makePageContentForField(field)).join('\n');
pageContent = pageContentFieldKey
.map((field) => makePageContentForField(field))
.filter((fieldContent) => fieldContent.length > 0)
.join('\n');
}
}

Expand Down

0 comments on commit 96f1989

Please sign in to comment.