diff --git a/components/errdoc/main.go b/components/errdoc/main.go index 67864ca771..368c8f2a00 100644 --- a/components/errdoc/main.go +++ b/components/errdoc/main.go @@ -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" @@ -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 } @@ -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 } @@ -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 @@ -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 -} diff --git a/components/errdoc/spec/spec.go b/components/errdoc/spec/spec.go index 46748d2e30..8f4741c539 100644 --- a/components/errdoc/spec/spec.go +++ b/components/errdoc/spec/spec.go @@ -15,6 +15,7 @@ package spec import ( "fmt" + "strings" "github.com/tj/go-termd" ) @@ -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 { @@ -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)