feat(beachcomber): search highlights and field projection (quick-261108-pe4)
- Additive: Query.IncludeFields, ExcludeFields and Highlight; left empty, the Typesense request is the same as before. - SearchResult.Hits holds one Hit per id, in step with IDs; SearchPage always fills it. - Snippets are plain-text Segment runs split at private-use highlight tags, never HTML; MaxSnippetsPerArray is applied client-side. - Optional HighlightSearcher capability, checked with SupportsHighlights (true for Typesense, false for null and third-party engines). - README and docs: Highlights sections with the rule that a caller drops the hit of every id failing the SQL re-check before showing a snippet. Targets framework v0.6.4 (additive, no breaking change).
This commit is contained in:
@@ -45,7 +45,7 @@ func TestSearchPageFallsBackToSearchIDs(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(res, SearchResult{IDs: []string{"3", "1"}, Found: 2}) {
|
||||
if !reflect.DeepEqual(res, SearchResult{IDs: []string{"3", "1"}, Found: 2, Hits: []Hit{{ID: "3"}, {ID: "1"}}}) {
|
||||
t.Fatalf("res = %+v", res)
|
||||
}
|
||||
if !reflect.DeepEqual(e.got, q) {
|
||||
@@ -74,11 +74,63 @@ func TestSearchPageNullEngine(t *testing.T) {
|
||||
t.Fatal("the null engine implements PageSearcher")
|
||||
}
|
||||
res, err := SearchPage(context.Background(), e, "posts", Query{})
|
||||
if err != nil || res.Found != 0 || res.IDs == nil || len(res.IDs) != 0 {
|
||||
if err != nil || res.Found != 0 || res.IDs == nil || len(res.IDs) != 0 || res.Hits == nil || len(res.Hits) != 0 {
|
||||
t.Fatalf("res = %+v err = %v", res, err)
|
||||
}
|
||||
}
|
||||
|
||||
// highlightEngine implements HighlightSearcher with a configurable answer
|
||||
// and can return its own hits.
|
||||
type highlightEngine struct {
|
||||
pagedEngine
|
||||
supports bool
|
||||
hits []Hit
|
||||
}
|
||||
|
||||
func (e *highlightEngine) SupportsHighlights() bool { return e.supports }
|
||||
|
||||
func (e *highlightEngine) SearchPage(_ context.Context, _ string, q Query) (SearchResult, error) {
|
||||
e.got = q
|
||||
return SearchResult{IDs: e.ids, Found: e.found, Hits: e.hits}, nil
|
||||
}
|
||||
|
||||
func TestSupportsHighlights(t *testing.T) {
|
||||
for name, e := range map[string]Engine{
|
||||
"null": nullEngine{},
|
||||
"ids-only": &idsOnlyEngine{},
|
||||
"paged": &pagedEngine{},
|
||||
"nil": nil,
|
||||
} {
|
||||
if SupportsHighlights(e) {
|
||||
t.Fatalf("%s: SupportsHighlights = true", name)
|
||||
}
|
||||
}
|
||||
if !SupportsHighlights(&highlightEngine{supports: true}) {
|
||||
t.Fatal("an engine answering true supports highlights")
|
||||
}
|
||||
if SupportsHighlights(&highlightEngine{supports: false}) {
|
||||
t.Fatal("an engine answering false does not support highlights")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchPageFillsHits(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
res, err := SearchPage(ctx, &pagedEngine{idsOnlyEngine: idsOnlyEngine{ids: []string{"7"}}, found: 1}, "posts", Query{})
|
||||
if err != nil || !reflect.DeepEqual(res.Hits, []Hit{{ID: "7"}}) {
|
||||
t.Fatalf("hits = %+v err = %v", res.Hits, err)
|
||||
}
|
||||
own := []Hit{{ID: "7", Highlights: []Snippet{{Field: "title", ArrayIndex: -1, Segments: []Segment{{Text: "x", Match: true}}}}}}
|
||||
he := &highlightEngine{pagedEngine: pagedEngine{idsOnlyEngine: idsOnlyEngine{ids: []string{"7"}}}, supports: true, hits: own}
|
||||
res, err = SearchPage(ctx, he, "posts", Query{Highlight: &Highlight{}})
|
||||
if err != nil || !reflect.DeepEqual(res.Hits, own) {
|
||||
t.Fatalf("own hits = %+v err = %v", res.Hits, err)
|
||||
}
|
||||
res, err = SearchPage(ctx, &pagedEngine{}, "posts", Query{})
|
||||
if err != nil || res.Hits == nil || len(res.Hits) != 0 {
|
||||
t.Fatalf("zero ids: hits = %#v err = %v", res.Hits, err)
|
||||
}
|
||||
}
|
||||
|
||||
// flushEngine records Flush calls and can fail them.
|
||||
type flushEngine struct {
|
||||
idsOnlyEngine
|
||||
|
||||
Reference in New Issue
Block a user