chore: minor code cleanup in search (#10549)

Minor code cleanup for code/issue search.

Mostly breaking up the common functionality into separate functions :)

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/10549
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
Co-authored-by: Shiny Nematoda <snematoda.751k2@aleeas.com>
Co-committed-by: Shiny Nematoda <snematoda.751k2@aleeas.com>
This commit is contained in:
Shiny Nematoda 2025-12-23 00:38:51 +01:00 committed by Gusted
parent 537a802125
commit b1adc7d931
6 changed files with 113 additions and 106 deletions

View file

@ -7,6 +7,7 @@ import (
"bytes"
"context"
"html/template"
"slices"
"strings"
"forgejo.org/modules/highlight"
@ -46,6 +47,19 @@ const (
SearchModeFuzzy = internal.CodeSearchModeFuzzy
)
type Results []*Result
// Get the set of repo IDs from a list of search results
func (res Results) RepoIDs() []int64 {
ids := make([]int64, len(res))
for _, r := range res {
if !slices.Contains(ids, r.RepoID) {
ids = append(ids, r.RepoID)
}
}
return ids
}
func indices(content string, selectionStartIndex, selectionEndIndex int) (int, int) {
startIndex := selectionStartIndex
numLinesBefore := 0
@ -218,7 +232,7 @@ func searchResult(result *internal.SearchResult, startIndex, endIndex int) (*Res
}
// PerformSearch perform a search on a repository
func PerformSearch(ctx context.Context, opts *SearchOptions) (int, []*Result, []*SearchResultLanguages, error) {
func PerformSearch(ctx context.Context, opts *SearchOptions) (int, Results, []*SearchResultLanguages, error) {
if opts == nil || len(opts.Keyword) == 0 {
return 0, nil, nil, nil
}

View file

@ -233,21 +233,19 @@ func (b *Indexer) Search(ctx context.Context, options *internal.SearchOptions) (
}
var keywords []string
if len(options.Tokens) != 0 {
for _, token := range options.Tokens {
if !token.Fuzzy {
// to make it a phrase search, we have to quote the keyword(s)
// https://www.meilisearch.com/docs/reference/api/search#phrase-search
token.Term = doubleQuoteKeyword(token.Term)
}
// internal.BoolOptShould (Default, requires no modifications)
// internal.BoolOptMust (Not supported by meilisearch)
if token.Kind == internal.BoolOptNot {
token.Term = "-" + token.Term
}
keywords = append(keywords, token.Term)
for _, token := range options.Tokens {
if !token.Fuzzy {
// to make it a phrase search, we have to quote the keyword(s)
// https://www.meilisearch.com/docs/reference/api/search#phrase-search
token.Term = doubleQuoteKeyword(token.Term)
}
// internal.BoolOptShould (Default, requires no modifications)
// internal.BoolOptMust (Not supported by meilisearch)
if token.Kind == internal.BoolOptNot {
token.Term = "-" + token.Term
}
keywords = append(keywords, token.Term)
}
searchRes, err := b.inner.Client.Index(b.inner.VersionedIndexName()).