Skip to content

Commit

Permalink
add highlights to the search
Browse files Browse the repository at this point in the history
  • Loading branch information
smadbe committed Jun 28, 2023
1 parent 28de0a0 commit d09501c
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions src/search-client/search-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Client } from '@opensearch-project/opensearch';
import * as D from 'io-ts/Decoder';
import { decodeOrThrow } from '../utils/decode';

const nbHightlights = 3;

interface Content {
id: string,
title: string,
Expand All @@ -23,14 +25,25 @@ const apiResponseDecoder = D.struct({
id: D.string,
title: D.string,
type: D.literal('Task', 'Chapter')
}),
highlight: D.partial({
title: D.array(D.string),
summary: D.array(D.string),
l2subtitles: D.array(D.string),
l3subtitles: D.array(D.string),
fullText: D.array(D.string),
})
}))
})
})
/* eslint-enable @typescript-eslint/naming-convention */
});
interface SearchResponse {
search_results: (D.TypeOf<typeof apiResponseDecoder>['body']['hits']['hits'][number]['_source'] & { score: number })[],
search_results: (D.TypeOf<typeof apiResponseDecoder>['body']['hits']['hits'][number]['_source'] & {
score: number,
title_highlight: string|null,
highlights: string[],
})[],
}

export class SearchClient {
Expand Down Expand Up @@ -73,6 +86,16 @@ export class SearchClient {
// }
}
},
highlight: {
fields: {
title: {},
summary: {},
l2subtitles: {},
l3subtitles: {},
fullText: {}
},
number_of_fragments: nbHightlights
},
_source: [ 'id', 'title', 'type' ],
/* eslint-enable @typescript-eslint/naming-convention */
},
Expand All @@ -81,7 +104,17 @@ export class SearchClient {
if (debug) console.debug(`Search term: ${query}. Search response: ${JSON.stringify(rawResp)}`);
const resp = decodeOrThrow(apiResponseDecoder)(rawResp);
return {
search_results: resp.body.hits.hits.map(r => ({ ...r._source, score: r._score }))
search_results: resp.body.hits.hits.map(r => ({
...r._source,
score: r._score,
title_highlight: r.highlight.title?.at(0) ?? null,
highlights: [
...(r.highlight.summary ?? []),
...(r.highlight.l2subtitles ?? []),
...(r.highlight.l3subtitles ?? []),
...(r.highlight.fullText ?? [])
].slice(0, nbHightlights),
}))
};
}

Expand Down

0 comments on commit d09501c

Please sign in to comment.