Improve highlight support in osu

Co-authored-by: Florian Schade <f.schade@icloud.com>
This commit is contained in:
André Duffeck
2026-03-19 15:52:58 +01:00
parent a6dd9b9e18
commit 4fa5198501
3 changed files with 53 additions and 14 deletions

View File

@@ -112,11 +112,13 @@ func (b *Backend) Search(ctx context.Context, sir *searchService.SearchIndexRequ
boolQuery,
osu.SearchBodyParams{
Highlight: &osu.BodyParamHighlight{
PreTags: []string{"<mark>"},
PostTags: []string{"</mark>"},
Fields: map[string]osu.BodyParamHighlight{
HighlightOptions: osu.HighlightOptions{
PreTags: []string{"<mark>"},
PostTags: []string{"</mark>"},
},
Fields: map[string]osu.HighlightOptions{
"Content": {
Type: "fvh",
Type: osu.HighlightTypeFvh,
},
},
},

View File

@@ -41,10 +41,39 @@ func (q QueryReqBody[O]) MarshalJSON() ([]byte, error) {
//----------------------------------------------------------------------------//
type BodyParamHighlight struct {
PreTags []string `json:"pre_tags,omitempty"`
PostTags []string `json:"post_tags,omitempty"`
Type string `json:"type,omitempty"`
Fields map[string]BodyParamHighlight `json:"fields,omitempty"`
HighlightOptions
Fields map[string]HighlightOptions `json:"fields,omitempty"`
}
type HighlightType string
const (
HighlightTypeUnified HighlightType = "unified"
HighlightTypeFvh HighlightType = "fvh"
HighlightTypePlain HighlightType = "plain"
HighlightTypeSemantic HighlightType = "semantic"
)
type HighlightOptions struct {
Type HighlightType `json:"type,omitempty"`
FragmentSize int `json:"fragment_size,omitempty"`
NumberOfFragments int `json:"number_of_fragments,omitempty"`
FragmentOffset int `json:"fragment_offset,omitempty"`
BoundaryChars string `json:"boundary_chars,omitempty"`
BoundaryMaxScan int `json:"boundary_max_scan,omitempty"`
BoundaryScanner string `json:"boundary_scanner,omitempty"`
BoundaryScannerLocale string `json:"boundary_scanner_locale,omitempty"`
Encoder string `json:"encoder,omitempty"`
ForceSource bool `json:"force_source,omitempty"`
Fragmenter string `json:"fragmenter,omitempty"`
HighlightQuery Builder `json:"highlight_query,omitempty"`
Order string `json:"order,omitempty"`
NoMatchSize int `json:"no_match_size,omitempty"`
RequireFieldMatch bool `json:"require_field_match,omitempty"`
MatchedFields []string `json:"matched_fields,omitempty"`
PhraseLimit int `json:"phrase_limit,omitempty"`
PreTags []string `json:"pre_tags,omitempty"`
PostTags []string `json:"post_tags,omitempty"`
}
type BodyParamScript struct {

View File

@@ -9,7 +9,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/opencloud-eu/opencloud/services/search/pkg/opensearch/internal/osu"
"github.com/opencloud-eu/opencloud/services/search/pkg/opensearch/internal/test"
opensearchtest "github.com/opencloud-eu/opencloud/services/search/pkg/opensearch/internal/test"
)
func TestRequestBody(t *testing.T) {
@@ -46,10 +46,15 @@ func TestBuildSearchReq(t *testing.T) {
osu.NewTermQuery[string]("content").Value("content"),
osu.SearchBodyParams{
Highlight: &osu.BodyParamHighlight{
PreTags: []string{"<b>"},
PostTags: []string{"</b>"},
Fields: map[string]osu.BodyParamHighlight{
"content": {},
HighlightOptions: osu.HighlightOptions{
PreTags: []string{"<b>"},
PostTags: []string{"</b>"},
},
Fields: map[string]osu.HighlightOptions{
"content": {
PreTags: []string{"<strong>"},
PostTags: []string{"</strong>"},
},
},
},
},
@@ -69,7 +74,10 @@ func TestBuildSearchReq(t *testing.T) {
"pre_tags": []string{"<b>"},
"post_tags": []string{"</b>"},
"fields": map[string]any{
"content": map[string]any{},
"content": map[string]any{
"pre_tags": []string{"<strong>"},
"post_tags": []string{"</strong>"},
},
},
},
},