enhancement(search): implement search match highlighting for the content field
This commit is contained in:
@@ -115,7 +115,15 @@ func (e *Engine) Search(ctx context.Context, sir *searchService.SearchIndexReque
|
||||
)
|
||||
}
|
||||
|
||||
body, err := NewRootQuery(boolQuery).MarshalJSON()
|
||||
body, err := NewRootQuery(boolQuery, RootQueryOptions{
|
||||
Highlight: RootQueryHighlight{
|
||||
PreTags: []string{"<mark>"},
|
||||
PostTags: []string{"</mark>"},
|
||||
Fields: map[string]RootQueryHighlight{
|
||||
"Content": {},
|
||||
},
|
||||
},
|
||||
}).MarshalJSON()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to marshal query: %w", err)
|
||||
}
|
||||
|
||||
@@ -58,7 +58,14 @@ func searchHitToSearchMessageMatch(hit opensearchgoAPI.SearchHit) (*searchMessag
|
||||
MimeType: resource.MimeType,
|
||||
Deleted: resource.Deleted,
|
||||
Tags: resource.Tags,
|
||||
//Highlights: getFragmentValue(hit.Fragments, "Content", 0),
|
||||
Highlights: func() string {
|
||||
contentHighlights, ok := hit.Highlight["Content"]
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
|
||||
return strings.Join(contentHighlights[:], "; ")
|
||||
}(),
|
||||
Audio: func() *searchMessage.Audio {
|
||||
if !strings.HasPrefix(resource.MimeType, "audio/") {
|
||||
return nil
|
||||
|
||||
+10
-2
@@ -9,8 +9,6 @@ type RootQuery struct {
|
||||
options RootQueryOptions
|
||||
}
|
||||
|
||||
type RootQueryOptions struct{}
|
||||
|
||||
func NewRootQuery(builder Builder, o ...RootQueryOptions) *RootQuery {
|
||||
return &RootQuery{query: builder, options: merge(o...)}
|
||||
}
|
||||
@@ -50,3 +48,13 @@ func (q *RootQuery) String() string {
|
||||
b, _ := q.MarshalJSON()
|
||||
return string(b)
|
||||
}
|
||||
|
||||
type RootQueryOptions struct {
|
||||
Highlight RootQueryHighlight `json:"highlight,omitempty"`
|
||||
}
|
||||
|
||||
type RootQueryHighlight struct {
|
||||
PreTags []string `json:"pre_tags,omitempty"`
|
||||
PostTags []string `json:"post_tags,omitempty"`
|
||||
Fields map[string]RootQueryHighlight `json:"fields,omitempty"`
|
||||
}
|
||||
+32
-1
@@ -9,7 +9,7 @@ import (
|
||||
"github.com/opencloud-eu/opencloud/services/search/pkg/opensearch/internal/test"
|
||||
)
|
||||
|
||||
func TestQuery(t *testing.T) {
|
||||
func TestRootQuery(t *testing.T) {
|
||||
tests := []opensearchtest.TableTest[opensearch.Builder, map[string]any]{
|
||||
{
|
||||
Name: "simple",
|
||||
@@ -24,6 +24,37 @@ func TestQuery(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "highlight",
|
||||
Got: opensearch.NewRootQuery(
|
||||
opensearch.NewTermQuery[string]("content").Value("content"),
|
||||
opensearch.RootQueryOptions{
|
||||
Highlight: opensearch.RootQueryHighlight{
|
||||
PreTags: []string{"<b>"},
|
||||
PostTags: []string{"</b>"},
|
||||
Fields: map[string]opensearch.RootQueryHighlight{
|
||||
"content": {},
|
||||
},
|
||||
},
|
||||
},
|
||||
),
|
||||
Want: map[string]any{
|
||||
"query": map[string]any{
|
||||
"term": map[string]any{
|
||||
"content": map[string]any{
|
||||
"value": "content",
|
||||
},
|
||||
},
|
||||
},
|
||||
"highlight": map[string]any{
|
||||
"pre_tags": []string{"<b>"},
|
||||
"post_tags": []string{"</b>"},
|
||||
"fields": map[string]any{
|
||||
"content": map[string]any{},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
Reference in New Issue
Block a user