Skip to content

Commit

Permalink
Fix search error documentation via error code (#866)
Browse files Browse the repository at this point in the history
  • Loading branch information
lonng authored and lucklove committed Oct 30, 2020
1 parent 649fafe commit a5b171c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 32 deletions.
74 changes: 45 additions & 29 deletions components/errdoc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"

"github.com/BurntSushi/toml"
"github.com/blevesearch/bleve"
"github.com/blevesearch/bleve/analysis/lang/en"
_ "github.com/blevesearch/bleve/index/store/goleveldb"
"github.com/blevesearch/bleve/mapping"
"github.com/blevesearch/bleve/search/query"
"github.com/pingcap/tiup/components/errdoc/spec"
"github.com/pingcap/tiup/pkg/localdata"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -58,19 +58,53 @@ func searchError(args []string) error {
return err
}

result, err := index.Search(bleve.NewSearchRequest(bleve.NewMatchPhraseQuery(strings.Join(args, " "))))
// Bleve search
queries := []query.Query{
bleve.NewMatchPhraseQuery(strings.Join(args, " ")),
}
var terms []query.Query
var prefix []query.Query
for _, arg := range args {
terms = append(terms, bleve.NewTermQuery(arg))
prefix = append(prefix, bleve.NewPrefixQuery(arg))
}
queries = append(queries, bleve.NewConjunctionQuery(terms...))
queries = append(queries, bleve.NewConjunctionQuery(prefix...))

result, err := index.Search(bleve.NewSearchRequest(bleve.NewDisjunctionQuery(queries...)))
if err != nil {
return err
}

for i, match := range result.Hits {
spec := errStore[match.ID]
fmt.Print(spec)
if i != len(result.Hits)-1 {
fmt.Println()
all := map[string]struct{}{}
for _, match := range result.Hits {
all[match.ID] = struct{}{}
}

// Error code prefix match
if len(args) == 1 {
c := strings.ToLower(args[0])
for code := range errStore {
if strings.HasPrefix(strings.ToLower(code), c) {
all[code] = struct{}{}
}
}
}

var sorted []string
for code := range all {
sorted = append(sorted, code)
}
sort.Slice(sorted, func(i, j int) bool {
return sorted[i] < sorted[j]
})
for _, code := range sorted {
spec := errStore[code]
fmt.Println(spec)
}

fmt.Printf("%d matched\n", len(sorted))

return nil
}

Expand All @@ -91,7 +125,7 @@ func loadIndex() (bleve.Index, map[string]*spec.ErrorSpec, error) {
needIndex := err == bleve.ErrorIndexPathDoesNotExist

if needIndex {
indexMapping := buildIndexMapping()
indexMapping := bleve.NewIndexMapping()
if err := os.MkdirAll(indexPath, 0755); err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -124,6 +158,8 @@ func loadIndex() (bleve.Index, map[string]*spec.ErrorSpec, error) {
}
for code, spec := range file {
spec.Code = code
spec.ExtraCode = strings.ReplaceAll(code, ":", " ")
spec.ExtraError = strings.ReplaceAll(spec.Error, ":", " ")
errStore[code] = spec
if !needIndex {
continue
Expand All @@ -136,23 +172,3 @@ func loadIndex() (bleve.Index, map[string]*spec.ErrorSpec, error) {
})
return index, errStore, err
}

func buildIndexMapping() mapping.IndexMapping {
englishTextFieldMapping := bleve.NewTextFieldMapping()
englishTextFieldMapping.Analyzer = en.AnalyzerName

documentMapping := bleve.NewDocumentMapping()

documentMapping.AddFieldMappingsAt("error", englishTextFieldMapping)
documentMapping.AddFieldMappingsAt("description", englishTextFieldMapping)
documentMapping.AddFieldMappingsAt("workaround", englishTextFieldMapping)
documentMapping.AddFieldMappingsAt("tags", englishTextFieldMapping)
documentMapping.AddFieldMappingsAt("code", englishTextFieldMapping)

indexMapping := bleve.NewIndexMapping()
indexMapping.DefaultMapping = documentMapping
indexMapping.TypeField = "type"
indexMapping.DefaultAnalyzer = "en"

return indexMapping
}
10 changes: 7 additions & 3 deletions components/errdoc/spec/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package spec

import (
"fmt"
"strings"

"github.com/tj/go-termd"
)
Expand All @@ -27,6 +28,9 @@ type ErrorSpec struct {
Description string `toml:"description" json:"description"`
Tags []string `toml:"tags" json:"tags"`
Workaround string `toml:"workaround" json:"workaround"`
// Used for indexes
ExtraCode string `toml:"extracode" json:"extracode"`
ExtraError string `toml:"extraerror" json:"extraerror"`
}

func newCompiler() *termd.Compiler {
Expand All @@ -44,17 +48,17 @@ func (f ErrorSpec) String() string {
header = fmt.Sprintf("# Error: **%s**", f.Code)
}

tmpl := header + "\n" + f.Error
tmpl := header + "\n" + strings.TrimSpace(f.Error)

description := f.Description
if description != "" {
tmpl += `## Description
` + description
` + strings.TrimSpace(description)
}
workaround := f.Workaround
if workaround != "" {
tmpl += `## Workaround
` + workaround
` + strings.TrimSpace(workaround)
}

return newCompiler().Compile(tmpl)
Expand Down

0 comments on commit a5b171c

Please sign in to comment.