Repository navigation
Expand file tree
/
Copy pathelasticsearch-highlight-config.ts
More file actions
86 lines (78 loc) · 2.24 KB
/
Copy pathelasticsearch-highlight-config.ts
File metadata and controls
86 lines (78 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import type { estypes } from '@elastic/elasticsearch'
import type { HighlightOptions } from '@/search/lib/search-request-params/types'
export interface HighlightConfig {
type: string
fragment_size?: number
number_of_fragments?: number
no_match_size?: number
highlight_query?: object
}
export type HighlightFields = {
[key in HighlightOptions]: HighlightConfig
}
// When we query Elasticsearch, we can specify a highlight configuration
export function getHighlightConfiguration(
query: string,
highlightsFields: HighlightOptions[],
): estypes.SearchHighlight {
const fields = {} as HighlightFields
if (highlightsFields.includes('title')) {
fields.title = {
// Fast Vector Highlighter
// Using this requires that you first index these fields
// with {term_vector: 'with_positions_offsets'}
type: 'fvh',
fragment_size: 200,
number_of_fragments: 1,
}
}
if (highlightsFields.includes('content')) {
fields.content = {
// Fast Vector Highlighter
// Using this requires that you first index these fields
// with {term_vector: 'with_positions_offsets'}
type: 'fvh',
fragment_size: 150,
number_of_fragments: 1,
// So we can at least display something if there was no highlight match within the content.
no_match_size: 150,
highlight_query: {
match_phrase_prefix: {
content: {
query,
},
},
},
}
fields.content_explicit = {
// Fast Vector Highlighter
// Using this requires that you first index these fields
// with {term_vector: 'with_positions_offsets'}
type: 'fvh',
fragment_size: 150,
number_of_fragments: 1,
no_match_size: 0,
highlight_query: {
match_phrase_prefix: {
content_explicit: {
query,
},
},
},
}
}
if (highlightsFields.includes('term')) {
fields.term = {
// Fast Vector Highlighter
// Using this requires that you first index these fields
// with {term_vector: 'with_positions_offsets'}
type: 'fvh',
}
}
const highlightConfig: estypes.SearchHighlight = {
pre_tags: ['<mark>'],
post_tags: ['</mark>'],
fields,
}
return highlightConfig
}