mirror of
https://github.com/go-gitea/gitea.git
synced 2026-09-25 06:03:40 +09:00
The bleve path token filter added in https://github.com/go-gitea/gitea/pull/32210 never generated the full path of a file, so searching a file by its path (e.g. `potato/ham`) found nothing. It also gave the path tokens made-up offsets instead of their position in the path. The filter is replaced by a tokenizer that emits the raw path suffixes starting at each segment and word (`potato/ham.md`, `ham.md`). Paths below the root now match too, as do names containing `-` or spaces and dotfiles. An exact file name also ranks above files inside a directory with the same name. --------- Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
39 lines
1.1 KiB
Go
39 lines
1.1 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package bleve
|
|
|
|
import (
|
|
"github.com/blevesearch/bleve/v2/analysis"
|
|
unicode_tokenizer "github.com/blevesearch/bleve/v2/analysis/tokenizer/unicode"
|
|
"github.com/blevesearch/bleve/v2/registry"
|
|
)
|
|
|
|
const pathTokenizerName = "pathTokenizer"
|
|
|
|
// pathTokenizer emits the path suffixes starting at each segment and word, for the prefix query in Search
|
|
type pathTokenizer struct{}
|
|
|
|
func (pathTokenizer) Tokenize(input []byte) (ret analysis.TokenStream) {
|
|
words := unicode_tokenizer.NewUnicodeTokenizer().Tokenize(input)
|
|
for start := range input {
|
|
isWordStart := len(words) > 0 && words[0].Start == start
|
|
if isWordStart {
|
|
words = words[1:]
|
|
}
|
|
if start == 0 || input[start-1] == '/' || isWordStart {
|
|
ret = append(ret, &analysis.Token{
|
|
Start: start,
|
|
End: len(input),
|
|
Term: input[start:],
|
|
Position: len(ret) + 1,
|
|
})
|
|
}
|
|
}
|
|
return ret
|
|
}
|
|
|
|
func pathTokenizerConstructor(_ map[string]any, _ *registry.Cache) (analysis.Tokenizer, error) {
|
|
return pathTokenizer{}, nil
|
|
}
|