mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-08-03 00:45:22 +02:00
Global code search support (#3664)
* add global code search on explore * fix bug when no anyone public repos * change the icon * fix typo and add UnitTypeCode check for login non-admin user * fix ui description when no match
This commit is contained in:
parent
4163cdf3ea
commit
9e5d0a09eb
10 changed files with 238 additions and 9 deletions
|
@ -16,6 +16,7 @@ import (
|
|||
"github.com/blevesearch/bleve/analysis/token/lowercase"
|
||||
"github.com/blevesearch/bleve/analysis/token/unique"
|
||||
"github.com/blevesearch/bleve/analysis/tokenizer/unicode"
|
||||
"github.com/blevesearch/bleve/search/query"
|
||||
"github.com/ethantkoenig/rupture"
|
||||
)
|
||||
|
||||
|
@ -158,6 +159,7 @@ func DeleteRepoFromIndexer(repoID int64) error {
|
|||
|
||||
// RepoSearchResult result of performing a search in a repo
|
||||
type RepoSearchResult struct {
|
||||
RepoID int64
|
||||
StartIndex int
|
||||
EndIndex int
|
||||
Filename string
|
||||
|
@ -166,17 +168,29 @@ type RepoSearchResult struct {
|
|||
|
||||
// SearchRepoByKeyword searches for files in the specified repo.
|
||||
// Returns the matching file-paths
|
||||
func SearchRepoByKeyword(repoID int64, keyword string, page, pageSize int) (int64, []*RepoSearchResult, error) {
|
||||
func SearchRepoByKeyword(repoIDs []int64, keyword string, page, pageSize int) (int64, []*RepoSearchResult, error) {
|
||||
phraseQuery := bleve.NewMatchPhraseQuery(keyword)
|
||||
phraseQuery.FieldVal = "Content"
|
||||
phraseQuery.Analyzer = repoIndexerAnalyzer
|
||||
indexerQuery := bleve.NewConjunctionQuery(
|
||||
numericEqualityQuery(repoID, "RepoID"),
|
||||
phraseQuery,
|
||||
)
|
||||
|
||||
var indexerQuery query.Query
|
||||
if len(repoIDs) > 0 {
|
||||
var repoQueries = make([]query.Query, 0, len(repoIDs))
|
||||
for _, repoID := range repoIDs {
|
||||
repoQueries = append(repoQueries, numericEqualityQuery(repoID, "RepoID"))
|
||||
}
|
||||
|
||||
indexerQuery = bleve.NewConjunctionQuery(
|
||||
bleve.NewDisjunctionQuery(repoQueries...),
|
||||
phraseQuery,
|
||||
)
|
||||
} else {
|
||||
indexerQuery = phraseQuery
|
||||
}
|
||||
|
||||
from := (page - 1) * pageSize
|
||||
searchRequest := bleve.NewSearchRequestOptions(indexerQuery, pageSize, from, false)
|
||||
searchRequest.Fields = []string{"Content"}
|
||||
searchRequest.Fields = []string{"Content", "RepoID"}
|
||||
searchRequest.IncludeLocations = true
|
||||
|
||||
result, err := repoIndexer.Search(searchRequest)
|
||||
|
@ -199,6 +213,7 @@ func SearchRepoByKeyword(repoID int64, keyword string, page, pageSize int) (int6
|
|||
}
|
||||
}
|
||||
searchResults[i] = &RepoSearchResult{
|
||||
RepoID: int64(hit.Fields["RepoID"].(float64)),
|
||||
StartIndex: startIndex,
|
||||
EndIndex: endIndex,
|
||||
Filename: filenameOfIndexerID(hit.ID),
|
||||
|
|
|
@ -17,6 +17,7 @@ import (
|
|||
|
||||
// Result a search result to display
|
||||
type Result struct {
|
||||
RepoID int64
|
||||
Filename string
|
||||
HighlightClass string
|
||||
LineNumbers []int
|
||||
|
@ -98,6 +99,7 @@ func searchResult(result *indexer.RepoSearchResult, startIndex, endIndex int) (*
|
|||
index += len(line)
|
||||
}
|
||||
return &Result{
|
||||
RepoID: result.RepoID,
|
||||
Filename: result.Filename,
|
||||
HighlightClass: highlight.FileNameToHighlightClass(result.Filename),
|
||||
LineNumbers: lineNumbers,
|
||||
|
@ -106,12 +108,12 @@ func searchResult(result *indexer.RepoSearchResult, startIndex, endIndex int) (*
|
|||
}
|
||||
|
||||
// PerformSearch perform a search on a repository
|
||||
func PerformSearch(repoID int64, keyword string, page, pageSize int) (int, []*Result, error) {
|
||||
func PerformSearch(repoIDs []int64, keyword string, page, pageSize int) (int, []*Result, error) {
|
||||
if len(keyword) == 0 {
|
||||
return 0, nil, nil
|
||||
}
|
||||
|
||||
total, results, err := indexer.SearchRepoByKeyword(repoID, keyword, page, pageSize)
|
||||
total, results, err := indexer.SearchRepoByKeyword(repoIDs, keyword, page, pageSize)
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue