Files
gitea/tests/integration/repo_search_test.go
T
1ebeade2c1 fix(indexer): search code as case-insensitive literal substrings (#40028)
Code search now matches case-insensitive literal substrings like GitHub,
in every script. Word tokenizers can't do this: the ES pattern dropped
every non-ASCII word, and the `standard` tokenizer keeps `console.log`
as one token.

- Elasticsearch indexes positional trigrams, plus 1-2 grams for short
terms
- Bleve indexes distinct 1-3 grams as a candidate filter and verifies
candidates against the content before paging, because phrase queries
over trigrams are quadratic in repeated grams
- Snippet offsets come from the original content, so `<em>` in sources
and width-changing lowercase no longer misplace them
- `search_mode=fuzzy` requests behave like Words, the UI never offered
fuzzy for code search
- The ES code indexer test now runs in CI, it was skipped before

There are no UI changes. Both code indexes rebuild after upgrade.

Fixes: https://github.com/go-gitea/gitea/issues/40027
Fixes: https://github.com/go-gitea/gitea/issues/16158
Fixes: https://github.com/go-gitea/gitea/issues/21359

---------

Co-authored-by: silverwind <me@silverwind.io>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
2026-10-28 20:38:45 +08:00

73 lines
2.5 KiB
Go

// Copyright 2017 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package integration
import (
"net/http"
"testing"
repo_model "gitea.dev/models/repo"
"gitea.dev/modules/git"
code_indexer "gitea.dev/modules/indexer/code"
"gitea.dev/modules/setting"
"gitea.dev/modules/test"
"gitea.dev/tests"
"github.com/PuerkitoBio/goquery"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func resultFilenames(doc *HTMLDoc) []string {
filenameSelections := doc.doc.Find(".repository.search").Find(".repo-search-result").Find(".header").Find("span.file")
result := make([]string, filenameSelections.Length())
filenameSelections.Each(func(i int, selection *goquery.Selection) {
result[i] = selection.Text()
})
return result
}
func TestSearchRepo(t *testing.T) {
defer tests.PrepareTestEnv(t)()
repo, err := repo_model.GetRepositoryByOwnerAndName(t.Context(), "user2", "repo1")
assert.NoError(t, err)
code_indexer.UpdateRepoIndexer(repo)
testSearch(t, "/user2/repo1/search?q=Description&page=1", []string{"README.md"})
defer test.MockVariableValue(&setting.Indexer.IncludePatterns, setting.IndexerGlobFromString("**.txt"))()
defer test.MockVariableValue(&setting.Indexer.ExcludePatterns, setting.IndexerGlobFromString("**/y/**"))()
repo, err = repo_model.GetRepositoryByOwnerAndName(t.Context(), "user2", "glob")
require.NoError(t, err)
require.NoError(t, git.ForceFastImport(t.Context(), repo.CodeStorageRepo(), []git.FastImportCommit{{
Ref: "refs/heads/master",
Files: []git.FastImportFile{
{Path: "a.txt", Content: "file1 loren ipsum\n"},
{Path: "aaa.doc", Content: "file2 loren ipsum\n"},
{Path: "x/b.txt", Content: "file3\n"},
{Path: "x/y/a.txt", Content: "file4\n"},
{Path: "x/y/z/a.txt", Content: "file5\n"},
},
}}))
code_indexer.UpdateRepoIndexer(repo)
testSearch(t, "/user2/glob/search?q=loren&page=1", []string{"a.txt"})
testSearch(t, "/user2/glob/search?q=loren&page=1&t=match", []string{"a.txt"})
testSearch(t, "/user2/glob/search?q=file3&page=1", []string{"x/b.txt"})
testSearch(t, "/user2/glob/search?q=file3&page=1&t=match", []string{"x/b.txt"})
testSearch(t, "/user2/glob/search?q=file4&page=1&t=match", []string{})
testSearch(t, "/user2/glob/search?q=file5&page=1&t=match", []string{})
}
func testSearch(t *testing.T, url string, expected []string) {
t.Helper()
req := NewRequest(t, "GET", url)
resp := MakeRequest(t, req, http.StatusOK)
filenames := resultFilenames(NewHTMLParser(t, resp.Body))
assert.Equal(t, expected, filenames, "url=%s", url)
}