Skip to content

Commit

Permalink
fix search for inputs with a one-letter words
Browse files Browse the repository at this point in the history
  • Loading branch information
ShoshinNikita committed Oct 27, 2024
1 parent 5080718 commit cdb7951
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
11 changes: 7 additions & 4 deletions search/prefix_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand All @@ -202,6 +202,9 @@ func (index *prefixIndex) searchByPrefixes(words [][]rune) []searchHit {
delete(matchesForAllWords, k)
}
}
if len(matchesForAllWords) == 0 {
return nil
}
}
}

Expand Down
13 changes: 13 additions & 0 deletions search/prefix_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions search/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit cdb7951

Please sign in to comment.