Files
gitea/modules/indexer/code/bleve/bleve_test.go
T
Rafail Giavrimisandsilverwind 38389f00be feat: unify code search, add search API (#39403)
Adds GitHub-style code search endpoints and makes code search behave the
same in the web UI and the API, whether it runs on git grep, bleve or
Elasticsearch.

- `GET /repos/{owner}/{repo}/code/search` searches any branch, tag or
commit with git grep, so it works without the code indexer.
- `GET /search/code` searches the default branches of all accessible
repositories with the code indexer.
- One query syntax everywhere: every term must match a file's content or
path, case-insensitively. It supports `"phrases"`, `/regexp/` (git grep
only) and the `path:`, `language:`, `repo:`, `user:` and `org:`
qualifiers. Invalid queries return an error.
- All engines apply the same include, exclude, vendored and size rules,
and return every match. The web UI shows them as multiple snippets per
file with the matched text highlighted.

<img width="359" height="295" alt="image"
src="https://github.com/user-attachments/assets/59b7aa66-b1dc-4938-9ef9-66668ae08d7c"
/>

Fixes: https://github.com/go-gitea/gitea/issues/10706
Fixed: https://github.com/go-gitea/gitea/issues/15841
Fixes: https://github.com/go-gitea/gitea/issues/31375
Related: https://gitea.com/gitea/gitea-mcp/issues/280
Docs: https://gitea.com/gitea/docs/pulls/615

AI usage disclosure: Yes, I used AI to write code, review changes and
verify the factuality of the overview I provided. I take full
responsibility for the code in this PR.

---------

Co-authored-by: silverwind <me@silverwind.io>
2026-11-13 07:24:37 +00:00

73 lines
2.3 KiB
Go

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package bleve
import (
"testing"
"time"
"gitea.dev/models/db"
"gitea.dev/modules/indexer/code/internal"
inner_bleve "gitea.dev/modules/indexer/internal/bleve"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestBleveSearch(t *testing.T) {
indexer := NewIndexer(t.TempDir())
t.Cleanup(indexer.Close)
_, err := indexer.Init(t.Context())
require.NoError(t, err)
batch := inner_bleve.NewFlushingBatch(indexer.inner.Indexer, maxBatchSize)
for _, document := range []RepoIndexerData{
{Filename: "hit.go", Content: "abcdef", Language: "Go"},
{Filename: "false.go", Content: "abc bcd cde def", Language: "Go"},
{Filename: "dir/a\nb.txt", Content: "needle"},
} {
document.RepoID = 9007199254740993
document.UpdatedAt = time.Now().UTC()
require.NoError(t, batch.Index(internal.FilenameIndexerID(document.RepoID, document.Filename), &document))
}
require.NoError(t, batch.Flush())
for _, testCase := range []struct {
name string
term string
path string
page int
total int64
filenames []string
languageCount int
}{
{name: "short term page past end", term: "abc", page: 3, total: 2, languageCount: 2},
{name: "verified language count", term: "abcdef", page: 1, total: 1, filenames: []string{"hit.go"}, languageCount: 1},
{name: "global directory with newline filename", term: "needle", path: "dir", page: 1, total: 1, filenames: []string{"dir/a\nb.txt"}},
} {
t.Run(testCase.name, func(t *testing.T) {
total, results, languages, err := indexer.Search(t.Context(), &internal.SearchOptions{
Terms: []string{testCase.term},
Path: testCase.path,
Paginator: &db.ListOptions{Page: testCase.page, PageSize: 10},
})
require.NoError(t, err)
assert.Equal(t, testCase.total, total)
var filenames []string
for _, result := range results {
assert.EqualValues(t, 9007199254740993, result.RepoID)
filenames = append(filenames, result.Filename)
}
assert.Equal(t, testCase.filenames, filenames)
if testCase.languageCount > 0 {
require.Len(t, languages, 1)
assert.Equal(t, "Go", languages[0].Language)
assert.Equal(t, testCase.languageCount, languages[0].Count)
} else {
assert.Empty(t, languages)
}
})
}
}