diff --git a/modules/indexer/issues/bleve/bleve.go b/modules/indexer/issues/bleve/bleve.go index 64d3c8122e..5ab5d19fc9 100644 --- a/modules/indexer/issues/bleve/bleve.go +++ b/modules/indexer/issues/bleve/bleve.go @@ -155,11 +155,12 @@ func (b *Indexer) Delete(_ context.Context, ids ...int64) error { func (b *Indexer) Search(ctx context.Context, options *internal.SearchOptions) (*internal.SearchResult, error) { var queries []query.Query - if options.Keyword != "" { - tokens, err := options.Tokens() - if err != nil { - return nil, err - } + tokens, err := options.Tokens() + if err != nil { + return nil, err + } + + if len(tokens) > 0 { q := bleve.NewBooleanQuery() for _, token := range tokens { innerQ := bleve.NewDisjunctionQuery( diff --git a/modules/indexer/issues/elasticsearch/elasticsearch.go b/modules/indexer/issues/elasticsearch/elasticsearch.go index 1bf0145796..9c8ca3eb8a 100644 --- a/modules/indexer/issues/elasticsearch/elasticsearch.go +++ b/modules/indexer/issues/elasticsearch/elasticsearch.go @@ -148,12 +148,13 @@ func (b *Indexer) Delete(ctx context.Context, ids ...int64) error { func (b *Indexer) Search(ctx context.Context, options *internal.SearchOptions) (*internal.SearchResult, error) { query := elastic.NewBoolQuery() - if options.Keyword != "" { + tokens, err := options.Tokens() + if err != nil { + return nil, err + } + + if len(tokens) > 0 { q := elastic.NewBoolQuery() - tokens, err := options.Tokens() - if err != nil { - return nil, err - } for _, token := range tokens { innerQ := elastic.NewMultiMatchQuery(token.Term, "title", "content", "comments") if token.Fuzzy { diff --git a/modules/indexer/issues/internal/qstring.go b/modules/indexer/issues/internal/qstring.go index fdb89b09e9..edf7510cb3 100644 --- a/modules/indexer/issues/internal/qstring.go +++ b/modules/indexer/issues/internal/qstring.go @@ -36,12 +36,9 @@ func (t *Tokenizer) next() (tk Token, err error) { // skip all leading white space for { - if r, _, err = t.in.ReadRune(); err == nil && r == ' ' { - //nolint:staticcheck,wastedassign // SA4006 the variable is used after the loop - r, _, err = t.in.ReadRune() - continue + if r, _, err = t.in.ReadRune(); err != nil || r != ' ' { + break } - break } if err != nil { return tk, err @@ -98,11 +95,17 @@ nextEnd: // Tokenize the keyword func (o *SearchOptions) Tokens() (tokens []Token, err error) { + if o.Keyword == "" { + return nil, nil + } + in := strings.NewReader(o.Keyword) it := Tokenizer{in: in} for token, err := it.next(); err == nil; token, err = it.next() { - tokens = append(tokens, token) + if token.Term != "" { + tokens = append(tokens, token) + } } if err != nil && err != io.EOF { return nil, err diff --git a/modules/indexer/issues/internal/qstring_test.go b/modules/indexer/issues/internal/qstring_test.go index a911b86e2f..d1d2b90a00 100644 --- a/modules/indexer/issues/internal/qstring_test.go +++ b/modules/indexer/issues/internal/qstring_test.go @@ -41,6 +41,36 @@ var testOpts = []testIssueQueryStringOpt{ }, }, }, + { + Keyword: "Hello World", + Results: []Token{ + { + Term: "Hello", + Fuzzy: true, + Kind: BoolOptShould, + }, + { + Term: "World", + Fuzzy: true, + Kind: BoolOptShould, + }, + }, + }, + { + Keyword: " Hello World ", + Results: []Token{ + { + Term: "Hello", + Fuzzy: true, + Kind: BoolOptShould, + }, + { + Term: "World", + Fuzzy: true, + Kind: BoolOptShould, + }, + }, + }, { Keyword: "+Hello +World", Results: []Token{ @@ -156,6 +186,68 @@ var testOpts = []testIssueQueryStringOpt{ }, }, }, + { + Keyword: "\\", + Results: nil, + }, + { + Keyword: "\"", + Results: nil, + }, + { + Keyword: "Hello \\", + Results: []Token{ + { + Term: "Hello", + Fuzzy: true, + Kind: BoolOptShould, + }, + }, + }, + { + Keyword: "\"\"", + Results: nil, + }, + { + Keyword: "\" World \"", + Results: []Token{ + { + Term: " World ", + Fuzzy: false, + Kind: BoolOptShould, + }, + }, + }, + { + Keyword: "\"\" World \"\"", + Results: []Token{ + { + Term: "World", + Fuzzy: true, + Kind: BoolOptShould, + }, + }, + }, + { + Keyword: "Best \"Hello World\" Ever", + Results: []Token{ + { + Term: "Best", + Fuzzy: true, + Kind: BoolOptShould, + }, + { + Term: "Hello World", + Fuzzy: false, + Kind: BoolOptShould, + }, + { + Term: "Ever", + Fuzzy: true, + Kind: BoolOptShould, + }, + }, + }, } func TestIssueQueryString(t *testing.T) { diff --git a/modules/indexer/issues/internal/tests/tests.go b/modules/indexer/issues/internal/tests/tests.go index 8308cb7f60..973c5615f0 100644 --- a/modules/indexer/issues/internal/tests/tests.go +++ b/modules/indexer/issues/internal/tests/tests.go @@ -88,6 +88,11 @@ func TestIndexer(t *testing.T, indexer internal.Indexer) { } } +func allResults(t *testing.T, data map[int64]*internal.IndexerData, result *internal.SearchResult) { + assert.Len(t, result.Hits, len(data)) + assert.Equal(t, len(data), int(result.Total)) +} + var cases = []*testIndexerCase{ { Name: "default", @@ -97,6 +102,34 @@ var cases = []*testIndexerCase{ assert.Equal(t, len(data), int(result.Total)) }, }, + { + Name: "empty keyword", + SearchOptions: &internal.SearchOptions{ + Keyword: "", + }, + Expected: allResults, + }, + { + Name: "whitespace keyword", + SearchOptions: &internal.SearchOptions{ + Keyword: " ", + }, + Expected: allResults, + }, + { + Name: "dangling slash in keyword", + SearchOptions: &internal.SearchOptions{ + Keyword: "\\", + }, + Expected: allResults, + }, + { + Name: "dangling quote in keyword", + SearchOptions: &internal.SearchOptions{ + Keyword: "\"", + }, + Expected: allResults, + }, { Name: "empty", SearchOptions: &internal.SearchOptions{