Search: Explain scores (#98316)
This commit is contained in:
@@ -343,6 +343,16 @@ func StandardSearchFields() SearchableDocumentFields {
|
||||
Type: ResourceTableColumnDefinition_INT64,
|
||||
Description: "created timestamp", // date?
|
||||
},
|
||||
{
|
||||
Name: SEARCH_FIELD_EXPLAIN,
|
||||
Type: ResourceTableColumnDefinition_OBJECT,
|
||||
Description: "Explain why this result matches (depends on the engine)",
|
||||
},
|
||||
{
|
||||
Name: SEARCH_FIELD_SCORE,
|
||||
Type: ResourceTableColumnDefinition_DOUBLE,
|
||||
Description: "The search score",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
panic("failed to initialize standard search fields")
|
||||
|
||||
@@ -123,6 +123,9 @@ func NewTableBuilder(cols []*ResourceTableColumnDefinition) (*TableBuilder, erro
|
||||
}
|
||||
var err error
|
||||
for i, v := range cols {
|
||||
if v == nil {
|
||||
return nil, fmt.Errorf("invalid field definitions")
|
||||
}
|
||||
if table.lookup[v.Name] != nil {
|
||||
table.hasDuplicateNames = true
|
||||
continue
|
||||
|
||||
@@ -2,6 +2,7 @@ package search
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
@@ -413,7 +414,11 @@ func toBleveSearchRequest(req *resource.ResourceSearchRequest, access authz.Acce
|
||||
}
|
||||
}
|
||||
|
||||
queries = append(queries, newTextQuery(req))
|
||||
// Add a text query
|
||||
if req.Query != "" && req.Query != "*" {
|
||||
searchrequest.Fields = append(searchrequest.Fields, resource.SEARCH_FIELD_SCORE)
|
||||
queries = append(queries, bleve.NewFuzzyQuery(req.Query))
|
||||
}
|
||||
|
||||
if access != nil {
|
||||
// TODO AUTHZ!!!!
|
||||
@@ -581,19 +586,27 @@ func (b *bleveIndex) hitsToTable(selectFields []string, hits search.DocumentMatc
|
||||
}
|
||||
|
||||
for i, f := range fields {
|
||||
if f.Name == resource.SEARCH_FIELD_ID {
|
||||
var v any
|
||||
switch f.Name {
|
||||
case resource.SEARCH_FIELD_ID:
|
||||
row.Cells[i] = []byte(match.ID)
|
||||
continue
|
||||
}
|
||||
|
||||
// QUICK QUICK... more options yes
|
||||
v := match.Fields[f.Name]
|
||||
if v != nil {
|
||||
// Encode the value to protobuf
|
||||
row.Cells[i], err = encoders[i](v)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error encoding (row:%d/col:%d) %v %w", rowID, i, v, err)
|
||||
case resource.SEARCH_FIELD_SCORE:
|
||||
row.Cells[i], err = encoders[i](match.Score)
|
||||
|
||||
case resource.SEARCH_FIELD_EXPLAIN:
|
||||
if match.Expl != nil {
|
||||
row.Cells[i], err = json.Marshal(match.Expl)
|
||||
}
|
||||
default:
|
||||
v := match.Fields[f.Name]
|
||||
if v != nil {
|
||||
// Encode the value to protobuf
|
||||
row.Cells[i], err = encoders[i](v)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error encoding (row:%d/col:%d) %v %w", rowID, i, v, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -644,12 +657,3 @@ func newResponseFacet(v *search.FacetResult) *resource.ResourceSearchResponse_Fa
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
func newTextQuery(req *resource.ResourceSearchRequest) query.Query {
|
||||
if req.Query == "" || req.Query == "*" {
|
||||
return bleve.NewMatchAllQuery()
|
||||
}
|
||||
// TODO: wildcard query?
|
||||
// return bleve.NewWildcardQuery(req.Query)
|
||||
return bleve.NewFuzzyQuery(req.Query)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user