mirror of
https://github.com/go-gitea/gitea.git
synced 2026-08-18 10:41:21 +00:00
fix(indexer): correct bleve indexer token filters (#38853)
* fix #36228 * fix #37221 --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
@@ -15,7 +15,6 @@ import (
|
||||
|
||||
"github.com/blevesearch/bleve/v2"
|
||||
"github.com/blevesearch/bleve/v2/analysis/analyzer/custom"
|
||||
"github.com/blevesearch/bleve/v2/analysis/token/camelcase"
|
||||
"github.com/blevesearch/bleve/v2/analysis/token/lowercase"
|
||||
"github.com/blevesearch/bleve/v2/analysis/token/unicodenorm"
|
||||
"github.com/blevesearch/bleve/v2/analysis/tokenizer/unicode"
|
||||
@@ -26,7 +25,7 @@ import (
|
||||
const (
|
||||
issueIndexerAnalyzer = "issueIndexer"
|
||||
issueIndexerDocType = "issueIndexerDocType"
|
||||
issueIndexerLatestVersion = 7
|
||||
issueIndexerLatestVersion = 8
|
||||
)
|
||||
|
||||
const unicodeNormalizeName = "unicodeNormalize"
|
||||
@@ -103,7 +102,7 @@ func generateIssueIndexMapping() (mapping.IndexMapping, error) {
|
||||
"type": custom.Name,
|
||||
"char_filters": []string{},
|
||||
"tokenizer": unicode.Name,
|
||||
"token_filters": []string{unicodeNormalizeName, camelcase.Name, lowercase.Name},
|
||||
"token_filters": []string{unicodeNormalizeName, camelCaseKeepWholeName, lowercase.Name},
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -77,6 +77,47 @@ func TestBleveIndexerNoAssignee(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestBleveIndexerTokenFilter(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
indexer := NewIndexer(dir)
|
||||
defer indexer.Close()
|
||||
|
||||
_, err := indexer.Init(t.Context())
|
||||
require.NoError(t, err)
|
||||
|
||||
require.NoError(t, indexer.Index(t.Context(),
|
||||
&internal.IndexerData{ID: 1, Title: "fix(packages): SomeThing needs a rewrite (#12345)"},
|
||||
&internal.IndexerData{ID: 2, Title: "add support for mDNS discovery abc1234"},
|
||||
))
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
keyword string
|
||||
expectedIDs []int64
|
||||
}{
|
||||
{name: "exact original case", keyword: "SomeThing", expectedIDs: []int64{1}},
|
||||
{name: "case matching original transitions", keyword: "someThing", expectedIDs: []int64{1}},
|
||||
{name: "all lower case", keyword: "something", expectedIDs: []int64{1}},
|
||||
{name: "all upper case", keyword: "SOMETHING", expectedIDs: []int64{1}},
|
||||
{name: "number match", keyword: "12345", expectedIDs: []int64{1}},
|
||||
{name: "number as part", keyword: "1234", expectedIDs: []int64{2}},
|
||||
{name: "sub-word search still works", keyword: "DNS", expectedIDs: []int64{2}},
|
||||
{name: "sub-word search, lower case", keyword: "mdns", expectedIDs: []int64{2}},
|
||||
{name: "keyword is camel case", keyword: "addSupport", expectedIDs: []int64{2}},
|
||||
{name: "keyword not match", keyword: "addsupport", expectedIDs: []int64{}},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
result, err := indexer.Search(t.Context(), &internal.SearchOptions{
|
||||
Keyword: testCase.keyword,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
assert.ElementsMatch(t, testCase.expectedIDs, searchResultIDs(result))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func searchResultIDs(result *internal.SearchResult) []int64 {
|
||||
ids := make([]int64, 0, len(result.Hits))
|
||||
for _, hit := range result.Hits {
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package bleve
|
||||
|
||||
import (
|
||||
"gitea.dev/modules/util"
|
||||
|
||||
"github.com/blevesearch/bleve/v2/analysis"
|
||||
"github.com/blevesearch/bleve/v2/analysis/token/camelcase"
|
||||
"github.com/blevesearch/bleve/v2/registry"
|
||||
)
|
||||
|
||||
const camelCaseKeepWholeName = "camelCaseKeepWhole"
|
||||
|
||||
// camelCaseKeepWholeFilter behaves like bleve's built-in "camelCase" token filter,
|
||||
// it also uses the whole word for a token. For example: when indexing "someThing",
|
||||
// CamelCaseFilter only emits "some" and "thing", this filter also emits "something".
|
||||
// It is questionable why the "issue indexer" used the CamelCaseFilter, it just can't search "someThing".
|
||||
// To avoid breaking existing user experiences, this "whole token filter" is introduced to make the full word can be searched.
|
||||
type camelCaseKeepWholeFilter struct {
|
||||
inner *camelcase.CamelCaseFilter
|
||||
}
|
||||
|
||||
func (f *camelCaseKeepWholeFilter) Filter(input analysis.TokenStream) analysis.TokenStream {
|
||||
// First, do exactly what the stock camelCase filter does: split by "camelCase" tokens
|
||||
split := f.inner.Filter(input)
|
||||
|
||||
// Index the resulting position of the *first* sub-token produced for
|
||||
// each original token (matched by start offset), so the duplicated
|
||||
// whole-word token we add below lines up at the same position as the
|
||||
// sub-word it stands in for, instead of drifting out of sync for
|
||||
// fields with more than one original token.
|
||||
posByStart := make(map[int]int, len(split))
|
||||
for _, tok := range split {
|
||||
if _, ok := posByStart[tok.Start]; !ok {
|
||||
posByStart[tok.Start] = tok.Position
|
||||
}
|
||||
}
|
||||
|
||||
rv := make(analysis.TokenStream, 0, len(split)+len(input))
|
||||
rv = append(rv, split...)
|
||||
|
||||
// Then append one extra, un-split copy of every original token, so the
|
||||
// whole word survives as a standalone, independently searchable term.
|
||||
for _, token := range input {
|
||||
dup := *token
|
||||
dup.Term = append([]byte(nil), token.Term...)
|
||||
if pos, ok := posByStart[token.Start]; ok {
|
||||
dup.Position = pos
|
||||
}
|
||||
rv = append(rv, &dup)
|
||||
}
|
||||
|
||||
return rv
|
||||
}
|
||||
|
||||
func camelCaseKeepWholeFilterConstructor(_ map[string]any, _ *registry.Cache) (analysis.TokenFilter, error) {
|
||||
return &camelCaseKeepWholeFilter{inner: camelcase.NewCamelCaseFilter()}, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
util.MustNoError(registry.RegisterTokenFilter(camelCaseKeepWholeName, camelCaseKeepWholeFilterConstructor))
|
||||
}
|
||||
Reference in New Issue
Block a user