diff --git a/search/prefix_index.go b/search/prefix_index.go index 5b1720f..a8bd834 100644 --- a/search/prefix_index.go +++ b/search/prefix_index.go @@ -169,7 +169,7 @@ func (index *prefixIndex) searchByPrefixes(words [][]rune) []searchHit { matchCounts = make(map[uint64]int) matchesForAllWords = make(map[uint64]bool) ) - for i, word := range words { + for _, word := range words { // If a word length is less than MinPrefixLen, no prefixes will be generated, and // no hits will be returned. So, ignore such words. if len(word) < index.MinPrefixLen { @@ -185,13 +185,13 @@ func (index *prefixIndex) searchByPrefixes(words [][]rune) []searchHit { } } - // No reason to continue search. + // No reason to continue search - all words have to match. if len(matches) == 0 { return nil } - if i == 0 { - // Fill matchesForAllWords on first word. + if len(matchesForAllWords) == 0 { + // Fill matchesForAllWords on the first match. for k, v := range matches { matchesForAllWords[k] = v } @@ -202,6 +202,9 @@ func (index *prefixIndex) searchByPrefixes(words [][]rune) []searchHit { delete(matchesForAllWords, k) } } + if len(matchesForAllWords) == 0 { + return nil + } } } diff --git a/search/prefix_index_test.go b/search/prefix_index_test.go index 7d4adf1..bccc9d7 100644 --- a/search/prefix_index_test.go +++ b/search/prefix_index_test.go @@ -220,6 +220,19 @@ func TestPrefixIndex(t *testing.T) { hits, ) }) + + t.Run("search with a one-letter word", func(t *testing.T) { + r := require.New(t) + + index := newPrefixIndex([]string{"a beautiful picture"}, 3, 7) + hits := index.Search("a beautiful", 10) + r.Equal( + []rview.SearchHit{ + {Path: "a beautiful picture", Score: 5}, + }, + hits, + ) + }) } func TestNewSearchRequest(t *testing.T) { diff --git a/search/service_test.go b/search/service_test.go index 09e4c0f..a928523 100644 --- a/search/service_test.go +++ b/search/service_test.go @@ -61,6 +61,8 @@ func (s rcloneStub) GetAllFiles(ctx context.Context) (dirs, files []string, err } // ExampleSearch generates an output in Markdown format that is used in documentation for search. +// +//nolint:govet func ExampleSearch() { assertNoError := func(err error) { if err != nil {