fork of https://github.com/sourcegraph/zoekt
0

Configure Feed

Select the types of activity you want to include in your feed.

webserver: add score to web templates in debug mode (#305)

With this change we display scores for files and matches on the search
results page. The behavior can be toggled by adding &debug=1 to
the URL.

The debug info is not part of the JSON API and is only used for
template evaluation.

+34 -17
+3
api.go
··· 543 543 // a command-line flag 544 544 Trace bool 545 545 546 + // If set, the search results will contain debug information for scoring. 547 + DebugScore bool 548 + 546 549 // SpanContext is the opentracing span context, if it exists, from the zoekt client 547 550 SpanContext map[string]string 548 551 }
+6 -10
eval.go
··· 29 29 30 30 const maxUInt16 = 0xffff 31 31 32 - // DebugScore controls whether we collect data on match scores are 33 - // constructed. Intended for use in tests. 34 - var DebugScore = false 35 - 36 - func (m *FileMatch) addScore(what string, s float64) { 37 - if DebugScore { 32 + func (m *FileMatch) addScore(what string, s float64, debugScore bool) { 33 + if debugScore { 38 34 m.Debug += fmt.Sprintf("%s:%f, ", what, s) 39 35 } 40 36 m.Score += s ··· 368 364 // Maintain ordering of input files. This 369 365 // strictly dominates the in-file ordering of 370 366 // the matches. 371 - fileMatch.addScore("fragment", maxFileScore) 372 - fileMatch.addScore("atom", float64(atomMatchCount)/float64(totalAtomCount)*scoreFactorAtomMatch) 367 + fileMatch.addScore("fragment", maxFileScore, opts.DebugScore) 368 + fileMatch.addScore("atom", float64(atomMatchCount)/float64(totalAtomCount)*scoreFactorAtomMatch, opts.DebugScore) 373 369 374 370 // Prefer earlier docs. 375 - fileMatch.addScore("doc-order", scoreFileOrderFactor*(1.0-float64(nextDoc)/float64(len(d.boundaries)))) 376 - fileMatch.addScore("shard-order", scoreShardRankFactor*float64(md.Rank)/maxUInt16) 371 + fileMatch.addScore("doc-order", scoreFileOrderFactor*(1.0-float64(nextDoc)/float64(len(d.boundaries))), opts.DebugScore) 372 + fileMatch.addScore("shard-order", scoreShardRankFactor*float64(md.Rank)/maxUInt16, opts.DebugScore) 377 373 378 374 if fileMatch.Score > scoreImportantThreshold { 379 375 importantMatchCount++
+7
web/api.go
··· 56 56 Branches []string 57 57 Matches []Match 58 58 URL string 59 + 60 + // Don't expose to caller of JSON API 61 + Score float64 `json:"-"` 62 + ScoreDebug string `json:"-"` 59 63 } 60 64 61 65 // Match holds the per line data provided to the search results template ··· 67 71 Fragments []Fragment 68 72 Before string `json:",omitempty"` 69 73 After string `json:",omitempty"` 74 + 75 + // Don't expose to caller of JSON API 76 + Score float64 `json:"-"` 70 77 } 71 78 72 79 // Fragment holds data of a single contiguous match within in a line
+7
web/server.go
··· 226 226 227 227 func (s *Server) serveSearchErr(r *http.Request) (*ApiSearchResult, error) { 228 228 qvals := r.URL.Query() 229 + 230 + debugScore := false 231 + if qvals.Get("debug") == "1" { 232 + debugScore = true 233 + } 234 + 229 235 queryStr := qvals.Get("q") 230 236 if queryStr == "" { 231 237 return nil, fmt.Errorf("no query found") ··· 307 313 sOpts.TotalMaxImportantMatch = n 308 314 } 309 315 sOpts.MaxDocDisplayCount = num 316 + sOpts.DebugScore = debugScore 310 317 311 318 result, err := s.Searcher.Search(ctx, q, &sOpts) 312 319 if err != nil {
+8 -5
web/snippets.go
··· 92 92 seenFiles := map[string]string{} 93 93 for _, f := range result.Files { 94 94 fMatch := FileMatch{ 95 - FileName: f.FileName, 96 - Repo: f.Repository, 97 - ResultID: f.Repository + ":" + f.FileName, 98 - Branches: f.Branches, 99 - Language: f.Language, 95 + FileName: f.FileName, 96 + Repo: f.Repository, 97 + ResultID: f.Repository + ":" + f.FileName, 98 + Branches: f.Branches, 99 + Language: f.Language, 100 + Score: f.Score, 101 + ScoreDebug: f.Debug, 100 102 } 101 103 102 104 if dup, ok := seenFiles[string(f.Checksum)]; ok { ··· 122 124 FileName: f.FileName, 123 125 LineNum: m.LineNumber, 124 126 URL: fMatch.URL + fragment, 127 + Score: m.Score, 125 128 } 126 129 127 130 md.Before = string(m.Before)
+3 -2
web/templates.go
··· 219 219 {{else}}.{{end}} 220 220 </h5> 221 221 {{range .FileMatches}} 222 + {{$showScoreDebug := .ScoreDebug}} 222 223 <table class="table table-hover table-condensed"> 223 224 <thead> 224 225 <tr> 225 226 <th> 226 227 {{if .URL}}<a name="{{.ResultID}}" class="result"></a><a href="{{.URL}}" >{{else}}<a name="{{.ResultID}}">{{end}} 227 228 <small> 228 - {{.Repo}}:{{.FileName}}</a>: 229 + {{.Repo}}:{{.FileName}} {{if $showScoreDebug}}<i>(score:{{.Score}} <-- {{.ScoreDebug}})</i>{{end}}</a>: 229 230 <span style="font-weight: normal">[ {{if .Branches}}{{range .Branches}}<span class="label label-default">{{.}}</span>,{{end}}{{end}} ]</span> 230 231 {{if .Language}}<button 231 232 title="restrict search to files written in {{.Language}}" ··· 240 241 {{range .Matches}} 241 242 <tr> 242 243 <td style="background-color: rgba(238, 238, 255, 0.6);"> 243 - <pre class="inline-pre"><span class="noselect">{{if .URL}}<a href="{{.URL}}">{{end}}<u>{{.LineNum}}</u>{{if .URL}}</a>{{end}}: </span>{{range .Fragments}}{{LimitPre 100 .Pre}}<b>{{.Match}}</b>{{LimitPost 100 .Post}}{{end}}</pre> 244 + <pre class="inline-pre"><span class="noselect">{{if .URL}}<a href="{{.URL}}">{{end}}<u>{{.LineNum}}</u>{{if .URL}}</a>{{end}}: </span>{{range .Fragments}}{{LimitPre 100 .Pre}}<b>{{.Match}}</b>{{LimitPost 100 .Post}}{{end}} {{if $showScoreDebug}}<i>(score:{{.Score}})</i>{{end}}</pre> 244 245 </td> 245 246 </tr> 246 247 {{end}}